$(document).ready(function(){
	onWindowResized();
	
	$(window).wresize( function(){    
		onWindowResized();
	});

	showSelectedProducts(product_id);
	
	if (b_alcohol) {
		
		if($.cookie('a_mixtips-b_pass_agecheck') == '1')
		{
			$(".product-specific-cont-hidden").removeClass('product-specific-cont-hidden');
			// fix for right tab border on page load
			//$('.tab_border_correct').attr('height','0');
			$(document).pngFix();
			return;
		}
		
		// show popup
		$('a[rel*=facebox]').facebox({opacity : 0.8})
		jQuery.facebox($('#agecheck_popup').html());
		
	}
	else
	{
		$(".product-specific-cont-hidden").removeClass('product-specific-cont-hidden');
		// fix for right tab border on page load
		$('.tab_border_correct_tr').attr('height','28');
		$('.tab_border_correct').css('display','none');
		
	}
	
	$(document).pngFix();
	
});


function afterFooterLoaded() {
	onWindowResized();
}

function passagecheck() {
	$(".product-specific-cont-hidden").removeClass('product-specific-cont-hidden');
	$.cookie('a_mixtips-b_pass_agecheck', '1');
	$.facebox.close()
}

function failagecheck() {
	
	window.location.href = $('input#mixtipurl').val();
}


function onWindowResized()
{
	setViewPort();
	reposFooter();
	reposPageContents(product_id);
}

function reposPageContents(id)
{
	// vars 
	// image size, based on actual file size
	var imgX = 1912;
	var imgY = 971;
	var footerHeight  = $('#area_footer').height();
	
	// repos background
	var bHeight = vpHeight - footerHeight;
	var imgWidth = bHeight/imgY * imgX;
	
	if(imgWidth > vpWidth) 
	{
		$('#mixtip_container').css('width', vpWidth);
		//$('html').css('overflow-x' ,'hidden'); 
	}
	
	// set product page left
	var left = vpWidth/2 - imgWidth/2; 
	var pLeft = ( left < 0 ) ? left : 0;
	$('#product_page').css('left', pLeft);
	
	// set filler
	var fillerWidth = (left < 0) ? 0 : left;
	
	// resize left-right filler
	$('#product-'+id+ ' .resizeable-left img').css('height', bHeight);
	$('#product-'+id+ ' .resizeable-left img').css('width', fillerWidth);
	
	$('#product-'+id+ ' .resizeable-right img').css('height', bHeight);
	$('#product-'+id+ ' .resizeable-right img').css('width', fillerWidth);
	
	// resize filler
	$('#product-'+id+ ' .resizeable-filler img').css('height', bHeight);
	$('#product-'+id+ ' .resizeable-filler img').css('width', vpWidth);

	
	// resize image
	$('#product-'+id+ ' .resizeable-image img').css('height', bHeight);
	
	// repos prod navigation
	var nTop = vpHeight - 280;
	var nLeft = 50;
	$('#mixtip_products').css('top', nTop);
	$('#mixtip_products').css('left', nLeft);
	
	// repos infobox
	var iLeft = '65%';//vpWidth * 15/21 - 30;
	var iTop  = vpHeight/5 - 20;
	$('#product-'+id+ ' .product-info-box').css('left', iLeft);
	$('#product-'+id+ ' .product-info-box').css('top', iTop);
	
	// repos payoff
	// repos payoff
	var pTop = vpHeight/5 - 25;
	var pLeft = vpWidth/6 - 100;
	$('#text-payoff').css('top', pTop);
	$('#text-payoff').css('left', pLeft);
	
	// repos navigation arrows
	if (num_product_items > 1){
		var arrows = '#area_arrows';
		$(arrows).css('display','block');
		var aTop = vpHeight - footerHeight - 80;
		var aLeft= vpWidth/2 - $(arrows).width()/2;
		$(arrows).css('top',aTop);
		$(arrows).css('left',aLeft);
	}
}


function productOn(elm)
{
	if($(elm).hasClass('selected'))
		return;
	var icon = $(elm).attr('rel');
	$(elm).attr('src',icon + '-on.png');
}

function productOff(elm)
{
	if($(elm).hasClass('selected'))
		return;
	var icon = $(elm).attr('rel');
	$(elm).attr('src',icon + '-off.png');
}


function productsPrev()
{
	product_item_start = (product_item_start > 0)? product_item_start - 1 : 0;
	showProductItems(product_item_start);
	if (product_item_start == 0){
		$('.prod_nav_prev_icon').css('display','none');
	} else {
		$('.prod_nav_prev_icon').css('display','');
	}
	$('.prod_nav_next_icon').css('display','');
}

function productsNext()
{
	product_item_start = product_item_start + 1;
	if (num_product_items - product_item_start > num_visible_products)
	{
		$('.prod_nav_next_icon').css('display','');
	} else {
		$('.prod_nav_next_icon').css('display','none');
	}
	$('.prod_nav_prev_icon').css('display','');
	showProductItems(product_item_start);
}

function showProductItems(product_item_start)
{
	$('.prod_icon').each(function(){
		for (var i=0; i < num_product_items; i++)
		{
			if (i >= product_item_start && i < (product_item_start + num_visible_products))
			{
				$('#prod_icon-'+i).css('display','inline');
			} else {
				$('#prod_icon-'+i).css('display','none');
			}
		}
	});
}

function gotoProductIndex(offset)
{
	var target = current_product_index + offset;
	target = (target < 0) ? product_tastes.length - 1 : target;
	target = (target > product_tastes.length - 1) ? 0 : target;
	
	current_product_index = target;
	
	var target_taste = product_tastes[target];
	var elm = $('img.taste-' + target_taste);
	gotoProduct(elm);
}

function gotoProduct(elm)
{
	var id = $(elm).attr('id').replace(/product-id-/,'');
	$.address.title($('#product-'+id).find('h2').attr('title') + ' - ' + s_base_title)
	
	jQuery.each($('#mixtip_products .selected'),function(){
		var icon = $(this).attr('rel');
		$(this).attr('src',icon + '-off.png');
		$(this).removeClass('selected');
	});
	
	var old_prod = 'product-'+product_id;
	var new_prod = 'product-'+id;
	
	var w  = $(window).width();
	var pw = $('#'+old_prod).width();
	var pleft = w/2 - pw/2;
	
	var min_left = $('#body_container').width()/2 - pw/2;
	pleft = (pleft < min_left) ? min_left : pleft;
	
	// prepare the body overflow
	//$('body').css('overflow','hidden');
	
	var icon = $(elm).attr('rel');
	$(elm).attr('src',icon + '-on.png');
	$(elm).addClass('selected');
	
	// hide arrows
	$('.icon-arrow').each(function(){
		$(this).css('display','none');
	});
	
	// show arrow
	//$('.icon-' + id).css('display','block');
	
	// hide old product	
	$('#product-'+product_id).css('display','none');
	
	// prepare new product
	$('#product-'+id).css('display','');
	reposPageContents(id);
	product_id = id;

}

function showSelectedProducts(id)
{
	var taste = $('img#product-id-'+id).attr('class').replace(/taste-/,'');
	
	$('.mixtip_products > .selected').removeClass('selected');
	var icon = $('img#product-id-'+id).attr('rel');
	$('img#product-id-'+id).attr('src',icon + '-on.png');
	$('img#product-id-'+id).addClass('selected');
}

function showTab(elm, id, name)
{
	// reset
	var tabs = ['description','video','meer'];
	for (var i=0; i<3 ; i++)
	{

		$('#'+id+' .tab-' + tabs[i]).css('display','none');
		
		if ($('#'+id+' .t-' + tabs[i] + ' img').attr('src'))
		{
			var img = $('#'+id+' .t-' + tabs[i] + ' img').attr('src').replace(/_over/,'_out');
			$('#'+id+' .t-' + tabs[i] + ' img').attr('src', img);
		}
	}
	
	// fix for right tab border on tab click
	if (name == 'description') {
		// fix for right tab border on page load
		$('.tab_border_correct_tr').attr('height','28');
		$('.tab_border_correct').css('display','none');
	}
	else {
		$('.tab_border_correct_tr').attr('height', '26');
		$('.tab_border_correct').css('display', '');
	}
	
	// show
	$('#'+id+' .tab-' + name).css('display','');
	var src =  $('#'+id+' .t-' + name + ' img').attr('src').replace(/_out/,'_over');

	$('#'+id+' .t-' + name + ' img').attr('src',src);
	
}

function openswfvideo()
{
  jQuery.facebox($('#swfvideo_popup').html());
  //Cufon.replace('#facebox h5');
}

