var chart1,chart2,map,score,step,years,actclass,fondurok,typ;
var markersArray = [],markerCluster;

Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep)
{ 
    var n = this,
    c = isNaN(decimals) ? 2 : Math.abs(decimals), //if decimal is zero we must take it, it means user does not want to show any decimal
    d = decimal_sep || ',', //if no decimal separetor is passed we use the comma as default decimal separator (we MUST use a decimal separator)

    /*
   according to [http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function]
   the fastest way to check for not defined parameter is to use typeof value === 'undefined' 
   rather than doing value === undefined.
   */   
    t = (typeof thousands_sep === 'undefined') ? ' ' : thousands_sep, //if you don't want ot use a thousands separator you can pass empty string as thousands_sep value

    sign = (n < 0) ? '-' : '',

    //extracting the absolute value of the integer part of the number and converting to string
    i = parseInt(n = Math.abs(n).toFixed(c)) + '', 

    j = ((j = i.length) > 3) ? j % 3 : 0; 
    return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); 
}


function showLetter(ltr){
    
    if(ltr!='all'){  
        $('div[id^="letter_data_"]').each(function(index,element) {
            $(element).hide();  
        });    
        $('div[id^="letter_data_'+ltr+'"]').show();
    }else{
        $('div[id^="letter_data_"]').each(function(index,element) {
            $(element).show();  
        });
    }
    
    $('li[id^="letter_tab_"]').each(function(index,element) {
        $(element).removeClass("active");  
    });
    
    $('li[id^="letter_tab_'+ltr+'"]').addClass("active");    
}

$(document).ready(function() {


    $('a[href$=".zip"],a[href$=".doc"],a[href$=".xls"],a[href$=".xlsx"],a[href$=".docx"],a[href$=".pdf"]').each(function(index,odkaz) {
        $(odkaz).attr('target', '_blank');
    });
	
    // odber newsletteru
    if($("a.newsletter_fancy").length)
        $("a.newsletter_fancy").fancybox({
            'transitionIn'	:	'elastic',
            'transitionOut'	:	'elastic',
            'speedIn'		:	250, 
            'speedOut'		:	100,
            'titleShow'		: false, 
            'overlayShow'	:	true,
            'overlayOpacity': 0.75,
            'overlayColor'  : "#222222",
            'padding'       : 0,
            'width'         : 388,
            'height'        : 320,
            'autoDimensions': false
        });
   
    $('a.topanchor').click(function(){
        $('html, body').animate({
            scrollTop:0
        }, 'slow');
        return false;
    });

    $('form#login .text input, form#login .password input').focus(function() {
        if($(this).hasClass('onblur')) {
            $(this).removeClass('onblur');
            $(this).attr('title',$(this).val());
            $(this).val('');
        }
    });
	
    $('form#login .text input, form#login .password input').blur(function() {
        if($(this).val() == '') {
            $(this).val($(this).attr('title'));
            $(this).attr('title','');
            $(this).addClass('onblur');
        }
    });
	
    if($('#oceneni ul').length)
        $('#oceneni ul').cycle({
            fx: 'fade',
            timeout: 4500,
            speed: 700
        });

  
    

});


function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()+"; path=/");
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name){
            return unescape(y);
        }
    }
}

function recalcJednKolik(val) {
    if(!val) {
        val = $( "#slider-jedn-kolik" ).slider("value");
    }
    $( "#slider-jedn-kolik  .ui-slider-handle" ).html( val.toMoney(0) + '&nbsp;'+igcurr);
    recalcJedn(val,$( "#slider-jedn-delka" ).slider("value"));
}

function recalcJednDelka(val) {
    if(!val) {
        val = $( "#slider-jedn-delka" ).slider("value");
    }
    $( "#slider-jedn-delka  .ui-slider-handle" ).html( val + '&nbsp;'+igterm);
    recalcJedn($( "#slider-jedn-kolik" ).slider("value"),val);
}

function recalcJedn2Kolik(val) {
    if(!val) {
        val = $( "#slider-jedn2-kolik" ).slider("value");
    }
    $( "#slider-jedn2-kolik  .ui-slider-handle" ).html( val.toMoney(0) + '&nbsp;'+igcurr);
    recalcJedn2(val,$( "#slider-jedn2-delka" ).slider("value"));
}

function recalcJedn2Delka(val) {
    if(!val) {
        val = $( "#slider-jedn2-delka" ).slider("value");
    }
    $( "#slider-jedn2-delka  .ui-slider-handle" ).html( val + '&nbsp;'+igterm);
    recalcJedn2($( "#slider-jedn2-kolik" ).slider("value"),val);
}

function recalcMesKolik(val) {
    if(!val) {
        val = $( "#slider-mes-kolik" ).slider("value");
    }
    $( "#slider-mes-kolik  .ui-slider-handle" ).html( val.toMoney(0) + '&nbsp;'+igcurr);
    recalcMes(val,$( "#slider-mes-delka" ).slider("value"));
}

function recalcMesDelka(val) {
    if(!val) {
        val = $( "#slider-mes-delka" ).slider("value");
    }
    $( "#slider-mes-delka  .ui-slider-handle" ).html( val + '&nbsp;'+igterm);
    recalcMes($( "#slider-mes-kolik" ).slider("value"),val);
}

function recalcMes2Kolik(val) {
    if(!val) {
        val = $( "#slider-mes2-kolik" ).slider("value");
    }
    $( "#slider-mes2-kolik  .ui-slider-handle" ).html( val.toMoney(0) + '&nbsp;'+igcurr);
    recalcMes2(val,$( "#slider-mes2-delka" ).slider("value"));
}

function recalcMes2Delka(val) {
    if(!val) {
        val = $( "#slider-mes2-delka" ).slider("value");
    }
    $( "#slider-mes2-delka  .ui-slider-handle" ).html( val + '&nbsp;'+igterm);
    recalcMes2($( "#slider-mes2-kolik" ).slider("value"),val);
}


function recalcIJednKolik(val) {
    if(!val) {
        val = $( "#slider-ijedn-kolik" ).slider("value");
    }
    $("#input-ijedn-kolik").attr("value", val);
    $("#input-ijedn-kolik").change();
}

function recalcIJednDelka(val) {
    if(!val) {
        val = $( "#slider-ijedn-delka" ).slider("value");
    }
    $("#input-ijedn-delka").attr("value", val);
    $("#input-ijedn-delka").change();
}

function recalcIswitchKolik(val) {
    if(!val) {
        val = $( "#slider-iswitch-kolik" ).slider("value");
    }
    $("#input-iswitch-kolik").attr("value", val);
    $("#input-iswitch-kolik").change();
}

function recalcIswitchDelka(val) {
    if(!val) {
        val = $( "#slider-iswitch-delka" ).slider("value");
    }
    $("#input-iswitch-delka").attr("value", val);
    $("#input-iswitch-delka").change();
}
function recalcIMesKolik(val) {
    if(!val) {
        val = $( "#slider-imes-kolik" ).slider("value");
    }
    $("#input-imes-kolik").attr("value", val);
    $("#input-imes-kolik").change();
}

function recalcIMesDelka(val) {
    if(!val) {
        val = $( "#slider-imes-delka" ).slider("value");
    }
    $("#input-imes-delka").attr("value", val);
    $("#input-imes-delka").change();
}

function manIMesKolik(promn){
    if(promn > 0){
        $("#slider-imes-kolik").slider("value", promn);
        recalcIMes(promn ,$("#input-imes-delka").attr("value"));
    }
}
function manIJednKolik(promn){
    if(promn > 0){
        $("#slider-ijedn-kolik").slider("value", promn);
        recalcIJedn(promn ,$("#input-ijedn-delka").attr("value"));
    }
}
function manIswitchKolik(promn){
    if(promn > 0){
        $("#slider-iswitch-kolik").slider("value", promn);
        recalcSwitch(promn ,$("#input-iswitch-delka").attr("value"));
    }
}
function manDelka(promn){
    if(promn > 0){
        $("#slider-imes-delka").slider("value", promn);
        $("#slider-ijedn-delka").slider("value", promn);
        $("#slider-iswitch-delka").slider("value", promn);
        recalcSwitch($("#input-imes-kolik").attr("value"), promn);
        recalcIJedn($("#input-ijedn-kolik").attr("value"), promn);
        recalcIMes($("#input-iswitch-kolik").attr("value"), promn);
    }
}

function recalcJedn(valkolik,valdelka) {
    var urok1m = Math.pow(1+(2.0/100),(1/12)) - 1;
    var urok2m = Math.pow(1+(4.5/100),(1/12)) - 1;
    var urok3m = Math.pow(1+(8.0/100),(1/12)) - 1;
	
    $( "#vysl-jedn-1" ).html( Math.round(valkolik * (Math.pow(1+urok1m,valdelka*12))).toMoney(0));
    $( "#vysl-jedn-2" ).html( Math.round(valkolik * (Math.pow(1+urok2m,valdelka*12))).toMoney(0));
    $( "#vysl-jedn-3" ).html( Math.round(valkolik * (Math.pow(1+urok3m,valdelka*12))).toMoney(0));
}

function recalcJedn2(valkolik,valdelka) {
    var urok1m = Math.pow(1+(2.0/100),(1/12)) - 1;
    var urok2m = Math.pow(1+(4.5/100),(1/12)) - 1;
    var urok3m = Math.pow(1+(8.0/100),(1/12)) - 1;
	
    $( "#vysl-jedn2-1" ).html( Math.round(valkolik * (1/(Math.pow(1+urok1m,valdelka*12)))).toMoney(0));
    $( "#vysl-jedn2-2" ).html( Math.round(valkolik * (1/(Math.pow(1+urok2m,valdelka*12)))).toMoney(0));
    $( "#vysl-jedn2-3" ).html( Math.round(valkolik * (1/(Math.pow(1+urok3m,valdelka*12)))).toMoney(0));
}

function recalcMes(valkolik,valdelka) {
    var urok1m = Math.pow(1+(2.0/100),(1/12)) - 1;
    var urok2m = Math.pow(1+(4.5/100),(1/12)) - 1;
    var urok3m = Math.pow(1+(8.0/100),(1/12)) - 1;
	
    $( "#vysl-mes-1" ).html( Math.round(valkolik * ((Math.pow(1+urok1m,valdelka*12)-1)/urok1m)).toMoney(0));
    $( "#vysl-mes-2" ).html( Math.round(valkolik * ((Math.pow(1+urok2m,valdelka*12)-1)/urok2m)).toMoney(0));
    $( "#vysl-mes-3" ).html( Math.round(valkolik * ((Math.pow(1+urok3m,valdelka*12)-1)/urok3m)).toMoney(0));
}

function recalcMes2(valkolik,valdelka) {
    var urok1m = Math.pow(1+(2.0/100),(1/12)) - 1;
    var urok2m = Math.pow(1+(4.5/100),(1/12)) - 1;
    var urok3m = Math.pow(1+(8.0/100),(1/12)) - 1;
	
    $( "#vysl-mes2-1" ).html( Math.round(valkolik * (urok1m/(Math.pow(1+urok1m,valdelka*12)-1))).toMoney(0));
    $( "#vysl-mes2-2" ).html( Math.round(valkolik * (urok2m/(Math.pow(1+urok2m,valdelka*12)-1))).toMoney(0));
    $( "#vysl-mes2-3" ).html( Math.round(valkolik * (urok3m/(Math.pow(1+urok3m,valdelka*12)-1))).toMoney(0));
}

function recalcIAll(urok) {
    fondurok = urok;
	
    recalcIJedn($( "#slider-ijedn-kolik" ).slider("value"),$( "#slider-ijedn-delka" ).slider("value"));
    recalcIMes($( "#slider-imes-kolik" ).slider("value"),$( "#slider-imes-delka" ).slider("value"));
    recalcSwitch($( "#slider-iswitch-kolik" ).slider("value"),$( "#slider-iswitch-delka" ).slider("value"));
}

function recalcIJedn(valkolik,valdelka) {

    var urok = Math.pow(1+(fondurok/100),(1/12)) - 1;
	
    $( "#vysl-ijedn" ).html( Math.round(valkolik * (Math.pow(1+urok,valdelka*12))).toMoney(0));
}

function recalcSwitch(valkolik,valdelka) {

    var urok = Math.pow(1+(fondurok/100),(1/12)) - 1;
	
    $( "#vysl-switch-1" ).html( Math.round(valkolik * (1/(Math.pow(1+urok,valdelka*12)))).toMoney(0));	
    $( "#vysl-switch-2" ).html( Math.round(valkolik * (urok/(Math.pow(1+urok,valdelka*12)-1))).toMoney(0));
	
}

function recalcIMes(valkolik,valdelka) {

    var urok = Math.pow(1+(fondurok/100),(1/12)) - 1;
	
    $( "#vysl-imes" ).html( Math.round(valkolik * ((Math.pow(1+urok,valdelka*12)-1)/urok)).toMoney(0));
}

function initCalcInvest(num,init) {
    if(num == 1) {
        $( "#slider-jedn-kolik" ).slider({
            value: (init > 0?init:jedncurr),
            min: 0,
            max: jednmax,
            step: jednstep,
            range: 'min',
            slide: function( event, ui ) {
                recalcJednKolik(ui.value);
            },
            stop: function( event, ui ) {
                recalcJednKolik(ui.value);
            }
        });
        recalcJednKolik();

        $( "#slider-jedn-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcJednDelka(ui.value);
            },
            stop: function( event, ui ) {
                recalcJednDelka(ui.value);
            }
        });
        recalcJednDelka();

        $( "#slider-jedn2-kolik" ).slider({
            value:dosahcurr,
            min: 0,
            max: dosahmax,
            step: dosahstep,
            range: 'min',
            slide: function( event, ui ) {
                recalcJedn2Kolik(ui.value);
            },
            stop: function( event, ui ) {
                recalcJedn2Kolik(ui.value);
            }
        });
        recalcJedn2Kolik();

        $( "#slider-jedn2-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcJedn2Delka(ui.value);
            },
            stop: function( event, ui ) {
                recalcJedn2Delka(ui.value);
            }
        });
        recalcJedn2Delka();
		
        $("#tab1switch").click(function(event) {
            $("#tabs-1-1").toggle();
            $("#tabs-1-2").toggle();
            event.preventDefault();
        });
		
    }
	
    if(num == 2) {
        $( "#slider-mes-kolik" ).slider({
            value:(init > 0?init:mescurr),
            min: 0,
            max: mesmax,
            step: messtep,
            range: 'min',			
            slide: function( event, ui ) {
                recalcMesKolik(ui.value);
            }
        });
        recalcMesKolik();

        $( "#slider-mes-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcMesDelka(ui.value);
            }
        });
        recalcMesDelka();	
		
        $( "#slider-mes2-kolik" ).slider({
            value:dosahcurr,
            min: 0,
            max: dosahmax,
            step: dosahstep,
            range: 'min',			
            slide: function( event, ui ) {
                recalcMes2Kolik(ui.value);
            }
        });
        recalcMes2Kolik();

        $( "#slider-mes2-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcMes2Delka(ui.value);
            }
        });
        recalcMes2Delka();
				
        $("#tab2switch").click(function(event) {
            $("#tabs-2-1").toggle();
            $("#tabs-2-2").toggle();
            event.preventDefault();
        });
    }
	
    if(num == 3) {
        $( "#slider-ijedn-kolik" ).slider({
            value:jedncurr,
            min: 0,
            max: jednmax,
            step: jednstep,
            range: 'min',
            slide: function( event, ui ) {
                recalcIJednKolik(ui.value);
            },
            stop: function( event, ui ) {
                recalcIJednKolik(ui.value);
            }
        });
        recalcIJednKolik();
		
        $( "#slider-ijedn-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcIJednDelka(ui.value);
            },
            stop: function( event, ui ) {
                recalcIJednDelka(ui.value);
            }
        });
        recalcIJednDelka();		
		
        $( "#slider-imes-kolik" ).slider({
            value:mescurr,
            min: 0,
            max: mesmax,
            step: messtep,
            range: 'min',			
            slide: function( event, ui ) {
                recalcIMesKolik(ui.value);
            }
        });		
        recalcIMesKolik();

        $( "#slider-imes-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcIMesDelka(ui.value);
            }
        });
        recalcIMesDelka();

        $( "#slider-iswitch-kolik" ).slider({
            value:dosahcurr,
            min: 0,
            max: dosahmax,
            step: dosahstep,
            range: 'min',
            slide: function( event, ui ) {
                recalcIswitchKolik(ui.value);
            },
            stop: function( event, ui ) {
                recalcIswitchKolik(ui.value);
            }
        });
        recalcIswitchKolik();

        $( "#slider-iswitch-delka" ).slider({
            value:10,
            min: 0,
            max: 60,
            step: 1,
            range: 'min',
            slide: function( event, ui ) {
                recalcIswitchDelka(ui.value);
            },
            stop: function( event, ui ) {
                recalcIswitchDelka(ui.value);
            }
        });
        recalcIswitchDelka();
		
        $("#tab1switch").click(function(event) {
            $("#tabs-1-1").toggle();
            $("#tabs-1-2").toggle();
            event.preventDefault();
        });
		
        $("a.cswitch").click(function(event) {
            $("#calc-switch-1").toggle();
            $("#calc-switch-2").toggle();
            event.preventDefault();
        });
		
		
    }	
}

function recalcKonto() {
    var rezerva = 0;
    var posilat = 0;
    var vydaje = 0;
    $('.cpikonto-vydaje div.input input').each(function (idx,el) {
        if(!isNaN(parseInt($(el).val()))) {
            vydaje += parseInt($(el).val());
        }
    });
    $('#cpikonto-vydaje-celkem').html(vydaje.toMoney(0));
    var partner = 0;
    if(!isNaN(parseInt($('#cpikonto-partner').val()))) {
        partner = parseInt($('#cpikonto-partner').val());
    }
    posilat = (vydaje + partner) * 1.2;
    rezerva = posilat - vydaje - partner;
    $('#cpikonto-posilat').html(posilat.toMoney(0));
    $('#cpikonto-rezerva').html(rezerva.toMoney(0));
}

function initCalcKonto() {
    $('.cpikonto-vydaje div.input input, .cpikonto-partner div.input input').change(function(event) {
        recalcKonto();
    });
	
    $('.cpikonto-vydaje div.input input, .cpikonto-partner div.input input').focus(function() {
        if($(this).hasClass('onblur')) {
            $(this).removeClass('onblur');
            $(this).attr('title',$(this).val());
            $(this).val('');
        }
    });
	
    $('.cpikonto-vydaje div.input input, .cpikonto-partner div.input input').blur(function() {
        if($(this).val() == '') {
            $(this).val($(this).attr('title'));
            $(this).attr('title','');
            $(this).addClass('onblur');
        }
    });	

    recalcKonto();
}

function mapInitialize() {

    latlng = new google.maps.LatLng(49.84,15.43);
   
    var myOptions = {
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
        },
        panControl: false,
        streetViewControl: false,
        zoom: 7,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("googlemap"), myOptions);

}

function getImgByType(type) {
    switch(type) {
        case "OM":
            img = 'pobocky.png';
            break;
        case "OZ":
        case "VOZ":
            img = 'poradci.png';
            break;
        case "SI":
            img = 'sidlo-cpinvest-pointer.png';
            break;
        case "KC":
            img = 'klientske-centrum-cpinvest-pointer.png';
            break;
    }
    return img;
}

function mapSmallDraw(ppoint,ptype) {

    var myOptions = {
        mapTypeControl: false,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
        },
        panControl: false,
        streetViewControl: false,
        zoom: 15,
        center: ppoint,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("gmapsmall"), myOptions);
	
    marker = new google.maps.Marker({
        map: map,
        position: ppoint,
        icon: new google.maps.MarkerImage('/gfx/icons/' + getImgByType(ptype), null, null, new google.maps.Point(18,47)) 
    });	

}

function markersToMap(data) {
    
    var boxOptions = {
        content: ''
        ,
        disableAutoPan: false
        ,
        maxWidth: 0
        /*,pixelOffset: new google.maps.Size(-140, 0)*/
        ,
        zIndex: null
        ,
        closeBoxURL: ""
        ,
        infoBoxClearance: new google.maps.Size(1, 1)
        ,
        isHidden: false
        ,
        pane: "floatPane"
        ,
        enableEventPropagation: false
    };	
    iwin = new InfoBox(boxOptions);
	
    for (var index = 0; index < data.length; ++index) {
        var item = data[index];

        marker = new google.maps.Marker({
            map: map,
            title: item.title,
            position: new google.maps.LatLng(parseFloat(item.gps_lat),parseFloat(item.gps_lon)),
            box: item.box,
            typ: item.typ,
            icon: new google.maps.MarkerImage('/gfx/icons/' + getImgByType(item.typ), null, null, new google.maps.Point(18,47)),
            shape: {
                coords:[16,43,9,29,2,23,0,15,4,5,9,1,15,0,23,1,30,9,31,15,29,24,21,30],
                type:'poly'
            }
        });

        google.maps.event.addListener(marker, 'click', function(event) {
            iwin.setContent(this.box);
            iwin.open(map,this);
        });
		
        markersArray.push(marker);
	
    }
	
    markerCluster = new MarkerClusterer(map, markersArray, {
        ignoreHidden:true,
        gridSize:35,
        maxZoom:18,
        styles:[{
            textColor: '#00356f',
            height: 31, 
            width: 31, 
            url:'/gfx/icons/skupina-obchodnich-mist-prazdne.png'
        }]
    });
    
}

function markersByType() {
    var show_oz = $('#show_oz').is(':checked');
    var show_voz = $('#show_voz').is(':checked');
    var show_om = $('#show_om').is(':checked');
	
    if (markersArray.length > 0) {
        for (i = 0; i < markersArray.length; i++) {
            if((markersArray[i].typ == "VOZ" && show_voz ) || (markersArray[i].typ == "OZ" && show_oz ) || (markersArray[i].typ == "OM" && show_om ) || markersArray[i].typ == "SI" || markersArray[i].typ == "KC" ) {
                markersArray[i].setVisible(true);
            } else {
                markersArray[i].setVisible(false);
            }
			
        }
    }
    markerCluster.repaint();
}

function searchLocation() {
    
    geocoder = new google.maps.Geocoder();
	
    srchtxt = $('#search_loc').val();
    if(srchtxt != '') {
        geocoder.geocode( {
            'address': srchtxt
        }, function(results, status) {
            $('#search_loc').val('');
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                map.setZoom(14);
            }
        });
    }
    
}

function initInvesticniDotaznik(ptyp) {

    $('.investicni-dotaznik div.profiles div.q').hide();
    $('.investicni-dotaznik div.products div.q').hide();
    $('.investicni-dotaznik div.products h3').hide();
    $('.investicni-dotaznik div.otazky div.q').hide();
    $('.investicni-dotaznik div.otazky #q1').show();
    $('.investicni-dotaznik a.again').hide();
	
    $( ".investicni-dotaznik div.progressbar span" ).width('0%');
    //$( ".investicni-dotaznik #investicni-progress" ).progressbar({value: 0});
    score = 0;
    years = 0;
    typ = ptyp;
}

function moveDotaznik(ascore,gotostep) {
    steps = 5;
    score = score + ascore;
    $('.investicni-dotaznik div.otazky div.q').hide();
	
    if(gotostep == 0) {
        $( ".investicni-dotaznik div.progressbar span" ).width('100%');
        $('.investicni-dotaznik div.products h3').show();
        $('.investicni-dotaznik a.again').show();
        if(score >= 5 && score <= 12) {
            $('.investicni-dotaznik div.profiles #p1').show();
            if(typ==1) {
                if(years < 4) {
                    $('.investicni-dotaznik div.products #pr1').show();
                } else {
                    $('.investicni-dotaznik div.products #pr2').show();
                }
            } else if(typ==0) {
                $('.investicni-dotaznik div.products #pr5').show();
            }
        } else if(score >= 13 && score <= 18) {
            $('.investicni-dotaznik div.profiles #p2').show();
            if(typ==1) {
                if(years < 2) {
                    $('.investicni-dotaznik div.products #pr1').show();
                } else {
                    $('.investicni-dotaznik div.products #pr2').show();
                }
            } else if(typ==0) {
                $('.investicni-dotaznik div.products #pr6').show();
            }
        } else {
            $('.investicni-dotaznik div.profiles #p3').show();
            if(typ==1) {
                if(years < 4) {
                    $('.investicni-dotaznik div.products #pr2').show();
                } else if(years < 6) {
                    $('.investicni-dotaznik div.products #pr3').show();
                } else {
                    $('.investicni-dotaznik div.products #pr4').show();
                }
            } else if(typ==0) {
                $('.investicni-dotaznik div.products #pr7').show();
            }			
        }
    } else {
        $( ".investicni-dotaznik div.progressbar span" ).width(Math.round((gotostep-1) / steps * 100)+'%');
        //$( ".investicni-dotaznik #investicni-progress" ).progressbar({value: Math.round((gotostep-1) / steps * 100)});
        $('.investicni-dotaznik div.otazky #q'+gotostep).show();
    }
}

function initNewsScroller(pselector,pid) {
    $(pid + ' div').hide();
    $(pid + ' div:first').show().addClass('active');
	
    $(pselector + ' a.next').click(function(event) {
        var act = $(pid + ' div.active');
        var next = act.next();
        if(next.length > 0) {
            $(pid).hide('slide',null,'fast',function() {
                act.removeClass('active').hide();
                next.addClass('active').show();
                $(pid).show('slide',{
                    direction:'right'
                },'fast');
            });
        }
        event.preventDefault();
    });
    $(pselector + ' a.prev').click(function(event) {
        var act = $(pid + ' div.active');
        var prev = act.prev();
        if(prev.length > 0) {
            $(pid).hide('slide',{
                direction:'right'
            },'fast',function() {
                act.removeClass('active').hide();
                prev.addClass('active').show();
                $(pid).show('slide',null,'fast');
            });
        }
        event.preventDefault();
    });
}

//kravatak
function exchangeClass($item) {

    if($.browser.name == "msie" && $.browser.versionNumber < 9){
        $item.hide();
        $drop.hide();

        var dropclass;
        $.each(krclasses, function(index, value) {
            if($drop.children().hasClass(value)) {
                dropclass = value;
                $drop.children().removeClass(value);
            }
            if($item.hasClass(value)) {
                $item.removeClass(value);
                $drop.children().addClass(value);
                recalcKravatak(index);
                actclass = value;
            }
        });
		
        $item.addClass(dropclass);
        $item.show();
        $drop.show();

    } else {
        $item.fadeOut();
        $drop.fadeOut(function() {
            var dropclass;
            $.each(krclasses, function(index, value) { 
                if($drop.children().hasClass(value)) {
                    dropclass = value;
                    $drop.children().removeClass(value);
                }
                if($item.hasClass(value)) {
                    $item.removeClass(value);
                    $drop.children().addClass(value);
                    recalcKravatak(index);
                    actclass = value;
                }
            });
            $item.addClass(dropclass);
            $item.fadeIn();
            $drop.fadeIn();
        });
    }
}

function getOff( $item ) {
    var pos = $item.offset();
    var mindist = -1, minitem = null;

    $('.widget .items ul li').each(function() {
        var ipos = $(this).offset();
        var dist = Math.sqrt(Math.pow(ipos.left-pos.left,2) + Math.pow(ipos.top-pos.top,2));
        if(mindist == -1 || dist < mindist) {
            mindist = dist;
            minitem = $(this);
        }
    });
    exchangeClass(minitem);
}

function recalcKravatak(idx) {
    var mes_investice = krinvests[idx];
    var mesicni_urok = Math.pow(1+0.06,1/12)-1;
    var roky = 5;
    var konec = Math.round((mes_investice*((Math.pow(1+mesicni_urok,roky*12)-1)/mesicni_urok)) / 1000) * 1000;
    var jednorazova = Math.round((konec*(1/(Math.pow(1+mesicni_urok,roky*12)))) / 1000) * 1000;
	
    $('.widget .right-col .headline span').html(krnames[idx]);
    $('.widget .right-col .price p.left em').html(mes_investice.toMoney(0));
    $('.widget .right-col .price p.right em').html(jednorazova.toMoney(0));
    $('.widget .right-col p.zisk em').html(konec.toMoney(0));
    $(".widget a#kra-1").attr("href", krurl[0]+'?p='+mes_investice);
    $(".widget a#kra-2").attr("href", krurl[1]+'?p='+jednorazova);

}

function initKravatak() {

    if($.browser.name == "msie" && $.browser.versionNumber < 9){
        $( "li", $seznam ).draggable({
            revert: "invalid", // when not dropped, the item will revert back to its initial position
            containment: $( ".widget .items" ),
            helper: "clone",
            cursor: "move"
        });
		
        $drop.draggable({
            containment: $( ".widget .items" ),
            helper: function( event ) {
                return $( "<div id='postava-off' class='"+actclass+"'></div>" );
            },
            cursorAt: {
                top: 50, 
                left: 45
            },
            cursor: "move"
        });
    } else {
        $( "li", $seznam ).draggable({
            revert: "invalid", // when not dropped, the item will revert back to its initial position
            containment: $( ".widget .items" ),
            helper: "clone",
            cursor: "move",
            opacity: 0.7
        });
		
        $drop.draggable({
            containment: $( ".widget .items" ),
            helper: function( event ) {
                return $( "<div id='postava-off' class='"+actclass+"'></div>" );
            },
            cursorAt: {
                top: 50, 
                left: 45
            },
            cursor: "move",
            opacity: 0.7
        });	
    }

    $drop.droppable({
        accept: ".widget .items ul > li",
        activeClass: "xactive",
        hoverClass: "xhover",
        drop: function( event, ui ) {
            exchangeClass( ui.draggable );
        }
    });

    $seznam.droppable({
        accept: ".widget .items div",
        drop: function( event, ui ) {
            getOff( ui.helper );
        }
    });

    $('.widget .bottom a').click(function(event) {
        if($('.widget').hasClass('widget-opened')) {
            if($.browser.name == "msie" && $.browser.versionNumber < 9){
                $('.widget-opened .items, .widget-opened .right-col, .widget-opened .opened').hide();
            } else {
                $('.widget-opened .items, .widget-opened .right-col, .widget-opened .opened').fadeOut();
            }
            $('.widget-opened .middle').animate({
                height:166
            },function() {
                $('.widget-opened').removeClass('widget-opened').addClass('widget-closed');
                if($.browser.name == "msie" && $.browser.versionNumber < 9){
                    $('.widget-closed .postavy, .widget-closed .closed').show();
                } else {
                    $('.widget-closed .postavy, .widget-closed .closed').fadeIn();
                }
            });
            if($.browser.name == "msie" && $.browser.versionNumber == 7){
                $('#grafcontainer').show();
            }
			
            setCookie('kravatak','c',50);
			
        } else {
            if($.browser.name == "msie" && $.browser.versionNumber < 9){
                if($.browser.name == "msie" && $.browser.versionNumber == 7){
                    $('#grafcontainer').hide();
                }
                $('.widget-closed .postavy, .widget-closed .closed').hide();
                $('.widget-closed').removeClass('widget-closed').addClass('widget-opened');
                $('.widget-opened .middle').animate({
                    height:505
                },function() {
                    $('.widget-opened .items, .widget-opened .opened, .widget-opened .right-col').show();
                });
            } else {
                $('.widget-closed .postavy, .widget-closed .closed').fadeOut(function () {
                    $('.widget-closed').removeClass('widget-closed').addClass('widget-opened');
                    $('.widget-opened .middle').animate({
                        height:505
                    },function() {
                        $('.widget-opened .items, .widget-opened .opened, .widget-opened .right-col').fadeIn();
                    });
                });
            }		
            setCookie('kravatak','o',50);		
        }
		
		
        event.preventDefault();
    });
	
    recalcKravatak(9);
    actclass = krclasses[9];
	
}

function switchInvDoporuceni() {
    $('.investice-roky .recommend ul').hide();
    if($('.investice-roky #years6').is(':checked')) {
        $('.investice-roky .recommend #ryears6').show();
    } else {
        $('.investice-roky .recommend #ryears7').show();
    }
}
