function setvjCookie() {
	var expire = new Date();
	expire.setTime(expire.getTime() + 3600000*24*180);
	document.cookie = "fancy=never;expires="  + expire.toGMTString()+ ";path=/";
	$.fancybox.close();
}
var leaving = true;
$(document).ready(function() {
	$("a#inline").fancybox({
		'hideOnOverlayClick': false,
		'onComplete': (function(){$('#fancybox-outer').corner();})
	});
	$(".gallery-icon a").each(function () {
		$(this).attr('rel','gallery');
	});
	$(".gallery-icon a").fancybox({
		'titleShow' : false
	});
	if ($('.tabs').length){
		$('.prev').addClass('disabled');
		$(".tabs li:first").addClass('vjfirst');
		$wid = $(".tabs li:last").outerWidth(true);
		$(".tabs li:last").prevAll().each(function () {
			$wid += $(this).outerWidth(true);
			if($wid<700)
				$last = $(this);
			else
				return false;
		});
		if($last.hasClass('vjfirst'))
			$('next').addClass('disabled');
		else{
			$last.addClass('vjlast');
			$('.prev').bind('click', function() {
				$old = $('.first');
				$new = $('.first').prev();
				if($new.hasClass('vjfirst'))
					$('.items').animate({
						left: '0'
						},500);
				else
					$('.items').animate({
						left: '+='+$new.outerWidth(true)
						},500);
				if($('.next').hasClass('disabled'))
					$('.next').removeClass('disabled');
				$old.removeClass('first');
				$new.addClass('first');
				if($new.hasClass('vjfirst'))
					$('.prev').addClass('disabled');
			
			});
			$('.next').bind('click', function() {
				$old = $('.first');
				$new = $('.first').next();
				$('.items').animate({
					left: '-='+$old.outerWidth(true)
					},500);
				if($('.prev').hasClass('disabled'))
					$('.prev').removeClass('disabled');
				$old.removeClass('first');
				$new.addClass('first');
				if($new.hasClass('vjlast'))
					$('.next').addClass('disabled');
			
			});
			if(!$('.next').hasClass('disabled') && !$('.vjfirst').hasClass('current')){
				var hidenext;
				$width=$('.vjfirst').outerWidth(true);
				$('.tabs li').each(function () {
					if(!$(this).hasClass('vjfirst')){
					$first = $(this);
					if($first.hasClass('vjlast')){
						hidenext=true;
						return false;
					}
					if ($first.hasClass('current'))
						return false;
					$width += $first.outerWidth(true);
					}
				});
				$('.items').animate({
					left: '-'+$width
					},0);
				$('.first').removeClass('first');
				$first.addClass('first');
				if(hidenext != undefined)
					$('.next').addClass('disabled');
			}
			if(!$('.vjfirst').hasClass('first'))
				$('.prev').removeClass('disabled');
		}

	}

	$("a").bind('click', function(e) {
		var link = $(this).attr('href');
		if(link != undefined && link.indexOf(window.location.host) != -1 && e.which ==1)
			leaving = false;
	});
	$('form').submit(function() {
		var link = $(this).attr('action');
		if(!link || link.indexOf(window.location.host) != -1 )
			leaving = false;
		});
    jQuery("#menu-main-menu ul").css({display: "none"}); // Opera Fix 
    jQuery("#menu-main-menu li").hover(function(){ 
		jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).show(268); 
    },function(){ 
		jQuery(this).find('ul:first').css({visibility: "hidden"}); 
    }); 
	if ($.browser.msie && (parseInt($.browser.version) <= 7)) {
		try {
			document.execCommand('BackgroundImageCache', false, true);
		} catch(e){}
	}
	if (window.navigator.userAgent.indexOf("Chrome") !== -1)
		$('html').css('-webkit-text-size-adjust', 'none');

	(function() {
		if ($('#product-slideshow').length < 1) return false;
		var index = 0,
			min = 1,
			lis = $('#product-slideshow li');
		var len = $('#product-slideshow li').length;
		$('#product-slideshow .btn').click(function() {		
			var ind = $('#product-slideshow ul li').index($('#product-slideshow ul li.active:first'));
			if (len <= min) return;
			if ($(this).hasClass('right')) {
				next = (ind+1) % len;
				index = index >= (len - 1) ? (min - 1) : ++index;
			} else {
				next = (ind+len-1) % len;
				index = index <= (min - 1) ? (len - 1) : --index;
			}
			ind = ind+1;
			next = next+1;
			$('#product-slideshow ul li:nth-child('+ind+'),#info-'+ind).fadeOut(500,function(){$(this).removeClass('active')});
			setTimeout(function() {$('#product-slideshow ul li:nth-child('+next+'),#info-'+next).fadeIn(500).addClass('active');}, 500);
		});
	})();
});

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
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 unload(){
	var neva = readCookie('fancy');
	if(neva != 'never'){
		var nValue = parseInt(neva);
		if ((nValue > 1) || leaving)
			createCookie('fancy',nValue-1);
		else if(nValue == 1)
			createCookie('fancy',-1);

	}
}
function load(){
	var neva = readCookie('fancy');
	if(neva == null){
		createCookie('fancy',1);
		$(document).ready(function() {
			$("#inline").trigger('click');
		});
	}
	else if(neva != 'never'){
		var nValue = parseInt(neva);
		if (nValue) {
			if(nValue >0)
				createCookie('fancy',1+nValue);
			else
				createCookie('fancy',1);
		}

	}
}

