// JavaScript stuff for eshop
/*
** vlozi produkt do kosiku
** id = id produktu
** price = cena produktu
** type = elektricky (==2) / fyzicky (==1)
** quan - quantity = pocet produktu
*/
function add_product( id, type, quan ){
	$.ajax({
	  url: window.HTTP_HOST+"/eshop/kosik/pridat",
	  type: "POST",
	  data: "id="+id+"&type="+type+"&quant="+quan,
	  success: function( data ){
			load_basket_string();
			$("#msg"+id).fadeIn().html( data ).delay(6000).fadeOut();
          }
	});
}

function basket_del( id ) {
	if( confirm( "Opravdu odebrat položku?" ) ) {
		$.ajax({
		  url: window.HTTP_HOST+"/eshop/kosik/smazat",
		  type: "POST",
		  data: "id="+id,
		  success: function( data ){
				load_basket_string();
				$("#row"+id).fadeOut();
				$("#count_fyz"+id).val(0);
				$("#count_ele"+id).attr("checked", "");
				eshop_recount_price( id, 0 );
				//count_price_global();
			  }
		});
	}
}

function add_product_form( id ) {
	if( $('#fyz').val() > 0 ) {
		add_product( id, 1, $('#fyz').val() );
	}
	if( $('#ele').attr('checked') == true ){
		add_product( id, 2, 1 );
	}
	
}

function set_product( id, type, quant ){
	$.ajax({
	  url: window.HTTP_HOST+"/eshop/kosik/upravit",
	  type: "POST",
	  data: "id="+id+"&type="+type+"&quant="+quant
	});
	
	load_basket_string();
}

function load_basket_string() {
	$.ajax({
	  url: window.HTTP_HOST+"/eshop/kosik/text",
	  success: function( data ){
		$("#kosik-content").html( data );  
	  }
	});
}

function eshop_recount_price( item_id, clicked ){
	var price		= $('#price'+item_id).html();
	var price_ele	= $('#price_ele'+item_id).html();
	var count_fyz	= $('#count_fyz'+item_id).val();
	var count_ele	= $('#count_ele'+item_id).attr('checked');
	
	var price_all	= 0;
	
	if( count_ele == true ){
	// objednava elektricky?
		
		price_all += parseInt(price_ele);
		
		$("#yesno"+item_id).html( "Ano" );
		
		set_product( item_id, 2, 1 );
	
	}
	
	if( count_ele == false ) {
	// neobjednava
		$("#yesno"+item_id).html( "Ne" );
		
		set_product( item_id, 2, 0 );
	
	}
	
	if( count_fyz > 0 ){
		price_all += parseInt(price) * parseInt(count_fyz);
	}
	
	set_product( item_id, 1, count_fyz );
	
	// cena nosicu vcetne updatu v kosiku
	$("#price_all"+item_id).html( price_all );
	
	count_price_products();
}


function count_price_global(){
	var global_price = parseInt( $("#price-sum").html() );
	
	var delivery	= $('#delv option:selected').attr("title");
	var payment		= $('#paym option:selected').attr("title");
	
	global_price	+= parseInt( delivery ) + parseInt( payment );
	
	$("#price-all-sum").html( global_price );
	$("#price-all-sum-inp").val( global_price );

}

function count_price_products() {
	var global_price	= 0;
	
	for( x = 0; x < $(".price_all").length; x++ ){
		global_price	+= parseInt( $(".price_all")[x].innerHTML );
	}
	
	$("#price-sum").html( global_price );
	
	validate_delivery();
	count_price_global();
}

function validate_delivery( input ){
	
	if( count_item_fyz() == 0 ) {
		$("#delv").attr("selectedIndex", 0);
		
		if( input == 1 )
			alert( "Když alba stahujete, není potřeba vybírat způsob doručení." );
		
	}else if( $("#delv").attr("selectedIndex") == 0 ) {
		$("#delv").attr("selectedIndex", 1);
		
		if( input == 1 )
			alert( "Při objednání nosičů je nutné vybrat nějaký způsob doručení." );
	}
	
}

function count_item_fyz() {
	var nosic = 0;
		
	for( x = 0; x < $(".count_fyz").length; x++ ){
		if( $(".count_fyz")[x].value == "" )
			nosic += parseInt( 0 );
		else
			nosic += parseInt( $(".count_fyz")[x].value );
	}
	
	return nosic;	
}

function play_selected(){
	url = window.HTTP_HOST+"/prehravac";
	for( x = 0; x < $(".play_alb:checked").length; x++ ){
		 url = url+"/"+$(".play_alb:checked")[x].value;
	}
	
	if( x == 0 )
		alert("Musíte vybrat nějaká alba!");
	else
		window.open( url , "player", "width=280,height=410");
		//window.location.href = url;
}











