var ifIE = false;
var scroller = null;
var speed = 0;
var swidth = false;
var kids;
var blocksCount = 0;

$(document).ready(function() {
	$('a.foto').lightBox({imagesUrl: 'http://demo.desire.lt/mamosgidas.lt/beta/js/'});
	
	ifIE = $.browser.msie;
	
	wwidth = $(window).width();
	scroller = $(".scrollerdiv");
	speed = 1;
	swidth = scroller.width();
		
	if(ifIE){
		scroller.css('position', 'absolute');
	} else {
		scroller.css('position', 'fixed');
	}

	scroller.css('visibility', 'visible');
	scroller.css('display', 'block');
	
	updatePosition();
	scrool(0);
	
	var cookieMenu = $.cookie('cookieMenu');
	
	if (!cookieMenu) {
		cookieMenu = 'sub0';
	}
	
	var timeoutMenu;
	
	$('#mainMenu .topFirst').hover(function() {
		clearTimeout(timeoutMenu);
		$('.topFirst').removeClass('active');
		$(this).addClass('active');
		
		link = $(this).children('.item').children('a');
		
		if ($('#' + $(link).attr('class')).length > 0) {
			$('.topSubs').hide();
			$('#' + $(link).attr('class')).show();
		}
	});
	
	$('#mainMenu').mouseover(function() {
		clearTimeout(timeoutMenu);
	});
	
	$('#mainMenu').mouseleave(function() {
		timeoutMenu = setTimeout(function(){
			$('.topFirst').removeClass('active');
			$('.topSubs').hide();
			
			if (cookieMenu) {
				$('.' + cookieMenu).parent().parent().addClass('active')
				$('#' + cookieMenu).show();
			}
		}, 1000);
	});
	
	$('.topFirst').removeClass('active');
	$('.topSubs').hide();
	$('.' + cookieMenu).parent().parent().addClass('active')
	$('#' + cookieMenu).show();
	
	$('#gallery_container').show();
	
});

function storePosition(element, url) {
	$.cookie('cookieMenu', element, { expires: 1, path: '/' });
	
	if (url) {
		window.location = url;
	}
}

function showInner(id, obj) {
	if (!$(obj).hasClass('active')) {
		$('.inner').slideUp(700);
		$('ul.innerMenu li a').removeClass('active');
		
		$('#inner'+id).slideToggle(700);
		
		$(obj).addClass('active');
	}
	
	return false;
}

function toggleClass(obj) {
	//if ($(obj).hasClass('active'))
}

function scrool(widt) {
	if (blocksCount > 0) {
	    widt = widt-speed;
	    
	    if (widt < (swidth / -blocksCount)) {
			widt = 0;
			kids = scroller.children();
			$(kids[0]).remove().appendTo(".scrollerdiv");
	    }
	    
	    scroller.css('left', (widt)+'px');
	    alertTimerId    = setTimeout('scrool('+widt+')', 30);
	}
}

function updatePosition() {	
	if(ifIE){
		scroller.css('top', (document.documentElement.scrollTop + $(window).height() - 37)+'px');
	} else {
		scroller.css('top', (window.innerHeight - 35)+'px');
	}
	
	alertTimerId    = setTimeout('updatePosition()', 100);
}

//function commentsMenu(read) {
//	
//	$('div#readComments').removeClass('active');
//	$('div#writeComment').removeClass('active');
//	$('div#readComments').removeClass('inActive');
//	$('div#writeComment').removeClass('inActive');
//	
//	$('div.toComment').addClass('hidden');
//	$('div.commentsList').addClass('hidden');
//	
//	if (read) {
//		$('div#readComments').addClass('active');
//		$('div#writeComment').addClass('inActive');
//		
//		$('div.toComment').hide();
//		$('div.commentsList').show();
//		
//	} else {
//		$('div#writeComment').addClass('active');
//		$('div#readComments').addClass('inActive');
//		
//		$('div.commentsList').hide();
//		$('div.toComment').show();
//	}
//}

function notifyComment(commentId, obj) {
	if (confirm(wrongCommentQuestion) && commentId) {
		$.post(servicesUrl + '/notifyComment', {commentId: commentId},
			function(data){
				data = $.trim(data);
				
				if (data == 1) {
					$(obj).html(wrongCommentAnswer);
					$(obj).attr('title', wrongCommentAnswer);
					$(obj).removeAttr('onclick');
				}
				
				alert(wrongCommentAnswer);
			}
		, 'text');
	}
}

function toggleVisibility(elementId) {
	$('#' + elementId).slideToggle(400);
}

function rightTabsToggle(root, item, tab) {
	$(root + ' .topicalities').hide();
	$(root + ' ' + item).show();
	$(root + ' .tabActive').addClass('tab');
	$(root + ' .tabActive').removeClass('tabActive');
	$(root + ' ' + tab).addClass('tabActive');
	$(root + ' .tabActive').removeClass('tab');
}
