$(document).ready(function(){
	$(".lightbox").lightbox();
	
	opacity = {
		current: 1,
		row: .7,
		other: .5,
		none: 0
	};
	duration = {
		long: 600,
		medium: 300,
		short: 300
	}

	items = $('.nav-row a');
	clones = $('.nav-row a').find('img').addClass('default').clone();
	
	var isIE = false;
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && jQuery.browser.version.substr(0,1)=="6") {
			isIE = true;
		}
	});

	$('.nav-row a').each(function(i) {
		if(isIE) {
			src = clones.eq(i).attr('src').replace(/^(.*)\.png$/, "$1.gif");
			$('.nav-row a img.default').eq(i).attr('src', src);
		} else {
			src = clones.eq(i).attr('src').replace(/^(.*)\.png$/, "$1-hover.png");
		}
		
		clones.eq(i).removeClass('default').addClass('hover');
		clones.eq(i).attr('src', src);
		$(this).append(clones.eq(i));
	});
	
	$('.nav-row a.current').find('img.hover').css({opacity: opacity.none});
	$('.nav-row a').not('.current').find('img.default').css({opacity: opacity.other});
	$('.nav-row a').not('.current').find('img.hover').css({opacity: opacity.none});
	
	currentOpacity = false;
	$('.nav-row a')
		.bind('mouseover', function(){
			if(!isIE) {
				currentOpacity = $(this).find('img.default').css('opacity');
				$(this).find('img.default').stop().animate({opacity: opacity.none}, duration.long);
				$(this).find('img.hover').stop().animate({opacity: 1}, duration.short);
			} else {
				$(this).find('img.hover').css({opacity: 1});
			}
		}).bind('mouseout', function(){
			if(!isIE) {
				$(this).find('img.default').stop().animate({opacity: currentOpacity}, duration.short);
				$(this).find('img.hover').stop().animate({opacity: opacity.none}, duration.long);
			} else {
				$(this).find('img.hover').css({opacity: 0});
			}
		}
	);
		
	// modules interaction
	mMinTop = -50; mMaxTop = -5; mMinOpac = .6;	mMaxOpac = 1;
	$('.header-module-wrapper')
		.css({top: mMinTop, opacity: mMinOpac})
		.unbind('mouseover').unbind('mouseout')
		.bind('mouseover', function(){$(this).css({opacity: mMaxOpac})})
		.bind('mouseout', function(){$(this).css({opacity: mMinOpac})})
		.find('.bottom')
		.toggle(
			function() {
				$(this).find('.info').fadeOut(function(){$(this).next().fadeIn().addClass('visible')});
				$(this).parent().parent()
					.animate({top: mMaxTop, opacity: mMaxOpac}, {queue: false})
					.unbind('mouseout');
			},
			function() {
				$(this).find('.info').fadeIn(function(){$(this).next().removeClass('visible')});
				$(this).removeClass('visible')
					.parent().parent()
					.animate({top: mMinTop, opacity: mMinOpac}, {queue: false})
					.bind('mouseout', function(){$(this).css({opacity: mMinOpac})});
			}
		);
	
	//newsletter
	defaultText = $('#newsletter-email').val();
	$('#newsletter-email').click(function(){$(this).removeClass('default').val('')});
	$('#newsletter-email').blur(function(){
		if($(this).val() == '') $(this).addClass('default').val(defaultText);
	});
	
	$("a.group").fancybox({
		'overlayOpacity': 0.8,
		'hideOnContentClick': false,
		'frameWidth': 480,
		'frameHeight': 380
	});

});


function next(){
	var totalban=$('#totalbanners').val();
	if(current==totalban){
		current=1;
	}else{
		current++;
	}

	for(i=1;i<=totalban;i++){
		$('#img'+i).hide();
		$('#div'+i).hide();
	}
	$('#img'+current).show();
	$('#div'+current).show();
	banner = window.clearInterval(banner);
	banner = self.setInterval("next()",5000);
}

function previous(){
	var totalban=$('#totalbanners').val();
	if(current==1){
		current=totalban;
	}else{
		current--;
	}

	for(i=1;i<=totalban;i++){
		$('#img'+i).hide();
		$('#div'+i).hide();
	}
	$('#img'+current).show();
	$('#div'+current).show();
	banner = window.clearInterval(banner);
	banner = self.setInterval("next()",5000);
}

function newsletter() {
	if($('#newsletter-email').val()!=""){
		email=$('#newsletter-email').val();
		$.ajax({
			url: url_base+'ajax/newsletter.php',
			type: 'GET',
			cache: false,
			dataType: 'html',
			data: '&email='+email,
			success: function(responseText){
				vals = responseText.split("___");
				if(vals[0]==0){
					$('#newsletter-email').css('border','1px solid #f00');
				}else{
					$('#newsletter-email').css('border','none');
				}
				$('#newsletter-email').val(vals[1]);
			}
		});
	}
}