/*********************************************************************************************************************************/
/* DOCUMENT READY CALLS **********************************************************************************************************/
/*********************************************************************************************************************************/
$(document).ready(function(){

	/* EXTERNAL LINKS start ***********************************************************/
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});	
	/* EXTERNAL LINKS end *************************************************************/
	$('html').addClass('js');
	
	/* NAVIGATION start ***************************************************************/
	$('#nav ul > li.level1 > ul').prepend('<li class="top first"></li>');
	$('#nav ul ul ul').prepend('<li class="top"></li>');
	$('#nav ul ul').append('<li class="bottom"></li>');
	$('#nav ul ul').hide();
	//hover show next level
	$('#nav ul > li').hover(function() { $('ul:first', this).show(); $('a:first',this).addClass('hover'); },function() {$('ul:first', this).hide(); $('a:first',this).removeClass('hover'); });
	//add class for flyouts
	if ($('#nav ul ul ul,#nav-top ul ul ul').length > 0) { $('#nav ul ul ul',this).prev().parent().addClass('flyout'); }
	
	$('#nav li.level1:first').addClass('first');
	$('#nav li.level1:last').addClass('last');
	/* NAVIGATION end ****************************************************************/
	
	/* SLIDESHOW start ***************************************************************/
	var slideshow_start = setInterval( "slideSwitch()", 5000 );
	if($('#slideshow').length > 0){
	$('#slideshow .scroll li a').live("click",function(){
		clearInterval(slideshow_start);
		$('#slideshow .scroll li').removeClass('active');
		$('#slideshow .display p').removeClass('active');
		var $display = $(this).attr('rel');
		$('#'+$display).addClass('active');
		$(this).parent().addClass('active');
	});
	
		
		$("#slideshow .scroll").jCarouselLite({
			visible: 4,
			vertical:true,
			speed: 1000,
			btnNext: "#slideshow .down",
			btnPrev: "#slideshow .up",
			mouseWheel: true,
			circular: false,
			scroll: 1
		});
		$("#slideshow .scroll ul li:first").addClass('active');
	}
	
	/* SLIDESHOW end *****************************************************************/

	/* Product Listing */

	var $popups = $('div.pg_product');
	var $product_previews = $('div.pg_product_preview');

	$(window).bind('click', function(){
		$popups.hide('fast');
	});

	$popups.bind('click', function(event){
		event.stopPropagation();
	});

	$popups.find('.close').bind('click', function(){
		$(this).parent().hide('fast');
	});

	$product_previews.bind('click', function(event){
		event.stopPropagation();
		$popups.hide('fast');
		$(this).find('div.pg_product').show('slow');
	});

	$('select.product_filter').bind('change', function(){
		var $select = $(this);
		createCookie($select.attr('name'), $select.val(), 0);
		window.location.reload();
	});

	$('#remove_style').bind('click', function(){
		eraseCookie('style');
	});
			
	$('#remove_country').bind('click', function(){
		eraseCookie('country');
	});
			
}); //close document.ready

$(window).bind('load', function(){
	/* CAROUSEL */
	if($('#find-your-favorite').length > 0){
		$("#list-tryit-distributing .carousel").jCarouselLite({
			visible: 20,
			speed: 1000,
			btnNext:'#list-tryit-distributing .next',
			btnPrev: '#list-tryit-distributing .prev',
			mouseWheel: true,
			scroll: 5
		});		
		$("#list-balkan-beverages .carousel").jCarouselLite({
			visible: 20,
			speed: 1000,
			btnNext:'#list-balkan-beverages .next',
			btnPrev: '#list-balkan-beverages .prev',
			mouseWheel: true,
			scroll: 5
		});
		$('#find-your-favorite').css({ 'background-image':'none'});
	}
	/* CAROUSEL */

	$('#find-your-favorite li img').hover(
		function(){
			var $img = $(this);
			$img.css({'height' : $img.height() + 10 + 'px', 'marginLeft' : '-2px'});
		},
		function(){
			var $img = $(this);
			$img.css({'height' : $img.height() - 10 + 'px', 'marginLeft' : '0'});
		}
	);
	$('#find-your-favorite .container:first').addClass('active');
	$('#find-your-favorite h3').click(
		function(){
			$('#find-your-favorite .container').removeClass('active');
			$(this).parent().addClass('active');
		}
	);

});

/*********************************************************************************************************************************/
/* DOCUMENT READY FUNCTIONS ******************************************************************************************************/
/*********************************************************************************************************************************/

/* SLIDESHOW function start *********************************************************/
function slideSwitch() {
	var $active = $('#slideshow .display p.active');
	var $active_li = $('#slideshow .scroll li.active');

	if ( $active.length == 0 ) $active = $('#slideshow .display p:last');
	if ( $active_li.length == 0 ) $active_li = $('#slideshow .scroll li:last');

	var $next =  $active.next('p').length ? $active.next() : $('#slideshow .display p:first');
	var $next_li =  $active_li.next('li').length ? $active_li.next() : $('#slideshow .scroll li:first');

	$active.addClass('last-active');
	$active_li.addClass('last-active');
	
	$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {
			 $active.removeClass('active last-active');
			 
	});
			
	$active_li.removeClass('active last-active');	
	$next_li.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {		 
	});
	
}
/* SLIDESHOW function end ***********************************************************/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}