﻿//	jQuery("#iframe-post-form").attr('src',jQuery("#iframe-post-form")[0].original);
jQuery(document).ready(function() {
	
	traverseFrameAfterLoad("#iframe-post-form");	
	var window_height=jQuery(window).height();
	jQuery('#floating_basket .inner ul').height(jQuery(window).height()-448)
	
	

    jQuery(window).scroll(
	function() {
		if(jQuery(window).scrollTop() < 250){
			var top_distance=300-jQuery(window).scrollTop() + "px";
		}else{
			var top_distance=50;
		}
		jQuery('#floating_basket').stop().animate(
			{ top: top_distance }, 
			{ queue: true, duration: 0, easing: 'linear' }
		);
		jQuery('#floating_basket .inner ul').stop().animate(
			{height : (
				jQuery(window).height()-
				(parseInt(jQuery('#floating_basket').css('top')) + 150)
			)},
			{ queue: true, duration: 0, easing: 'linear' }
		);
			//jQuery('#floating_basket').css('top', 300-jQuery(window).scrollTop() + "px");
	}
	);
	jQuery(window).scroll();
	//adaptBasketHeightAndPos();
	bindAddSubButtons();
});

function adaptBasketHeightAndPos() {
		if(jQuery(window).scrollTop() < 250){
			var top_distance=300-jQuery(window).scrollTop() + "px";
		}else{
			var top_distance=50;
		}
		jQuery('#floating_basket').stop().animate(
			{ top: top_distance }, 
			{ queue: true, duration: 0, easing: 'linear' }
		);
		jQuery('#floating_basket .inner ul').stop().animate(
			{height : (
				jQuery(window).height()-
				(parseInt(jQuery('#floating_basket').css('top')) + 150)
			)},
			{ queue: true, duration: 0, easing: 'linear' }
		);
			//jQuery('#floating_basket').css('top', 300-jQuery(window).scrollTop() + "px");
};
	
function traverseFrameAfterLoad(framka){
	var iframe = jQuery(framka);
	iframe.load(function (){
		if(jQuery("#iframe-post-form")[0].original==undefined){
			jQuery("#iframe-post-form")[0].original=jQuery("#iframe-post-form").attr('src');
		}
		traverseTheBasketSubpage(iframe);	
	});
}

function reactionToFormSubmit(selector){
    //alert(selector);
	var loader_target=jQuery(selector + ' .baskbut');
	loader_target.addClass('baskbutwait');
	activateBasketSpinner();
	increaseBasketBusyLevel();
}

function reactionToFormResult(selector){
	var loader_target=jQuery(selector + ' .baskbut');
	loader_target.removeClass('baskbutwait');
	//traverseTheBasketSubpage("#iframe-post-form");
	//alert(selector+'-postform-iframe');		
	decreaseBasketBusyLevel();
	var busy=getBasketBusyLevel();

	//Wyjatek dla strony glownej - bez koszyka
	if(jQuery('#floating_basket').length==0)
		alert("Produkt został dodany do koszyka");
	
	if(busy==0){
		jQuery("#iframe-post-form").attr('src',jQuery("#iframe-post-form")[0].original);
	}else{
		traverseTheBasketSubpage(selector+'-postform-iframe');	
	}
}
function bindAddSubButtons(){
	var lista=jQuery('.adbut');
	lista.each(function(index){
		jQuery(this).click(function(){
			addButton(this);
		});
	});
	lista=jQuery('.subbut');
	lista.each(function(index){
		jQuery(this).click(function(){
			subButton(this);
		});

	});
	
}

function addButton(object){
	var ilosc=jQuery(object).siblings().find('input').val();
	ilosc=parseInt(ilosc);
	ilosc=ilosc+1;
	jQuery(object).siblings().find('input').val(ilosc);
}
function subButton(object){
	var ilosc=jQuery(object).siblings().find('input').val();
	ilosc=parseInt(ilosc);
	ilosc=ilosc-1;
	if(ilosc<1){
		ilosc=1;
	}
	jQuery(object).siblings().find('input').val(ilosc);
}

function myappend(where, what){
	alert("22222"+what.html());
	if(jQuery.browser.msie){
		var selectContent = where.html();
		jQuery(where).html( what.html() + selectContent );
	} else {
		jQuery(where).append( what );
	}
}

function traverseTheBasketSubpage(framka){
	var lista=jQuery(framka).contents().find("tbody > tr > td.name");
	var ilosc_w_koszyku=lista.length;
	
	var koszyk=jQuery('#floating_basket');


	// // // // // // // // // 
	// // C A U T I O N // //
	// // // // // // // // 
	// Vital for IE support.
	//
	var htemplate;
		htemplate=koszyk.find('li.svr_product_template');
	phtemplate=htemplate.html();

	//alert("11111"+phtemplate);

	koszyk.find('ul').html('');
	koszyk.find('ul').append('<li class="svr_product_template"></li>');
	koszyk.find('ul li').html(phtemplate);
	//myappend(koszyk.find('ul'),htemplate)
	//alert("44444"+koszyk.find('ul').html());

	// // // // // // //
	
	
	lista.each(function(index){
//		alert(index + '/'+ ilosc_w_koszyku + ': ' + jQuery(this).text());
//		alert(index + '/'+ ilosc_w_koszyku + ': ' + jQuery(this).parent().contents().find(".actions > a").html());
		jQuery(this).parent().find('a')[0];//link do produktu
		var prod_name=jQuery(this).parent().find('a').html();//Nazwa produktu
		var prod_link=jQuery(this).parent().find('a').attr('href');//href produktu
		var variant=jQuery(this).parent().find('.variant').text();//opcje
		
		jQuery(this).parent().find('.quantity input')[0];//pole input z ilością
		var prod_amount_field=jQuery(this).parent().find('.quantity input');
		var prod_amount=jQuery(this).parent().find('.quantity input')[0].value;//zamówiona ilość
		var prod_price=jQuery(this).parent().find('.price').text();//cena jednostkowa
		var prod_sum=jQuery(this).parent().find('.sum').text();//suma
		//jQuery(this).parent().find('a.prodremove')[0];//link do usuwania produktu
		var prod_delete=jQuery(this).parent().find('a.prodremove').attr('href');//href do usuwania
		
		
		var koszyk=jQuery('#floating_basket');
		var template=koszyk.find('li.svr_product_template').html();
		//koszyk.find('ul').append('<li class="new"></li>');
		koszyk.find('ul').prepend('<li class="new"></li>');		
		koszyk.find('li.new').html(template);
		var nowa=koszyk.find('li.new');
		var len=lista.length;
		//kopiowanie linku
		nowa.find('.product_name').html(prod_name);
		nowa.find('.product_variants').html(variant);
		if(variant==""){
			nowa.find('.product_variants').hide();
		}
		nowa.find('.product_name').attr('href',prod_link);
		nowa.find('.amount').html(prod_amount);
		nowa.find('.number_of')[0].value=prod_amount;
		nowa.find('.price').html(prod_price);
		nowa.find('.sum').html(prod_sum);
		nowa.find('.remove').attr('href',prod_delete);
		
		nowa.find('.remove').click(function(){
			activateBasketSpinner();
		});
		
		
		nowa.find('.remove').attr('target','iframe-post-form');
		
		(function() {
			var localindex = index;
			nowa.find('.add').click(function(){
				addProductAmount(localindex);
			});
		})();

		(function() {
			var localindex = index;
			nowa.find('.sub').click(function(){
				subProductAmount(localindex);
			});
		})();
		
		(function() {
			var localindex = index;		
			nowa.find('.ok').click(function(){
				modifyProductAmount(index)
			});
		})();
		
		(function() {
			var localindex = index;
			nowa.find('.number_of').change(function(){
				changedProductAmount(localindex);
			});
		})();
		
		
		nowa.find('.svr_order_id').html(index+'');
		nowa.find('.svr_order_id').addClass('svr_order_id'+index+'');
		koszyk.find('li.new').removeClass('new').addClass('done');
		//nowa.find('.number_of').change();
	});
	
	var total_sum=jQuery(framka).contents().find(".recount .sum").text();
	koszyk.find('#suma .number').html(total_sum);
	
	var kwota_darmowej_dostawy=150;
	total_sum = (total_sum.replace(/[\szł]/g,''));
	total_sum = (total_sum.replace(/,/g,'.'));
	var brakuje_do_darmowej_dostawy=0;
	if(total_sum > kwota_darmowej_dostawy){
		brakuje_do_darmowej_dostawy=0;
	}else{
		brakuje_do_darmowej_dostawy=kwota_darmowej_dostawy-total_sum;
	}

	brakuje_do_darmowej_dostawy = brakuje_do_darmowej_dostawy.toFixed(2);
	brakuje_do_darmowej_dostawy = (brakuje_do_darmowej_dostawy.replace(/\./g,','));
	//brakuje_do_darmowej_dostawy = (brakuje_do_darmowej_dostawy.replace(/(,\d)\s/g,'$1 0'));	
	//brakuje_do_darmowej_dostawy = (brakuje_do_darmowej_dostawy.replace(/\s/g,''));		
	brakuje_do_darmowej_dostawy = brakuje_do_darmowej_dostawy + " zł";	
	koszyk.find('#do_dostawy .number').html(brakuje_do_darmowej_dostawy);
	//alert("W koszyku mamy: "+ ilosc_w_koszyku +" rodzajów produktów.");
	
	koszyk.find('#wyczysc_koszyk').attr('onclick','emptyTheBasket()');

	var busy=getBasketBusyLevel();
	if(busy==0){
		deactivateBasketSpinner();
	}
		
};

function getBasketBusyLevel(){
	var busy_level=parseInt(jQuery('.basket_load_indicator').html());
	if(isNaN(busy_level)) busy_level=0;
	return busy_level;
};

function setBasketBusyLevel(input){
	var busy_level=parseInt(input);
	if(NaN==busy_level) busy_level=0;
	if(busy_level==0) busy_level='';
	jQuery('.basket_load_indicator').html(busy_level);
	return busy_level;
};
function increaseBasketBusyLevel(){
	var busy_level=parseInt(getBasketBusyLevel());
	busy_level++;
	setBasketBusyLevel(busy_level);
}
function decreaseBasketBusyLevel(){
	var busy_level=parseInt(getBasketBusyLevel());
	busy_level--;
	setBasketBusyLevel(busy_level);
}


function activateBasketSpinner(){
	jQuery('.basket_load_indicator').addClass('active');
};

function deactivateBasketSpinner(){
	jQuery('.basket_load_indicator').removeClass('active');
};

function modifyProductAmount(index){
	activateBasketSpinner();
	var lista=jQuery("#iframe-post-form").contents().find("tbody > tr > td.name");
	//prod_name=jQuery(lista[index]).parent().find('a').html();//Nazwa produktu
	var prod_amount=jQuery(lista[index]).parent().find('.quantity input')[0].value;
	
	var koszyk=jQuery('#floating_basket li.done');
	var number_of=jQuery('.svr_order_id'+index+'').parent().find('.number_of');	
	var ilosc=number_of[0].value;
	
	if(ilosc==0){
		removeProduct(index);
		return 0;
	}
	
	jQuery(lista[index]).parent().find('.quantity input')[0].value=ilosc;
	jQuery("#iframe-post-form").contents().find(".innerbox form").submit()

};

function removeProduct(index){
	activateBasketSpinner();
	var koszyk=jQuery('#floating_basket li.done');
	var remove_link=jQuery('.svr_order_id'+index+'').parent().find('.remove');
	var adres=remove_link.attr('href');
	var iframe = jQuery("#iframe-post-form");
	iframe.attr('src',adres);
}
function addProductAmount(index){
	var koszyk=jQuery('#floating_basket li.done');
	var number_of=jQuery('.svr_order_id'+index+'').parent().find('.number_of');
	//var ilosc=jQuery(koszyk[index]).find('.number_of')[0].value;
	var ilosc=number_of[0].value;
	ilosc=parseInt(ilosc);
	ilosc=ilosc+1;
	number_of[0].value=ilosc;
	number_of.change();
}
function subProductAmount(index){
	var koszyk=jQuery('#floating_basket li.done');
	var number_of=jQuery('.svr_order_id'+index+'').parent().find('.number_of');
	var ilosc=number_of[0].value;
	ilosc=parseInt(ilosc);
	ilosc=ilosc-1;
	if (ilosc<0) {
		ilosc=0;
	}
	number_of[0].value=ilosc;
	number_of.change();
}
function changedProductAmount(index){
	var koszyk=jQuery('#floating_basket li.done');
	var number_of=jQuery('.svr_order_id'+index+'').parent().find('.number_of');
	var picked=number_of.val();
	var ordered=jQuery('.svr_order_id'+index+'').parent().find('.amount').text();
	//alert("picked:"+picked+"/ordered:"+ordered+"")
	if(picked==ordered){
		jQuery('.svr_order_id'+index+'').parent().find('.amount_controls *').hide();
	}else{
		jQuery('.svr_order_id'+index+'').parent().find('.amount_controls *').show();
	}
}
function emptyTheBasket(){
	activateBasketSpinner();
	setBasketBusyLevel(0);
	var koszyk=jQuery('#floating_basket li.done');
	koszyk.each(function(index){
		increaseBasketBusyLevel();
		var temp_frame='<iframe id="removerNumber'+index+'" style="display:none;" src="'+jQuery(this).find('.remove').attr('href')+'"></iframe>'
		jQuery(this).find('.svr_order_id').append(
			temp_frame
		);
		jQuery("#removerNumber"+index).load(function (){
			//traverseTheBasketSubpage("#removerNumber"+index);
			decreaseBasketBusyLevel();
			var busy=getBasketBusyLevel();
			if(busy==0){
				//iframe1 = jQuery("#removerNumber"+index);
				//data=iframe1.contents()[0].body.innerHTML
				
				//iframe2 = jQuery("#iframe-post-form");
				//iframe2.contents()[0].body.html(data);

				//alert(iframe[0].html());
				//iframe[0].contentWindow.location.reload(true);
				//traverseTheBasketSubpage(iframe2);
				jQuery("#iframe-post-form").attr('src',jQuery("#iframe-post-form")[0].original);
			}
		});
	});
	//setBasketBusyLevel(0);
	//iframe = jQuery("#iframe-post-form");
	//iframe[0].contentWindow.location.reload(true);
}
