
 $(document).ready( function() {
	 initPage();
	 
	 
	// confirmator
	
	$('a[data-nette-confirm], button[data-nette-confirm], input[data-nette-confirm]').live('click', function (e) {
		 var el = $(this);
		 if (!confirm(el.attr('data-nette-confirm'))) {
			 e.preventDefault();
			 e.stopImmediatePropagation();
			 return false;
		 }

	 });

	// animation
	
	$('a[data-nette-animation-fadeout]').live('click', function (e) {
		
		var el = $(this);
		var cont = el.attr('data-nette-animation-fadeout');
		
		$('#' + cont).fadeOut(1000);

	 });

	$('a[data-nette-animation-drop]').live('click', function (e) {
		
		var el = $(this);
		var cont = el.attr('data-nette-animation-drop');
		
		$('#' + cont).effect('explode', {}, 1000);

	 });
	
		
	$("a.ajax").live('click', function (event) {
		event.preventDefault();
		$.getJSON(this.href, jQuery.nette.success);
		
		if (typeof _gaq != "undefined") _gaq.push(['_trackEvent', 'clicks', 'click', this.href]);
		
	});

	$("form.ajax").live('submit', function (event) {
		$(this).ajaxSubmit(jQuery.nette.success);
		event.preventDefault();
		if (typeof _gaq != "undefined") _gaq.push(['_trackEvent', 'forms', 'submit', this.action]);
		
	});

	$("form.ajax :submit").live('click', function (event) {
		
		$(this).ajaxSubmit(jQuery.nette.success);
		event.preventDefault();
		if (typeof _gaq != "undefined") _gaq.push(['_trackEvent', 'forms', 'submit', this.action]);
		
	});


	jQuery.nette.createSpinner();
	 
	$("#fbPlace").html('<iframe src="https://www.facebook.com/plugins/like.php?href=http%253A%252F%252Fhosting.blueboard.cz&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=recommend&amp;font=trebuchet%2Bms&amp;colorscheme=dark&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px; " allowTransparency="true"></iframe>');




	 
 });

 function initPage(){
 
	if (Modernizr.mq('only all and (min-width: 768px)')){
	
		$('div.dialog').draggable(
			{
				handle: "h2.handle"
			}
		).center();
		
	}
	else
	{
		
	}



	// yellow highlight

	$(".yft").each(function(){
		$(this).effect("highlight", {}, 5000);
	});	

	 //console.log('reload');
	 
	 $("a.sipicka").unbind().click(function(e){
		 var r = $("div.rozbalovatko");
		 r.slideToggle(500);
		 
		 $("a.sipicka, a.sipicka2").each(function(){
			 var s = $(this);
			 if (s.hasClass('sipicka2')){
				 s.removeClass('sipicka2');
				 s.addClass('sipicka');
				 s.text('Více informací');
			 }
			 else{
				 s.removeClass('sipicka');
				 s.addClass('sipicka2');
				 s.text('Méně informací');
			 }
		 });
		 return false;
	 });
	 
	 
	 $("[data-scroll]").live('click', function(){
		 if (!$(this).hasClass('ajax'))
		 {
			 $.scrollTo('#'+$(this).attr('data-scroll'));
		 }
		 
	 });

	 
	// openable closable blocks
	
	// hidden
	
	$(".hidden").hide();
	
	// shower
	
	$('a[data-open]').unbind().click(function(event){
		
		$target = $(this).attr('data-open');
		$('#' + $target).slideToggle();
        if ($(this).hasClass('up'))
        {
            $(this).removeClass('up');
            $(this).addClass('down');
        }
        else
        {
            $(this).removeClass('down');
            $(this).addClass('up');
        }
		event.preventDefault();
	});	
	 
	// closable

	$(".closable .close").unbind().click(function(){
		//$(this).closest('.closable');
		$('.overlay').fadeOut(500);
		return false;
	});
	

    // vlastni sorter na datum a na měnu

    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'mena', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            var numerized = parseFloat(s);
            return (numerized == s) ? 0: numerized;
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    }); 

    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'czechDate', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            var numerized = s.replace(/(\d{1,2})\.\s?(\d{1,2})\.\s?(\d{2,4})/, "$3$2$1");
            return (numerized == s) ? 0: numerized;
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    }); 
    
    $(".sortable").tablesorter();
    $(".pagable").tablesorterPager({container: $(".pager")}); 

	// klientska sekce, zmena domeny v Emailech
	
	$("#emailyDomena").change(function(){
		document.location.href = $(this).val();
	});
	
	
	$('.tip').tipTip({ 
		maxWidth: 300, 
		edgeOffset: 3, 
		delay: 50
	});

	$('.toptip').tipTip({ 
		maxWidth: 300, 
		edgeOffset: 0, 
		delay: 50,
		defaultPosition: "top"
	});



	$('input.nactiAres').change( function() {
	   
	   var parent = $(this).closest('div');
	   var ic = parent.find("input.nactiAres").val();
	   var icInput = parent.find("input.nactiAres");

	   parent.find('input').prop('disabled', true);
	   
	   
	   icInput.after('<span class="fromAres">Načítám data ze systému ARES...</span>');
		
		
	   jQuery.support.cors = true; 
		
	   $.ajax({
		    url: '/hosting/ares',
		    contentType: "application/json",
		    dataType: "json", 
		    data: { ico: ic},
		    cache: false,
		    crossDomain: true,
		    success: function(data) {
		    	if (data && data.stav == 'ok') 
		    	{
					parent.find('input[name*=dic]').val(data.dic);
					if (data.firma) 
					{
						parent.find('input[name*=obchodni]').val(data.firma);
					}
					if (data.jmeno) 
					{
						parent.find('input[name*=jmeno]').val(data.jmeno);
						parent.find('input[name*=obchodni]').val('');
					}
					parent.find('input[name*=ulice]').val(data.ulice);
					parent.find('input[name*=mesto]').val(data.mesto);
					parent.find('input[name*=psc]').val(data.psc);
					$("span.fromAres").text("Hotovo").css('background-image','none').delay(2000).fadeOut();
					
		    	} 
		    	else 
		    	{
		      		$("span.fromAres").text("Podle tohoto IČ se nepodařilo načíst žádná data.").css('background-image','none').delay(2000).fadeOut();
		     	}
		     	//console.log(data);
		    },
		    complete: function() {
		    	parent.find('input').prop('disabled', false);
		    	$("span.fromAres").css('background-image','none').delay(2000).fadeOut();
		    },
		    error:function (xhr, ajaxOptions, thrownError){
                    /*alert(xhr.status);
                    alert(thrownError);
                    alert(xhr.statusText);
                    alert(xhr.responseText);*/
            }    
	   });	

	});		
		
	$('input[type=text]').tipTip({ 
		maxWidth: 300, 
		edgeOffset: 5, 
		defaultPosition: 'right', 
		delay: 50, 
		activation: 'focus'
	});
	
	// objednavka bar animation
	
	$('div.progress div.bar').delay(100).animate({'width': $('input.progressValue').val() + '%'}, 'slow');


	// flashes
	
	
	if ($(window).scrollTop() > 50 && $("div.flashes div.flash").length > 0)
	{
		var dialog = $("div.dialogFlashes");
		dialog.show();
		dialog.find("div.dialogContent").html('');
		$("div.flashes").children().detach().prependTo(dialog.find("div.dialogContent"));
		dialog.center();
	}

 };
 
 
jQuery.fn.center = function () {
    //this.css("position","absolute");
    this.css("top", Math.max((($(window).height() - this.outerHeight()) / 2),  20) + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

