$(document).ready(function(){
	initSafary();
	initSlide();
	initHover();
	initMenu();
});


function initSafary(){
	if (jQuery.browser.safari) {$("p").css('word-spacing:','-1px')};
}


function initHover(){
	$('ul.case a').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover');}
	)
	
	var cur_href = $('#sidebar ul.menu li.active div.bottom > a').attr('href');

	if ($.cookie('location') == cur_href) {
		$('#sidebar ul.menu li.active').find('ul').css({
			display : 'block',
			visibility: 'visible'
		});
	}
	else {
		$('#sidebar ul.menu li.active').find('ul').slideDown(1000, function(){
			$(this).css('visibility','visible');
		});
	}
	
	$('#sidebar ul.menu li.active a.active').click(function(){
		return false;
	});
	
	$('#sidebar ul.menu > li div.bottom > a').click(function(){
		if ($(this).parents('ul.menu > li > div.top > div.bottom > ul').hasClass('active')) return true;
		
		if ( $('#sidebar ul.menu li.active ul').get(0) ) {
			if ($(this).hasClass('active')) return false;
			
			var href = $(this).attr('href');
			$('#sidebar ul.menu li.active ul').css('opacity',0);
			$('#sidebar ul.menu li.active ul').slideUp(1000, function(){
				$(this).css({'visibility':'hidden', 'opacity':1});
				window.location = href;
			});
			
			return false;
		}
		else return true;
	});
}


function initMenu(){
		var href = $('#sidebar ul.menu li.active div.bottom > a').attr('href');
		$.cookie('location',href);
}

var activePreview = 1;
function initSlide(){
	if ( !$("#content div.slide > div.slider > div.carousel").get(0) ) return false;
	
	var contWidth = $("#content div.slide > div.slider > div.carousel").width();
	
	var itemWidth = $('#content div.slide > div.slider > div.carousel > ul > li').width() + parseInt($('#content div.slide > div.slider > div.carousel > ul > li').css('margin-left')) + parseInt($('#content div.slide > div.slider > div.carousel > ul > li').css('margin-right'));
	var listWidth = itemWidth * $('#content div.slide > div.slider > div.carousel > ul > li').size();
	
	var scroll = 1;
	var scrollWidth = scroll * itemWidth;
	
	if (contWidth >= listWidth) $("#content div.slide > div.slider > a.btn-next, #content div.slide > div.slider > a.btn-previous").addClass('inactive').css('opacity', 0.3);
	
	var margin = 0;
	activePreview = 1;
	
	$("#content div.slide > div.slider > a.btn-next").click(function(){
		if ((margin-contWidth) <=  -listWidth) {
			$(this).addClass('inactive').css('opacity', 0.3);
			return false;
		}
		
		$("#content div.slide > div.slider > a.btn-previous").removeClass('inactive').css('opacity', 1);
		
		if ( (margin-contWidth+listWidth) <= scrollWidth ){
			activePreview += (margin-contWidth+listWidth) / itemWidth;
			margin = -listWidth + contWidth;
			$(this).addClass('inactive').css('opacity', 0.3);
		}
		else {
			margin -= scrollWidth;
			activePreview += scroll;
		}
		
		activeImage($('#content div.slide > div.slider > div.carousel > ul > li:nth-child('+activePreview+') a'));
		$('#content div.slide > div.slider > div.carousel > ul').animate({
			marginLeft: margin
		},500);
		
		return false;
	});
	
	$("#content div.slide > div.slider > a.btn-previous").addClass('inactive').css('opacity', 0.3).click(function(){
		if (margin == 0) {
			$(this).addClass('inactive').css('opacity', 0.3);
			return false;
		}
		
		$("#content div.slide > div.slider > a.btn-next").removeClass('inactive').css('opacity', 1);
		
		if ( (-margin) <= scrollWidth ) {
			activePreview += margin / itemWidth;
			margin = 0;
			$(this).addClass('inactive').css('opacity', 0.3);
		}
		else {
			margin += scrollWidth;
			activePreview -= scroll;
		}
		
		activeImage($('#content div.slide > div.slider > div.carousel > ul > li:nth-child('+activePreview+') a'));
		$('#content div.slide > div.slider > div.carousel > ul').animate({
			marginLeft: margin
		},500,'',function(){
			
		});
		
		return false;
	});

	
	$("#content div.slide > div.slider > div.carousel > ul > li[class='active']").find('img').css('opacity','0.3');

	$("#content div.slide > div.slider > div.carousel > ul > li a").click(function(e) {
		e.preventDefault();
		activeImage(this);
	});
}

function activeImage(previewLink){
	$("#content div.slide > div.image img").attr("src", $(previewLink).attr("rel"));
	
	var text = $(previewLink).parents('li').find('div.text').html();
	$('#content div.leftcol-text').empty().html(text);
	
	$(previewLink).parents('ul').find('li img').css('opacity','1');
	$(previewLink).parents('ul').find('li').removeClass('active');
	$(previewLink).parents('li').addClass('active');
	$("#content div.slide > div.slider > div.carousel > ul > li[class='active']").find('img').css('opacity','0.3');
	activePreview = $("#content div.slide > div.slider > div.carousel > ul > li").index($('#content div.slide > div.slider > div.carousel > ul > li.active')[0]) + 1;
}









