function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.hover(function() {
    	carousel.options.smer = 'right';
        carousel.options.animation = 800;
        carousel.options.auto = 0.00001;
        carousel.startAuto();
    }, function() {
    	carousel.stopAuto();
    	carousel.options.auto = 0;
    });

    carousel.buttonPrev.hover(function() {
        carousel.options.smer = 'left';
        carousel.options.animation = 800;
        carousel.options.auto = 0.00001;
        carousel.startAuto();
    }, function() {
    	carousel.stopAuto();
    	carousel.options.auto = 0;
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.options.auto = 0;
    }, function() {
    	carousel.options.auto = 0;
    });
};


$(document).ready(function() { 
  
 ajx_form('#regmail','#box2_btm', true, false);
 ajx_form('#friendmail','#box3_btm', true, false);
 //ajx_cart('#interactive_set','#mk');

 $("#obj_form").validate();
 $("#reg_form").validate();
 $("#login_f").validate();
 $("#lostpass_form").validate();
 $("#ask_form").validate();
 
 $('#toplink').click(function(){
 	$.scrollTo( "#top", 600);
 });
 
  
 jQuery('#mycarousel').jcarousel({
    	visible: 7,
    	auto: 0,
        easing: 'linear',
        scroll: 1,
        animation: 800,
        initCallback: mycarousel_initCallback
 });
 
 
 
  		
 function ajx_form(fname, tid, rebind, modalMsg){
 	$(fname).submit(function() { 
   		$(this).ajaxSubmit({
 		target: tid,
 		success: function(){
 			if(modalMsg){
 		    	showMsg('msg',300,90,2000);
 		    }
 		    if(rebind){
 				ajx_form(fname, tid, rebind, modalMsg);
 			}
 		}
 	});
 	return false;});
 }
 
 function ajx_cart(fname, tid){
 	$(fname).submit(function() { 
   		var rel = $(this).attr("target");
   		$(this).ajaxSubmit({
 		url: "ajx_cart.php",
 		target: tid,
 		beforeSubmit: function(){tb_remove(false)},
 		success: function(){
 			document.getElementById(rel).value='';
 			showMsg('msg',400,35,2000);
 		}
 	});
 	return false;});
 }
  
 function showMsg(div,width,height,time){
 	tb_show('', '#TB_inline?reload=false&height='+height+'&width='+width+'&inlineId='+div+'&modal=true', null, true)
	setTimeout(function(){tb_remove(false)}, time);
 }
 
 function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
 }
 
 function switch2hash_forpsi(){
  var url = window.location.href;
  var hash = '#'+url.split("#")[1];
    
  if(hash!='#undefined'){    

    var url = str_replace(url,hash, '');
    var oldid = url.split("/")[6];
    var newurl = str_replace(url,oldid, '')+hash;
    var newurl = str_replace(newurl,'#', '');
    //var newurl = str_replace(newurl,'pohlednice/', 'foto');
    $("#foto").hide();
   
    window.location = newurl;
  }
 }
 
 function switch2hash(){
  var hash = window.location.hash;
  
  if(hash){
    
    var hash = str_replace(hash,'#', '');
    var url = window.location.href;
    var url = str_replace(url,hash, '');
    var oldid = url.split("/")[7];
    var newurl = str_replace(url,oldid, '')+hash;
    var newurl = str_replace(newurl,'pohlednice/', 'foto');
    
    $("#foto").hide();
   
    window.location = newurl;
  }
 }
 
 switch2hash_forpsi();
 
 
 
  
 interactive('typz');
 interactive('rozmer');
 interactive('paspartac');
 interactive('pasparta');
 interactive('laminace');
 interactive('hrana');
 interactive('ramovani');
 
 $('.slice').click(function(){
 	var relsl = $(this).attr("rel");
 	var oldsl = $("#ia_slices").val();
	
	$('.slice').removeClass('sactive');
	
 	if(oldsl==relsl || relsl=='0vh'){
 		$("#ia_slices").val('');
 		$("#foto").load("ajx_slice.php?slices=none",function(){
 			$("#cena").load("ajx_get_cena.php?kod=" + $("#ia_kod").val());
 		});
 		$("#foto").empty();
 		$("#sli0vh").addClass('sactive');
 	}
 	else{
 		$("#ia_slices").val(relsl);
 		$("#foto").load("ajx_slice.php?slices=" + $("#ia_slices").val(),function(){
 			$("#cena").load("ajx_get_cena.php?kod=" + $("#ia_kod").val());
 		});
 	}
 	
 	if($("#ia_slices").val()!='' || relsl=='0vh'){
 		$(this).addClass('sactive');
 	}
 	
 	//alert(relsl);
 });
 
 $('#dokosiku').click(function(){
 	$('#interactive_set').ajaxSubmit({
 	url: "ajx_cart.php",
 	target: '#mk',
 	success: function(){
 		showMsg('msg',400,35,2000);
 	}
 	});
  });
  
  
  $('.dokosiku').click(function(){
 	var rel = $(this).attr("rel");
 	$('#interactive_set'+rel).ajaxSubmit({
 	url: "ajx_cart.php",
 	target: '#mk',
 	success: function(){
 		showMsg('msg',400,35,2000);
 	}
 	});
  });
  
  
  $('#objednat').click(function(){
 	var rel = $(this).attr("rel");
 	
 	$('#interactive_set').ajaxSubmit({
 	url: "ajx_cart.php?a=objednat",
 	target: '#mk',
 	success: function(){
 		window.location = rel;
 	}
 	});
  });
  
  
  $('#mycarousel a img').mouseover(function(){
  	$(this).fadeTo(100,0.5,function(){
  		$(this).fadeTo(500,1);}
  	);
  });
  
  
  $('.c2v').click(function(){voter(this);});
      
  mainmenu();
  
 
 });
 //konec onload
  
 
 
 function voter(t){
 	var rel = $(t).attr("rel");
	var id = $(t).attr("id");
	$(".anketa").load("ajx_vote_anketa.php?id=" + rel + "&rep=" + id, {limit: 25}, function(){
		$('.c2v').click(function(){voter(this);});
	});
 }
 
 
 function mainmenu(){
$(".katmenu ul ").css({display: "none"}); // Opera Fix
$(".katmenu li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible"}).show(300);
		//$(document).find('.jcarousel-container:first').css({visibility: "hidden"});
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		//$(document).find('.jcarousel-container:first').css({visibility: "visible"});
		});
}
 

function interactive(cl){
 
 $('.ia_'+cl).click(function(){
 	var rel = $(this).attr("rel");
 	var t = this;
 	var old_typz = $("#ia_typz").val();
 	
 	$.ajax({
   	 type: "GET",
   	 url: "ajx_interactive.php?" + cl + "=" + rel,
   	 dataType: "xml",
   	 success: function(xml){ 
   	 
   	  $("data", xml).each(function(){
		var url = $("url", this).text();
		//alert(url);
		
		if(cl!='ramovani'){
		
			$(".ia_"+cl).css("background", "url('./images/ajx_radio.gif') left center no-repeat");
 			$(t).css("background", "url('./images/ajx_radio_sel.gif') left center no-repeat");
 		
 		}
 				
 		
 		$("#ia_"+cl).val(rel);
 		
 		//reakce na zmenu pasparty
 		if(cl=='pasparta' || cl=='paspartac'){
 			switch_foto($("#ia_kod").val(),false);
 			if(rel=='n'){
 				$(".ia_pasparta").css("background", "url('./images/ajx_radio.gif') left center no-repeat");
 				$("#ia_pasparta").val('');
 				$("#pasparta_sirka").hide();
 			}
 			
 			if((rel=='b' || rel=='w') && $("#ia_pasparta").val()==''){
 				$("#pas5").css("background", "url('./images/ajx_radio_sel.gif') left center no-repeat");
 				$("#ia_pasparta").val('5');
 				$("#pasparta_sirka").show();
 			}
 			
 			if((rel=='5' || rel=='10') && ($("#ia_paspartac").val()=='' || $("#ia_paspartac").val()=='n')){
 				$("#pasn").css("background", "url('./images/ajx_radio.gif') left center no-repeat");
 				$("#pasb").css("background", "url('./images/ajx_radio_sel.gif') left center no-repeat");
 				$("#ia_paspartac").val('b');
 			} 			
 		}
 		
 		
 		if(cl=='ramovani'){
 			$("#ia_ram").val(rel);
 			switch_ram();
 		}
 		
 		
 		if(cl=='ramovani'){
 			$("#ia_ram").val(rel);
 			switch_ram();
 		}
 		
 		
 		//reakce na zmenu typu zpracovani
 		if(cl=='typz'){
 		
 			 if((rel=='tisksl' || rel=='tiskra') && $("#ia_laminace").val()==''){
 			 	$("#lamm").css("background", "url('./images/ajx_radio_sel.gif') left center no-repeat");
 				$("#ia_laminace").val('m');
 				$("#laminace").show();
 			 }
 			 if((rel=='tisk' || rel=='tiskpl' || rel=='tiskcl' || rel=='tiskka' || rel=='tisku1' || rel=='tisku2' || rel=='tisku3' || rel=='tisku4' || rel=='tisku5')){
 				$(".ia_laminace").css("background", "url('./images/ajx_radio.gif') left center no-repeat");
 				$("#ia_laminace").val('');
 				$("#laminace").hide();
 			 }
 			 
 			 if((rel=='tisk' || rel=='tisksl' || rel=='tiskra' || rel=='tiskcl' || rel=='tiskka' || rel=='tisku1' || rel=='tisku2' || rel=='tisku3' || rel=='tisku4' || rel=='tisku5') && $("#ia_oldtypz").val()=='tiskpl'){
 			 	switch_foto($("#ia_kod").val(),false);
 			 }
 			 
 			 if((rel=='tisk' || rel=='tisksl' || rel=='tiskra' || rel=='tiskka' || rel=='tisku1' || rel=='tisku2' || rel=='tisku3' || rel=='tisku4' || rel=='tisku5') && ($("#ia_oldtypz").val()=='tiskcl')){
 			 	switch_foto($("#ia_kod").val(),false);
 			 }
 			 
 			 if(rel=='tiskpl'){
 			 	 			 	
 			 	if($("#ia_oldtypz").val()!='tiskpl'){
 			 		switch_foto($("#ia_kod").val(),false);
 				}
 			 	
 				if($("#ia_hrana").val()==''){
 					$("#hranao").css("background", "url('./images/ajx_radio_sel.gif') left center no-repeat");
 					$("#ia_hrana").val('o');
 				}
 				$("#hrana").show();
 			 } else {
 			 	$(".ia_hrana").css("background", "url('./images/ajx_radio.gif') left center no-repeat");
 				$("#ia_hrana").val('');
 				$("#hrana").hide();
 			 }
 			 
 			 
 			 if(rel=='tiskra'){ 			 
 			 	if($("#ia_ram").val()==''){
 					$("#ia_ram").val($("#ia_default_ram").val());
 				} 			 
 				switch_ram();
 				$("#ramovani").show();
 			 } else {
 			 	if(old_typz=='tiskra'){
 			 		$(".ram").css("background-image", "none");
 			 		$("#ia_ram").val('');
 			 	}
 			 	$("#ramovani").hide();
 			 }
 			 
 			 //if((rel=='tiskcl' && $("#ia_oldtypz").val()!='tiskka') || (rel=='tiskka' && $("#ia_oldtypz").val()!='tiskcl')){
 			 if(rel=='tiskcl' || $("#ia_oldtypz").val()!='tiskcl'){
 			 	//alert('Interaktivní nabídku pro toto provedení připravujeme. Tisk do Clip-rámu umožňuje pouze omezenou nabídku rozměrů. Více najdete na stránkách Materiály a provedení > V Clip-rámu');
 			 	$("#clipnfo").show();
 			 	switch_foto($("#ia_kod").val(),false);
 			 }
 			 
 			 if(rel!='tiskcl'){
 			 	$("#clipnfo").hide();
 			 }
 			 
 			 if(rel=='tiskra' || rel=='tiskcl'){
 			 	$("#foto").empty();
 			 	$('.slice').removeClass('sactive');
 			 	$("#sli0vh").addClass('sactive');
 			 	$("#ia_slices").val('');
 			 	$("#slices").hide();
 			 } else {
 			 	$("#slices").show();
 			 }
 			 
 			 $("#ia_oldtypz").val(rel);
 			 
 		}
 		
 		$("#cena").load("ajx_get_cena.php?kod=" + $("#ia_kod").val());
 	  });	
 		
 			   	   
   	  }
     });
     
 }); 
 
 } 



function switch_ram(){
    var kod_ramu = $("#ia_ram").val();

	$("#lh_ram").css("background-image", "url('./gallery/ramy/lh_ram" + kod_ramu + ".jpg')");
	$("#ld_ram").css("background-image", "url('./gallery/ramy/ld_ram" + kod_ramu + ".jpg')");
	$("#ph_ram").css("background-image", "url('./gallery/ramy/ph_ram" + kod_ramu + ".jpg')");
	$("#pd_ram").css("background-image", "url('./gallery/ramy/pd_ram" + kod_ramu + ".jpg')");
    $("#hh_ram").css("background-image", "url('./gallery/ramy/hh_ram" + kod_ramu + ".jpg')");
    $("#dh_ram").css("background-image", "url('./gallery/ramy/dh_ram" + kod_ramu + ".jpg')");
    $("#lv_ram").css("background-image", "url('./gallery/ramy/lv_ram" + kod_ramu + ".jpg')");
    $("#pv_ram").css("background-image", "url('./gallery/ramy/pv_ram" + kod_ramu + ".jpg')");
    
    $("#vybrany_ram").load("ajx_switch_ram.php?kod_ramu=" + kod_ramu);
    
}
 
 
 
function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}
 
 

function switch_foto(kod,value){
 	//var v = $(value).attr("title");
 	
 	$("#foto").fadeTo(200, 0);
 	 	
 	$.ajax({
   	 type: "GET",
   	 url: "ajx_switch_foto.php?kod=" + kod,
   	 dataType: "xml",
   	 success: function(xml){ 
		$("image", xml).each(function(){
			var url = $("url", this).text();
    		var tema = $("tema", this).text();
    		var nazev = $("nazev", this).text();
    		var autor = $("autor", this).text();
    		var pocitadlo = $("pocitadlo", this).text();
    		var reset_rozmer = $("reset_rozmer", this).text();
    		var nahled_width = $("nahled_width", this).text();
    		var nahled_height = $("nahled_height", this).text();
  		    		    		
    		setTimeout(function(){
    		    $("#foto").css("background", "url('" + url + "') no-repeat center center");
    		    $("#foto").css("width", nahled_width + "px");
    			$("#foto").css("height", nahled_height + "px");
    			
    			var rohw = (679-nahled_width)/2;
    			var rohh = (480-nahled_height)/2;    			
    			$(".roh").css("width", rohw + "px");
    			$(".roh").css("height", rohh + "px");
    			$("#hh_ram").css("width", nahled_width + "px");
    			$("#dh_ram").css("width", nahled_width + "px");
    			  		    
    		    $("#foto").fadeTo("fast", 1);
    		}, 500);    		
    		
    		
    		$(".carousel_foto").css("border", "1px solid #CACBCD");
    		$(value).css("border", "2px solid #D35500");
    		window.location.hash = kod;
    		$(".info").html('<strong>' + kod + '</strong> | ' + nazev + ' | ' + autor);
    		$(".pocitadlo").html(pocitadlo);
    		
    		//zapis do skryteho formulare
    		$("#ia_kod").val(kod);
    		if(reset_rozmer!=''){
    			$("#ia_rozmer").val(reset_rozmer);
    		}
    		
    		//aktualizace seznamu rozmeru
    		$("#rozmery").load("ajx_switch_foto.php?part=rozmery&kod=" + kod,function(){
    			 interactive('rozmer');
    		});
    		
    		$("#cena").load("ajx_get_cena.php?kod=" + kod);
    		
		});   	   
   	   }
     }); 
     
 	
}


function init_caru(start){
     $('#mycarousel2').jcarousel({
    	visible: 6,
    	auto: 0,
    	start: start,
        easing: 'linear',
        scroll: 6,
        animation: 800,
        itemLoadCallback: mycarousel_itemLoadCallback
 	});
 }

function mycarousel_itemLoadCallback(carousel, state)
{
    // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }
    
    jQuery.get(
        'ajx_carousel.php',
        {
            first: carousel.first,
            last: carousel.last
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
    // Set the size of the carousel
    carousel.size(parseInt(jQuery('total', xml).text()));

    jQuery('image', xml).each(function(i) {
    
    	var url = $("url", this).text();
    	var kod = $("kod", this).text();
    	var tema = $("tema", this).text();
    	    
        carousel.add(first + i, mycarousel_getItemHTML(url,kod,tema));
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url,kod,tema)
{
	return '<a href="javascript:;" onclick="switch_foto(\'' + kod + '\',this)" title="' + kod + '" onmouseover="blik(this)" style="background-image: url(\'' + url + '\'); background-position: center center; background-repeat: no-repeat" id="car' + kod + '" class="carousel_foto"></a>';
};



function blik(obj){

  	$(obj).fadeTo(100,0.5,function(){
  		$(obj).fadeTo(500,1);}
  	);
  
}




