// JavaScript Document
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        //carousel.startAuto(0); //stop autoscroll
    });

    carousel.buttonPrev.bind('click', function() {
        //carousel.startAuto(0); //stop autoscroll
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

$(document).ready(function(){
        $("a[rel='example1']").colorbox({transition:"fade"});
        $(".colorbox").colorbox({iframe:true, width:500, height:500});
		$(".doporucit").colorbox({iframe:true, width:520, height:670});
		$(".dotaz").colorbox({iframe:true, width:520, height:540});
		
		// Datepicker
		$('#datepicker').datepicker({
			dateFormat: "dd/mm/yy",
			inline: true
		});
		
		$('.home_middle_right_novinky').jCarouselLite({  
			vertical: true,
			hoverPause:true,
			visible: 1,
			auto:3000,
			speed:1000
		});		
		
		$('.carusel').jcarousel({
			scroll: 1,
			auto: 5,
	        wrap: 'last',
			initCallback: mycarousel_initCallback
		});
		
		//onclick reset input value
		$('.form_input').focus(
			function() {
				if (this.value == this.defaultValue) {
					this.value = '';
				}
			}
			);
		$('.form_input').blur(
			function() {
				if (this.value == '') {
					this.value = this.defaultValue;
				}
			}
		);
		
		$('#dropdown > li').hover(
			function(){
				//alert($(window).width()+" \ "+($(this).offset().left + 312));
				if((parseInt($(window).width())) < (parseInt($(this).offset().left + 310))){
					//alert('moooooc');
					/*$(this).find('ul').removeClass('to_right');	
					$(this).find('ul').addClass('to_left');*/
					$(this).find('ul').css({
						left: "",
						right: "0"
					});
				}
				else{
					/*$(this).find('ul').removeClass('to_left');	
					$(this).find('ul').addClass('to_right');*/
					$(this).find('ul').css({
						right: "",
						left: "0"
					});
				}
				$(this).find('ul').show();
				$(this).find('a:eq(0)').addClass('activated');												
			},
			function(){
				$(this).find('ul').hide();
				$(this).find('a:eq(0)').removeClass('activated');	
			}
		);
		
		$('#dropdown2 > li').hover(
			function(){
				//alert($(window).width()+" \ "+($(this).offset().left + 312));
				if((parseInt($(window).width())) < (parseInt($(this).offset().left + 362))){
					//alert('moooooc');
					/*$(this).find('ul').removeClass('to_right');	
					$(this).find('ul').addClass('to_left');*/
					$(this).find('ul').css({
						left: "",
						right: "0"
					});
				}
				else{
					/*$(this).find('ul').removeClass('to_left');	
					$(this).find('ul').addClass('to_right');*/
					$(this).find('ul').css({
						right: "",
						left: "0"
					});
				}
				$(this).find('ul').show();
				$(this).find('a:eq(0)').addClass('activated');												
			},
			function(){
				$(this).find('ul').hide();
				$(this).find('a:eq(0)').removeClass('activated');	
			}
		);
		
		$('#druh').selectbox();
		$('#model').selectbox();
		$('#palivo').selectbox();
		$('#cena').selectbox();
		$('#kategorie').selectbox();
		$('#serazeni').selectbox();
		$('#vybava').selectbox();

		$(".skladove_prev").live('click', function(event) {
			event.preventDefault(); //prevent synchronous loading
			var src = $(".skladove_prev").attr("href");
			$("#home_skladove").empty().html('<img src="/images/loading.gif" width="50" height="50" class="sklad_loading" />');
			setTimeout(function(){
				  // do something
	  			$('#home_skladove').load(src);
			},1000);
		});
		
		$(".skladove_next").live('click', function(event) {
			event.preventDefault(); //prevent synchronous loading
			var src = $(".skladove_next").attr("href");
			$("#home_skladove").empty().html('<img src="/images/loading.gif" width="50" height="50" class="sklad_loading" />');
			setTimeout(function(){
				  // do something
	  			$('#home_skladove').load(src);
			},1000);
			//alert($(".skladove_next").attr("href"));
		});
		
		$(".aktualni_prev").live('click', function(event) {
			event.preventDefault(); //prevent synchronous loading
			var src = $(".aktualni_prev").attr("href");
			$("#aktualni_nabidka").empty().html('<img src="/images/loading.gif" width="50" height="50" class="sklad_loading" />');
			setTimeout(function(){
				  // do something
	  			$('#aktualni_nabidka').load(src);
			},1000);
		});
		
		$(".aktualni_next").live('click', function(event) {
			event.preventDefault(); //prevent synchronous loading
			var src = $(".aktualni_next").attr("href");
			$("#aktualni_nabidka").empty().html('<img src="/images/loading.gif" width="50" height="50" class="sklad_loading" />');
			setTimeout(function(){
				  // do something
	  			$('#aktualni_nabidka').load(src);
			},1000);
			//alert($(".skladove_next").attr("href"));
		});
		
		$("#druh_container > ul > li").live('click', function(event) {
				var druh     = $('#druh').attr('value');
				//alert(tires_id_cat+' | '+f_width);
			$("#reload_models").empty().html('<img src="/images/loading_2.gif" width="160" height="22" />');
			setTimeout(function(){
				  // do something
	  			$('#reload_models').load('/ajax_models.php?druh='+druh);
			},100);				
		});	
		
		$(".selectbox-wrapper > ul > li").live('click', function(event) {
			var druh = $('#druh').attr('value');
			if(!druh){
				druh = "";
			}
			var model = $('#model').attr('value');
			if(!model){
				model = "";
			}
			var palivo = $('#palivo').attr('value');
			if(!palivo){
				palivo = "";
			}
			var cena = $('#cena').attr('value');
			if(!cena){
				cena = "";
			}
			var kategorie = $('#kategorie').attr('value');
			if(!kategorie){
				kategorie = "";
			}
			var serazeni = $('#serazeni').attr('value');
			if(!serazeni){
				serazeni = "";
			}
			var vybava = $('#vybava').attr('value');
			if(!vybava){
				vybava = "";
			}
			$("#reload_count").empty().html('-');
			$('#reload_count').load('/ajax_filter_count.php?druh='+druh+'&model='+model+'&palivo='+palivo+'&cena='+cena+'&kategorie='+kategorie+'&serazeni='+serazeni+'&vybava='+vybava);
		});
		
	var InfiniteRotator =
    {
        init: function()
        {
            //initial fade-in time (in milliseconds)
            var initialFadeIn = 1000;
 
            //interval between items (in milliseconds)
            var itemInterval = 10000;
 
            //cross-fade time (in milliseconds)
            var fadeTime = 2500;
 
            //count number of items
            var numberOfItems = $('.rotating-item').length;
 
            //set current item
            var currentItem = 0;
 
            //show first item
            $('.rotating-item').eq(currentItem).fadeIn(initialFadeIn);
 
            //loop through the items
            var infiniteLoop = setInterval(function(){
                $('.rotating-item').eq(currentItem).fadeOut(fadeTime);
 
                if(currentItem == numberOfItems -1){
                    currentItem = 0;
                }else{
                    currentItem++;
                }
                $('.rotating-item').eq(currentItem).fadeIn(fadeTime);
 
            }, itemInterval);
        }
    };
 
    InfiniteRotator.init();		
		
				$(".print").click(function(event){
					event.preventDefault();
					window.print();
			   	});
				
	$('#slideshowHolder').jqFancyTransitions({
		effect: 'zipper', // wave, zipper, curtain
		width: 950, // width of panel
		height: 380, // height of panel
		strips: 20, // number of strips
		delay: 6000, // delay between images in ms
		stripDelay: 50, // delay beetwen strips in ms
		titleOpacity: 0.7, // opacity of title
		titleSpeed: 800, // speed of title appereance in ms
		navigation: true, // prev and next navigation buttons
		links: true // show images as links
	});
});	

	  function initialize(lat,lng) {
		var latlng = new google.maps.LatLng(lat,lng);
		var myOptions = {
		  zoom: 14,
		  scrollwheel: false,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		var marker = new google.maps.Marker({
			  position: latlng, 
			  map: map
		});
	  }		


