var sLastActCat = '';
var sLastActArt = '';

function validateCompReq()
{
	var blValid = true;
	$('.comp_req').each(function(){
		if (blValid && this.value == '')
		{
			blValid = false;
			$("#dialog-submitCart").dialog({
				resizable: false,
				height:140,
				modal: true,
				buttons: {
					'Atšaukti': function() {
						$(this).dialog('close');
						
					}
				}
			});			
		}
	});
	return blValid;
}

function hideLoader(path){
	$("."+path+" .loader").hide();
}
function showLoader(path){
	$("."+path+" .loader").show();
}

function confirmClick(oObject, sArtID, sCatID, iPrGroupID, dNewPrice){
	$("#dialog-changeSet").dialog({
		resizable: false,
		height:130,
		modal: true,
		buttons: {
			'Pakeisti': function() {
				$(this).dialog('close');
				changeCompPriceGroup(oObject, sArtID, sCatID, iPrGroupID, dNewPrice);
			},
			'Atšaukti': function() {
				$(this).dialog('close');
				
			}
		}
	});
}

function changeCompPriceGroup(oObject, sArtID, sCatID, iPrGroupID, dNewPrice)
{


	if ($('#comp_price_group').val() != iPrGroupID)
	{
		$('.comp_item').each(function(){
			$(this).val('');
		});
		$('.comp_prices').each(function(){
			$(this).val(0);
		});
		
		$("#comp_cats a").removeClass("selected");
		$('#comp_price_group').val(iPrGroupID);
		$('#main_comp_price').val(dNewPrice);

		var dTotalSum = 0.0;
		$('input.comp_prices').each(function(){
			dTotalSum += parseFloat($(this).val());
		});
		$('#comp_price span').html(addCurrCommas(dTotalSum.toFixed(2)));

		if (sLastActCat == '') sLastActCat = sCatID;
		if (sLastActArt == '') sLastActArt = sArtID;
		loadCompArticles(null, sLastActArt, sLastActCat);
		
		$("#compTabs a").removeClass("active").removeClass("prevTab");
		$(oObject).children("a").addClass("active");
		$(oObject).prev("li").children("a").addClass("prevTab");
		showLoader("articleBox");
	}
}

function loadCompMain(oObject, sArtID, sCatID)
{
	$('#comp_main').html('');
	showLoader("articleBox");
	sLastActArt = sArtID;
	sLastActCat = sCatID;
	$.post(
		selfLink,
		{
			cl: "ajax",
			fnc: "comp_main",
			artid: sArtID,
			catid: sCatID,
			prgroup: $('#comp_price_group').val()
		},
		function(data){
			$('#comp_main').html(data);
			hideLoader("articleBox");
		}
	);
}

function loadCompArticles(oObject, sArtID, sCatID)
{
	$('#comp_articles').html('');
	showLoader("articleBox");
	if (oObject != null)
	{
		$("#comp_cats a").removeClass("active");
		$(oObject).children("a").addClass("active");		
	}
	sLastActArt = sArtID;
	sLastActCat = sCatID;
	$.post(
		selfLink,
		{
			cl: "ajax",
			fnc: "comp_articles",
			artid: sArtID,
			catid: sCatID,
			prgroup: $('#comp_price_group').val()
		},
		function(data){
			$('#comp_articles').html(data);
			hideLoader("articleBox");
			actArt = $("#comp_item_"+sCatID).val();
			$("#rel-"+actArt).addClass("active");
		}
	);
}

function loadCompItems(oObject, sArtID, sCatID, sVendorID)
{
	$('#comp_items').html('');
	showLoader("articleBox");
	sLastActArt = sArtID;
	sLastActCat = sCatID;
	$.post(
		selfLink,
		{
			cl: "ajax",
			fnc: "comp_items",
			artid: sArtID,
			catid: sCatID,
			vendid: sVendorID,
			prgroup: $('#comp_price_group').val()
		},
		function(data){
			$('#comp_items').html(data);
			hideLoader("articleBox");
			actArt = $("#comp_item_"+sCatID).val();
			$("#rel-"+actArt).addClass("active");			
		}
	);
}

function setCompItem(oObject, sRootID, sPrice, sArtID)
{
	if ($(oObject).hasClass("active"))
	{
		$('#comp_item_'+sRootID).val("");
		$('#comp_state_'+sRootID).removeClass("selected");
		$('#comp_price_'+sRootID).val(0);
	}
	else
	{
		$('#comp_item_'+sRootID).val(sArtID);
		$('#comp_state_'+sRootID).addClass("selected");
		$('#comp_price_'+sRootID).val(sPrice);
	}
	dTotalSum = 0.0;
	$('input.comp_prices').each(function(){
		dTotalSum += parseFloat($(this).val());
	});
	$('#comp_price span').html(addCurrCommas(dTotalSum.toFixed(2)));
}

function addCurrCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function load_video(sVid){
	$.post(selfLink,
		{
			cl: "ajax",
			fnc: "load_video",
			vid: sVid
		},
		function(data){
			hideLoader("productPic");
			$("#videoBox").html(data);
		}
	);	
}

$(function(){
	$(".external").attr("target", "_blank");
	$(".oLayer").colorbox({width:"50%"});
	$('#compForm').submit(function(){return validateCompReq()});
	
	$('.slider').anythingSlider({
	    easing: "swing",        // Anything other than "linear" or "swing" requires the easing plugin
	    autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
	    delay: 5000,                    // How long between slide transitions in AutoPlay mode
	    startStopped: false,            // If autoPlay is on, this can force it to start stopped
	    animationTime: 600,             // How long the slide transition takes
	    hashTags: true,                 // Should links change the hashtag in the URL?
	    buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Go",                // Start text
	    stopText: "Stop"               // Stop text
	});
	
	
	var catInTimeout;
	var catOutTimeout;
	$("#slideCats a:not(#slideCats a.active)").hover(function(){
		curItem = $(this);
		clearTimeout(catOutTimeout);
		catInTimeout = setTimeout(function(){
			/*$("#slideCats span").animate({
				height: "18px"
			}, 100 );
			*/
			curItem.children("span").animate({
				height: "82px"
			}, 500 );
		}, 300);
	}, function(){
			clearTimeout(catInTimeout);
			catOutTimeout = setTimeout(function(){
				curItem.children("span").animate({
					height: "18px"
				}, 100);
			}, 500);
		
	
	});
	
	var viewportWidth = $(window).width();
	sliderSideWidth = (( viewportWidth - $("#slider li").outerWidth() )/2);
	
	$(".back a, .forward a").css({"opacity":"0.6"});
	$(".back, .forward").css({"width":sliderSideWidth+15});
	correctSliderPos = (sliderSideWidth)+10;
	$("#slider ul").css("left", correctSliderPos);
	
	
	//$("body").append("<div id='debug'>");
	//$("#debug").html( viewportWidth );
	
	$("#comp_items li").live("mouseenter", function(){
		$(this).addClass("over");
	});
	$("#comp_items li").live("mouseleave", function(){
		$(this).removeClass("over");
	});
	$("#comp_items li").live("click", function(){
		if ($(this).hasClass("active"))
		{
			$(this).removeClass("active");
		}
		else
		{
			$("#comp_items li").removeClass("active");
			$(this).addClass("active");
		}
	});
	
	$("#comp_vendors a").live("click", function(){
		$("#comp_vendors a").removeClass("active");
		$(this).addClass("active");
	});
	
	$("a.get2comp").click(function(){

		$('html').animate({
			scrollTop: $("#comp").offset().top
		}, 800);

	return false;
	});	
	
	$("#subPics a").click(function(){
		oLink = $(this);
		getLink = oLink.attr("href");
		
		if(oLink.hasClass("video")){
			getVid = oLink.attr("id").replace(/vid-/ig, "");
			$("#mediaBox").hide();
			$("#videoBox").html('').show();	
			load_video(getVid);
			showLoader("productPic");
			
		}else{
			mediaCont = $("#mediaBox");
			$("#videoBox").html('').hide();
			mediaCont.show();
			mediaCont.children("img").attr("src", getLink);	
		}

		return false;
	});
	

});

