
$(document).ready(function() {

	var base = $('base').attr('href');
	
	function goToUrl(loc) {
		loc = base + loc ;
		window.location.assign( loc );
	}


/* ext links
---------------------------------------------------------------------------- */
	$("a[rel*=ext]")
		.addClass("ext")
		.click( function() {
			 window.open( $(this).attr('href') ); 
			 return false;
		}) ;


/* emails
---------------------------------------------------------------------------- */
	$("a[rel=email]").each( function(){
		var e = $(this).text() ;
		e = e.replace('/','@');
		$(this).attr('href', 'mailto:' + e);
		$(this).text(e);
	});
		
/* lightboxes
---------------------------------------------------------------------------- */
	$("a[rel*=box]").fancybox({	
		'titlePosition'		: 'over',
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000000'
	});
	
/* form_values
---------------------------------------------------------------------------- */
function set_form_values(form_values) {

//volani fce
//	set_form_values({"":""<?= $form_values; ?>}) ;

	$('input[type="text"], select').each(function() {
		fld_name = $(this).attr('name') ;
		if (form_values[fld_name])
			$(this).val(form_values[fld_name]) ;
	}) ;
	$('textarea').each(function() {
		fld_name = $(this).attr('name') ;
		if (form_values[fld_name])
			$(this).html(form_values[fld_name]) ;
	}) ;
	$('input[type="checkbox"]').each(function() {
		fld_name = $(this).attr('name') ;
		if (form_values[fld_name] && form_values[fld_name] != "0")
			$(this).attr('checked', 'checked') ;
	}) ;
	$('input[type="radio"]').each(function() {
		fld_name = $(this).attr('name') ;
		if ($(this).val() == form_values[fld_name])
			$(this).attr('checked', 'checked') ;
	}) ;
	
}
	
/* ajax prepocitat/kosik
---------------------------------------------------------------------------- */

	$('#kosik_mini').click( function() {
		goToUrl('shop/kosik') ;
	}) ;
	
	
	$('.koupit').submit(function() {
		var formdata = $(this).serialize() ;
		var btn_do_kosiku = $('.btn_do_kosiku', this) ;
		
		btn_do_kosiku.attr('src', '_images/preloader.gif');
		//alert(formdata);
		$.ajax({
			url: '_modul/shop/prepocitat',
			data: formdata,
			dataType: 'json',
			success: function(json) {
				$('#kosik_mini_sum').html(json.obj_sum).format({format:"#,###.00 Kč", locale:"cz"});
				$('#kosik_mini_sum_dph').html(json.obj_sum_dph).format({format:"#,###.00 Kč s DPH", locale:"cz"});
				btn_do_kosiku.attr('src', '_images/true.png') ;
			},
			error: function(data) {
				alert('ajax error');
			}
		});
		return false ;
	});

/* saul reference
---------------------------------------------------------------------------- */
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

$('#reference li span').hover(
	function () {
	/*
		$('#reference .detail').hide();
		$('.detail').clearQueue().hide();
		$(this).parent().find('.detail').delay(200).show('slow');
	*/
		$(this).parent().find('.detail').css('display', 'block').center();;
		//
	
	} ,
	function () {
	/*
		$(this).parent().find('.detail').clearQueue().hide();
		$('#reference .detail').hide();
		$(this).parent().find('.detail').css('display', 'none');
	*/
		$('.detail').css('display', 'none');
	}
);

	
/* formular
---------------------------------------------------------------------------- */

	
	$('#napiste').ajaxForm({
    	dataType:  'json', 	
        success:   function(json) {
        	if (json.msg.msg_type == 'error') {
        		$('#msg')
					.text(json.msg.msg_text)
					.removeClass('msg_type_success')
					.addClass('msg_type_error')
					.slideDown("fast")
					.delay(5000)
					.slideUp("fast");
			} else {
			
        		$('#msg')
					.text(json.msg.msg_text)
					.removeClass('msg_type_error')
					.addClass('msg_type_success')
					.slideDown("fast")
					.delay(5000)
					.slideUp("fast");
        		
        		$('#napiste').clearForm();
			}
		} 
	}); 
}); // jquery
