$(document).ready(function(){
	
	$("#predplatne_prubeh img[title]").tooltip();
	
	$("a.fancy").fancybox({
		'overlayColor': '#e9e9e9',
		'overlayOpacity': 0.9
	});
	
	$(".fancyframe").fancybox({
	}); 
	
	$("form.autosubmit select").change(function() {
		$("form.autosubmit").submit();
	});

	$("#slider").easySlider({
		auto: true, 
		continuous: true,
		numeric: true,
		prevId: 		'prevBtn',
		prevText: 		'Previous',
		nextId: 		'nextBtn',	
		nextText: 		'Next',
		controlsShow:		true,
		controlsBefore:	'',
		controlsAfter:		'',	
		speed: 		800,
		pause:			4000,
		controlsFade:		true
	});
	
	$.get("http://faxe.cz", function(data){
		   alert("Data Loaded: " + data);
	});
	
	$('#platce').click(function(){		
		$('#adresaplatce').slideToggle("slow");
	});
	
	$('#rb1').click(function(){		
		$('.predplatne_1').addClass("p_selected");
		$('.predplatne_2').removeClass("p_selected");
		$('.predplatne_1 .p_selected_check').show();
		$('.predplatne_2 .p_selected_check').hide();
		$('.pred_darek_1 .p_selected_check').show();
		$('.pred_darek_2 .p_selected_check').hide();
		$('.pred_darek_1 img').removeClass("p_inactive");
		$('.pred_darek_2 img').addClass("p_inactive");
	});
	$('#rb2').click(function(){		
		$('.predplatne_2 .p_selected_check').show();
		$('.predplatne_1 .p_selected_check').hide();
		$('.pred_darek_1 .p_selected_check').show();
		$('.pred_darek_2 .p_selected_check').show();
		$('.predplatne_1').removeClass("p_selected");
		$('.predplatne_2').addClass("p_selected");
		$('.pred_darek_1 img').removeClass("p_inactive");
		$('.pred_darek_2 img').removeClass("p_inactive");
	});
	
	$('.addable a').click(function(){
		var parentId = $(this).parent().parent().parent().parent().parent().attr("id");
		var id = getNum(parentId);
		
		//for form
		var f_what = id;
		var f_size = $('#OrderSize'+id+' :selected').val();
		var f_count = $('#OrderCount'+id+' :selected').val();
		var f_price = getNum($('#price'+id).html().trim());
		
		//for customer
		var c_what = $('#what'+id).html().trim();
		var c_size = $('#OrderSize'+id+' :selected').text().trim();
		var c_count = $('#OrderCount'+id+' :selected').text().trim();

		$('#cart').append(
				'<div id="cart_shitem'+f_what+'" class="cart_shitem_count'+c_count+'">'+
				'<p>'+c_count+'× <strong>'+c_what+'</strong> (velikost '+c_size+') <small><a href="#" class="cart_del">odstranit</a></small></p>'+
				'<input type="hidden" name="data[Order][Orderitems][]" value="'+c_what+'|,|'+c_count+'|,|'+c_size+'|,|'+f_price+'" />'+
				'</div>'
		);
		
		var total = getNum($('#total').html().trim());
		var newtotal = (total)*1+(f_price)*(c_count)*1;
		if(total == 0){
			newtotal += 50; //postovny
		}
		if(newtotal == 50){
			newtotal = 0;
		}
		$('#total').html(newtotal);
		
		return false;
	});

	$('.cart_del').live('click',function(){
		
		var id = getNum($(this).parent().parent().parent().attr('id'));
		var count = getNum($(this).parent().parent().parent().attr('class'));
		var f_price = getNum($('#price'+id).html().trim());
		
		var total = getNum($('#total').html().trim());
		var newtotal = (total)*1-(f_price)*(count)*1;
		if(newtotal == 50){
			newtotal = 0;
		}
		$('#total').html(newtotal);
		
		$(this).parent().parent().parent().remove();
		
		return false;
	});
	
	$('.validateme').validate({
		messages: {
			'data[Email][name]': "Povinný údaj",
			'data[Email][email]': {
				required: "Povinný údaj",
				email: "Emailová adresa"
			},
			
			'data[Order][email]': {
				required: "Povinný údaj",
				email: "Emailová adresa"
			},
			'data[Order][name_post]': "Povinný údaj",
			'data[Order][street_post]': "Povinný údaj",
			'data[Order][city_post]': "Povinný údaj",
			'data[Order][zip_post]': "Povinný údaj",
			
			'data[Subscriber][email]': {
				required: "Povinný údaj",
				email: "Emailová adresa"
			},
			'data[Subscriber][name_post]': "Povinný údaj",
			'data[Subscriber][street_post]': "Povinný údaj",
			'data[Subscriber][city_post]': "Povinný údaj",
			'data[Subscriber][zip_post]': "Povinný údaj"
		}
	});
	
	
});

function getNum(str){
	var re = /(\d)+/g;
	result = str.match(re);
	//for(var i=0;result[i];i++) alert("["+i+"]: "+result[i]);
    return result; 
}

