// hlavni menu
$(document).ready(function(){
	$("ul#horni-menu li.hlavni").hover(
		function() {
			$("a", this).addClass("vybr");
			$("ul", this).show();
		},
		function() {
			$("a", this).removeClass("vybr");
			$("ul", this).hide();
		}
	);
});


// slider - rok výroby
$(document).ready(function(){

	var min_stari = parseInt( $('#stari_min').val() , 10 ) ;
	var max_stari = parseInt( $('#stari_max').val() , 10 ) ;
	var step = 1;

	//alert( min_stari + ' - ' + max_stari );

	$("#slider-stari").slider({
		range: true ,
		values: [min_stari, max_stari],
		min: min_stari ,
		max: max_stari ,
		step: step ,
		slide: function(event, ui) {
                        label_stari( ui.values[0] , ui.values[1] );                            			
		}
	});

	var stari_od = $('#stari_od').val();
	var stari_do = $('#stari_do').val();

	if( stari_od ) {
	    $("#slider-stari").slider( "values" , 0 , stari_od ) ;
	} else {
	    stari_od = min_stari;
	}

	if( stari_do ) {
	    $("#slider-stari").slider( "values" , 1 , stari_do )
	} else {
	    stari_do = max_stari ;
	}

        label_stari( stari_od , stari_do  );

	$("#slider-stari").bind( "slidechange", function( event , ui ) {

                var val = $(this).slider('values', 0);                
                var val2 = $(this).slider('values', 1);
                
                if( stari_od != val || stari_do != val2 ){
                    
                    $('#stari_od').val(val) ;
                    $('#stari_do').val(val2) ;
                    document.vyber.submit();

                }
	});

});

function label_stari( stari_od , stari_do ){
        if( stari_od == stari_do ){
            $("#stariAmount").text( stari_od + ' let' );
        } else {
            $("#stariAmount").text( stari_od + ' - ' + stari_do + ' let' );
        }
}


// slider - cena vozu
$(document).ready(function(){

	var min_cena = parseInt( $('#cena_min').val() , 10 ) ;

	if( min_cena == 100000 && !cena_od  ){
	    min_cena = 0;
	}

	var max_cena = parseInt( $('#cena_max').val() , 10 ) ;	
	var step = 50000;
	if( ( max_cena - min_cena ) < 500000 ){
	    step = 10000;
	}

	$("#slider-cena").slider({
		range: true,
		values: [min_cena, max_cena],
		min: min_cena,
		max: max_cena,
		step: step,
		slide: function(event, ui) {
                        label_cena( ui.values[0] , ui.values[1] );
		}
	});
	
	var cena_od = $('#cena_od').val();
	var cena = $('#cena').val();

	if( cena_od ) {
	    $("#slider-cena").slider( "values" , 0 , cena_od ) ;
	} else {
	    cena_od = min_cena;
	}

	if( cena ) {
	    $("#slider-cena").slider( "values" , 1 , cena )
	} else {
	    cena = max_cena;
	}

        label_cena( cena_od , cena );

	$("#slider-cena").bind( "slidechange", function(event, ui) {

		var val = $(this).slider('values', 0);	
		var val2 = $(this).slider('values', 1);
		
                if( cena_od != val || cena != val2 ){
                    
                    $('#cena_od').val(val) ;
                    $('#cena').val(val2) ;
                    document.vyber.submit();

                }

	});

});

function label_cena( cena_od , cena_do ){
        if( cena_od == cena_do ){
            $("#cenaAmount").text( number_format( cena_od , 0, '.' , ' ' ) + ' Kč');
        } else {
            $("#cenaAmount").text( number_format( cena_od , 0, '.', ' ')  + ' - ' + number_format( cena_do , 0, '.' , ' ' ) + ' Kč');
        }
}





// slider - cena vozu
$(document).ready(function(){

	var min_najeto = parseInt( $('#najeto_min').val() , 10 ) ;

	if( min_najeto == 100000 && !najeto_od  ){
	    min_najeto = 0;
	}

	var max_najeto = parseInt( $('#najeto_max').val() , 10 ) ;
	var step = 10000;
//	if( ( max_najeto - min_najeto ) < 500000 ){
//	    step = 10000;
//	}

	$("#slider-najeto").slider({
		range: true,
		values: [min_najeto, max_najeto],
		min: min_najeto,
		max: max_najeto,
		step: step,
		slide: function(event, ui) {
                        label_najeto( ui.values[0] , ui.values[1] );
		}
	});

	var najeto_od = $('#najeto_od').val();
	var najeto_do = $('#najeto_do').val();

	if( najeto_od ) {
	    $("#slider-najeto").slider( "values" , 0 , najeto_od ) ;
	} else {
	    najeto_od = min_najeto;
	}

	if( najeto_do ) {
	    $("#slider-najeto").slider( "values" , 1 , najeto_do )
	} else {
	    najeto_do = max_najeto;
	}

    label_najeto( najeto_od , najeto_do );

	$("#slider-najeto").bind( "slidechange", function(event, ui) {

		var val = $(this).slider('values', 0);
		var val2 = $(this).slider('values', 1);

                if( najeto_od != val || najeto_do != val2 ){

                    $('#najeto_od').val(val) ;
                    $('#najeto_do').val(val2) ;
                    document.vyber.submit();

                }

	});

});

function label_najeto( najeto_od , najeto_do ){
        if( najeto_od == najeto_do ){
            $("#najetoAmount").text( number_format( najeto_od , 0, '.' , ' ' ) + ' Km');
        } else {
            $("#najetoAmount").text( number_format( najeto_od , 0, '.', ' ')  + ' - ' + number_format( najeto_do , 0, '.' , ' ' ) + ' Km');
        }
}


// menu nastaveni zobrazeni
$(document).ready(function(){
	$("ul#razeni-nastavit li.hlavni").hover(
		function() {$("ul", this).show();},
		function() {$("ul", this).hide();}
	);
});

// menu sdileni
$(document).ready(function(){
	$("ul#sdilet li.hlavni").hover(
		function() {$("ul", this).show();},
		function() {$("ul", this).hide();}
	);
});



// exkluzivni vozy
function formatText(index, panel) {
	return "<span></span>";
};
$(function () {
	$('#exklusivni-vozy').anythingSlider({
		easing: "swing",
		autoPlay: true,
		delay: 3000,
		startStopped: false,
		animationTime: 0,
		hashTags: false,
		buildNavigation: true,
		pauseOnHover: true,
		startText: "Go",
		stopText: "Stop",
		navigationFormatter: formatText
	});
});
// pretaceni po najeti mysi
$(document).ready(function() {
	$("#exklusivni-vozy .thumbNav li.link_1 span").hover(function() {$("#exklusivni-vozy").anythingSlider(1);});
	$("#exklusivni-vozy .thumbNav li.link_1 span").click(function() {window.open('http://www.lexus.cz');});

	$("#exklusivni-vozy .thumbNav li.link_2 span").hover(function() {$("#exklusivni-vozy").anythingSlider(2);});
	$("#exklusivni-vozy .thumbNav li.link_2 span").click(function() {window.open('http://www.infiniti.cz');});

	$("#exklusivni-vozy .thumbNav li.link_3 span").hover(function() {$("#exklusivni-vozy").anythingSlider(3);});
	$("#exklusivni-vozy .thumbNav li.link_3 span").click(function() {window.open('http://www.audi.cz/cz/brand/cs.html');});

	$("#exklusivni-vozy .thumbNav li.link_4 span").hover(function() {$("#exklusivni-vozy").anythingSlider(4);});
	$("#exklusivni-vozy .thumbNav li.link_4 span").click(function() {window.open('http://www.porsche.cz');});

	$("#exklusivni-vozy .thumbNav li.link_5 span").hover(function() {$("#exklusivni-vozy").anythingSlider(5);});
	$("#exklusivni-vozy .thumbNav li.link_5 span").click(function() {window.open('http://www.bmw.cz/cz/cs/');});

	$("#exklusivni-vozy .thumbNav li.link_6 span").hover(function() {$("#exklusivni-vozy").anythingSlider(6);});
	$("#exklusivni-vozy .thumbNav li.link_6 span").click(function() {window.open('http://www.mercedes-benz.cz/content/czechia/mpc/mpc_czechia_website/czng/home_mpc/passengercars.html');});
});


// slider selfpromo
$(function () {
	$('#selfpromo').anythingSlider({
		easing: "swing",
		autoPlay: true,
		delay: 6000,
		startStopped: false,
		animationTime: 300,
		hashTags: false,
		buildNavigation: true,
		pauseOnHover: true,
		startText: "Go",
		stopText: "Stop"
	});
});


// statistika AS
$(document).ready(function() {

    if( typeof( ads_view ) == "undefined" ){

        return true;

    } else {

        SaveAdsView( ads_view );
    }
    
})

function SaveAdsView( ads_view ){

    if( typeof( ads_view ) == "undefined" ){

        return true;

    } else {
        
        $.ajax({
            type:           'post',
            cache:          false,
            url: "/scripts/automodul/statad.php?pv_id=" + pv_id,
            data:           JSONstringify( ads_view ),
            processData:    false,
            contentType:   'application/json'
       });

   }

}

function PromoStatClick( pv_id , promo_position_id , serie_id , auto_id ){

        $.ajax({
            cache:          false,
            url: "/scripts/automodul/statpromo.php?pv_id="+ pv_id +"&promo_position_id=" + promo_position_id + "&serie_id=" + serie_id + "&auto_id=" + auto_id,
            processData:    false
       });

       return true;

}

function AdStatClick( id , position_id , pv_id , obj ){
        $.ajax({
            cache:          false,
            url: "/scripts/automodul/statadclick.php?id="+ id +"&position_id=" + position_id + "&url=" + escape(obj)+ "&pv_id=" + pv_id  ,
            processData:    false,
            async:          true
       });

       return true;

}




function DisplayDeaKontaktClick( pv_id , car_id ){

        $.ajax({
            cache:          false,
            url: "/scripts/automodul/statkontakt.php?pv_id="+ pv_id +"&car_id=" + car_id ,
            processData:    false,
            async:          true
       });

       return true;

}



// implementace JSON.stringify serialization
 function JSONstringify (obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
        // simple data type
        if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    }
    else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n];t = typeof(v);
            if (t == "string") v = '"'+v+'"';
            else if (t == "object" && v !== null) v = JSONstringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
}

//formátování čísel na straně .js
function number_format (number, decimals, dec_point, thousands_sep) {

    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }   
    return s;    
}

/******************************/

$(document).ready(function(){
	$('a[rel=lightbox]').lightBox();
	$('a[rel=pf]').lightBox();
	$(".validForm").formValidation();
	//$('marquee').marquee('mymarquee');
});

ScrollSpeed = 150;
ScrollChars = 0;

function SetupTicker() {
	window.status = "Chcete s námi komunikovat, není Vám něco jasné  - volejte: 800 100 550 - pište: info@autosystem.cz             ";
}

function RunTicker() {
	window.setTimeout('RunTicker()',ScrollSpeed);
	window.status = msg;
	msg = msg.substring(ScrollChars) + msg.substring(0,ScrollChars);
}
SetupTicker();

window.status = "Chcete s námi komunikovat, není Vám něco jasné - volejte: 800 100 550 - pište: info@autosystem.cz";

/********************************/

var countDeaKontaktClicked = null;

function CountDeaKontaktClick(pvId,autId){

	if( countDeaKontaktClicked == true ){
		return
	} else {            
		DisplayDeaKontaktClick(pvId,autId);
		countDeaKontaktClicked = true;
	}
}

function DisplayPoptavka(){
	$('#popt').css('display','block');
	$('#kont').css('display','none');
	$('#tab-popt').addClass('selected');
	$('#tab-kont').removeClass('selected');
	window.location.hash = 'poptavka'; 
	return;
}

function DisplayDotaz(){
	$('#popt').css('display','none');
	$('#kont').css('display','block');
	$('#tab-popt').removeClass('selected');
	$('#tab-kont').addClass('selected');
	window.location.hash = 'kontakt';
	return;
}

$(document).ready(function(){
	if(window.location.hash) {                                                
		var hash = window.location.hash.substring(1); 
		if(hash == 'kontakt'){
			DisplayDotaz();
		}                        
	}
});	

function udaje() {
	t = '';
	t += 'firma [<strong>' + document.getElementById('firma').value + '</strong>]; ';
	t += 'jméno [<strong>' + document.getElementById('jmeno').value + '</strong>]; ';
	t += 'příjmení [<strong>' + document.getElementById('prijmeni').value + '</strong>]; ';
	t += 'ulice [<strong>' + document.getElementById('ulice').value + '</strong>]; ';
	t += 'čp [<strong>' + document.getElementById('cp').value + '</strong>]; ';
	t += 'obec [<strong>' + document.getElementById('obec').value + '</strong>]; ';
	t += 'psč [<strong>' + document.getElementById('psc').value + '</strong>]; ';
	t += 'telefon [<strong>' + document.getElementById('telefon').value + '</strong>]; ';
	t += 'email [<strong>' + document.getElementById('email').value + '</strong>]; ';
	t += 'ič [<strong>' + document.getElementById('ico').value + '</strong>]; ';
	t += 'dič [<strong>' + document.getElementById('dic').value + '</strong>]; ';
	t += 'souhlas [<strong>' + document.getElementById('confirm').value + '</strong>]; ';
	return t;
}

/*************************************/
function DisplayLogin( display_end ){
	
	document.getElementById('remix').onclick = null;
	
	$('#remix').html(	
		'<form method="post" id="remix-form">'+
			'<input type="hidden" name="submit_context" value="hiddenlogin"/>'+
			'<input class="text" type="password" name="login" />'+
			'<input class="button" type="submit" value="ok" /><br />'+
			'<a href="" onclick="return HideLogin('+display_end+'); return false;">zavřít</a>'+
			( display_end == true ? '&nbsp;<a href="" onclick=" $(\'#remix-form\').submit(); ">ukončit</a>' : '' ) +
		'</form>');

	$('#remix input[name="login"]').focus();
}

function HideLogin(display_end){
	
	$('#remix').replaceWith(
		'<div id="remix" style="position: absolute;width: 10px; height: 10px; background-color: white;" onclick="DisplayLogin('+display_end+');">'
	);	

	return false;
}
