jQuery(document).ready(function(){
   // open external links in new window
  jQuery("a[rel='external']").click(function(e){
    e.preventDefault();
    window.open(this.href);
  });
});

sprintfWrapper = {

	init : function () {

		if (typeof arguments == "undefined") { return null; }
		if (arguments.length < 1) { return null; }
		if (typeof arguments[0] != "string") { return null; }
		if (typeof RegExp == "undefined") { return null; }

		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;

		while (match = exp.exec(string)) {
			if (match[9]) { convCount += 1; }

			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);

			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount]) < 0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);

		if (matches.length == 0) { return string; }
		if ((arguments.length - 1) < convCount) { return null; }

		var code = null;
		var match = null;
		var i = null;

		for (i=0; i<matches.length; i++) {

			if (matches[i].code == '%') { substitution = '%' }
			else if (matches[i].code == 'b') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'c') {
				matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'd') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'f') {
				matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'o') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 's') {
				matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'x') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'X') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
			}
			else {
				substitution = matches[i].match;
			}

			newString += strings[i];
			newString += substitution;

		}
		newString += strings[i];

		return newString;

	},

	convert : function(match, nosign){
		if (nosign) {
			match.sign = '';
		} else {
			match.sign = match.negative ? '-' : match.sign;
		}
		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l < 0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == "0" || nosign) {
				return match.sign + pad + match.argument;
			} else {
				return pad + match.sign + match.argument;
			}
		} else {
			if (match.pad == "0" || nosign) {
				return match.sign + match.argument + pad.replace(/0/g, ' ');
			} else {
				return match.sign + match.argument + pad;
			}
		}
	}
}

sprintf = sprintfWrapper.init;



function GetCount(){

	dateNow = new Date();									//grab current date
	amount = dateFuture.getTime() - dateNow.getTime();		//calc milliseconds between dates
	delete dateNow;

  days  = 0;
  hours = 0;
  mins  = 0;
  secs  = 0;

	// time is already past
	if(amount < 0){	  
		
	}
	// date is still good
	else{
		days=0;hours=0;mins=0;secs=0;out="";

		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

		days=Math.floor(amount/86400);//days
		amount=amount%86400;

		hours=Math.floor(amount/3600);//hours
		amount=amount%3600;

		mins=Math.floor(amount/60);//minutes
		amount=amount%60;

		secs=Math.floor(amount);//seconds	  
	}
	setTimeout("GetCount()", 1000);
	
  function generateItemCountDown(len,obj)
  {
  	if(len>0)
  	{
  	  var n;
      switch(len)
      {
      case 1:
        n = obj.l1
        break;
      case 2:
      case 3:
      case 4:
        n = obj.l2
        break;
      default:
        n = obj.l3
      } 
      jQuery("#odpocet .item:eq("+obj.pos+")").html('<strong><span>'+sprintf("%02d", len)+'</span>'+n+'</strong>');
    }
  }
  
  daysConfig = { 
    l1 : "den",
    l2 : "dny",
    l3 : "dn&#367;",
    pos : "0" 
  };
  generateItemCountDown(days,daysConfig);
  
  hoursConfig = { 
    l1 : "hodin",
    l2 : "hodiny",
    l3 : "hodin",
    pos : "1" 
  };
  generateItemCountDown(hours,hoursConfig);
  
  minsConfig = { 
    l1 : "minuta",
    l2 : "minuty",
    l3 : "minut",
    pos : "2"
  };
  generateItemCountDown(mins,minsConfig);

  secsConfig = {
    l1 : "sekunda",
    l2 : "sekundy",
    l3 : "sekund",
    pos : "3" 
  };
  generateItemCountDown(secs,secsConfig);  
}

jQuery(document).ready(function() {

  jQuery("#loadContent").click(function () {
   jQuery.ajax({
     type: "POST",
     cache: false,
     url: jQuery(this).attr("href"),
     success: function(html){
       jQuery("#loadedContentRemote").remove();
        jQuery(".bottom-box").after('<div id="loadedContentRemote">'+html+'</div>');
     }
   });
     return false;
  });
  jQuery(".detail").hide();

  jQuery(".expandButton").click(function () { $d = jQuery(".detail").eq(jQuery(".expandButton").index(this));
  if($d.is(":visible"))
 {
    $d.fadeOut(); jQuery(this).html('<a href="#">V&iacute;ce&nbsp;informac&iacute;&nbsp;&#0187;</a>');
 }
 else
 {
    $d.fadeIn(); jQuery(this).html('<a href="#">M&eacute;&#0328;e&nbsp;informac&iacute;&nbsp;&#0187;</a>');
 }; return false;
});

jQuery(".detail .close").click(function () {
 jQuery(this).parents(".detail").fadeOut();
 return false;
});
/*
   var _config = {
        changeTime : 7 * 1000
   }
   
   function clickBannerItem() {
      clearInterval(timer);
      
      $btn  = jQuery(".pagination-banner span");      
      nBtn  = $btn.length;
      nBtn2 = nBtn-2;
      index = jQuery(".pagination-banner span").index(this);      
      isNavButton = (index == (nBtn-1) || index == 0) ? true : false;
      
      $isBVis  = jQuery("#banner-panelak").is(":visible");
      $isB2Vis = jQuery("#banner-panelak2").is(":visible");

      if(isNavButton) {
        
        if($isBVis) {
          showB(1);
        } else if($isB2Vis) {
          showB(2);
        }
      } else {
        if(index==nBtn2) {
            if(!$isB2Vis) {
                showB(index-1);
            }
        } else {
            if(!$isBVis) {
                showB(index+1);
            }            
        }
      } 
   }

   function showB(number) {
    jQuery(".pagination-banner span").unbind("click");          
    $aVB  = jQuery("#banner-panelak");
    $aVB2 = jQuery("#banner-panelak2");

     if(typeof number == "undefined") {
        throw new Error("Nedefinovany banner");
     } else if (number == "1") {
        $aVB.css({
            "display":"block",
            "opacity":"1"
        }).stop()
        .animate({ 
            opacity: 0
        }, 250,"linear", function () {
            $aVB.hide();
            $aVB2.css({
                "display":"block",
                "opacity":"0"})
                .stop()
                .animate({ 
                    opacity: 1
                }, 250, "linear", function () {
            
                    jQuery(".pagination-banner span").bind("click",clickBannerItem);
        
                });
            
        });
     } else if (number == "2") {
        $aVB2.css({
            "display":"block",
            "opacity":"1"
        }).stop()
        .animate({ 
            opacity: 0
        }, 250,"linear", function () {
            $aVB2.hide();
            $aVB.css({
                "display":"block",
                "opacity":"0"})
                .stop()
                .animate({ 
                    opacity: 1
                }, 250, "linear", function () {
            
                    jQuery(".pagination-banner span").bind("click",clickBannerItem);
        
                });
            
        });
     }
  }

   function showBanner() {
        $aVB  = jQuery("#banner-panelak");
        $aVB2 = jQuery("#banner-panelak2");
        
        if($aVB.is(":visible")) {
            showB(1);
        } else if($aVB2.is(":visible")) {
            showB(2);
        }
   };

   function intervalTrigger() {
      return setInterval( showBanner, 7000 );
   };

   timer = intervalTrigger();
   jQuery(".pagination-banner span").bind("click",clickBannerItem);
   jQuery("#banner-panelak").bind('mouseover', function(){clearInterval(timer);});
   jQuery("#banner-panelak2").bind('mouseover', function(){clearInterval(timer);});
 */
});

function spocitatSlevu() {
	var license = {
		year: jQuery('#license_year').attr('value'),
		month: jQuery('#license_month').attr('value'),
		day: jQuery('#license_day').attr('value')
	}
	var now = new Date;
	now = {
		year: now.getYear(),
		month: (now.getMonth() +1),
		day: now.getDate()
	}
	if(now.year < 1000) now.year += 1900; // :)
	var years = now.year - license.year;
	if(years && (now.month < license.month || (now.month == license.month && now.day < license.day))) years--;
	sleva = years > 30 ? 30 : years;
	jQuery('#license_sleva').html(sleva +'&nbsp;%');
	jQuery('#f_license').attr('value', license.day +'.'+ license.month +'.'+ license.year); // pro kalendar
}

