$(document).ready(function(){
	bind_content ();
	bind_hover ();
	bind_search ();
	bind_news ();
	bind_banner ();
	bind_idea ();
	bind_gallery_overlay ();
	bind_table ();
	bind_path ();
	bind_animate_box3 ();
	bind_colorbox();
	bind_cycle();
	bind_requestform();
	bind_panel();
	bind_tabs();
//	bind_cufon ();
//	bind_targets();
//	bind_modernizr();
    bind_homepage_reference ();
    bind_form_poptavka ();
    bind_home_konfigurator ();
});

// upravi formular tak aby obsahoval dva sloupce
function bind_form_poptavka ()
{
    $('.formcontent div').slice(6,11).wrapAll("<div class='fl form-poptavka-col-second'>");
    $('.formcontent div').slice(0,6).wrapAll("<div class='fl form-poptavka-col-first'>");
}

function bind_homepage_reference ()
{
    $(".firstcol:eq(1)").addClass("lastcol");
    $(".firstcol:eq(1)").removeClass("firstcol");
    
    $(".homepage-reference-text").each(function(){
        if ($(this).height() > 120) $(this).addClass("homepage-reference-text-short");
    });
}

function bind_tabs ()
{
	// Tabs
	$('#tabs').tabs();
	
	//hover states on the static widgets
	$('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
	//vertical
	$("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
    $("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');
}


function bind_animate_box3 ()
{
	$('.animate>div').hoverIntent(
		function(){
			$('.animate>div').removeClass('sfhover');
			$(this).addClass('sfhover');
			$('.animate>div').each(function(){
				if (!$(this).hasClass('sfhover')) {
					$(this).animate({opacity:0.3},300);
				}
			});
		},function(){
			$('.animate>div').removeClass('sfhover').animate({opacity:1},100);
		}
	);
}


function bind_content ()
{
	$('.content .article:first').addClass('firstarticle');

	$('.reference .col:not(:has(img))').addClass('nologo');

	$('.reference .col ul').addClass('row2');

	var i = 0;
	$('.reference .col').each(function(){
		if (i % 3 == 0) {
			$(this).addClass('firstcol');
		}
		if (i % 3 == 2) {
			$(this).addClass('lastcol').after('<div class="cb"></div>');
		}
		i = i + 1;		
	});
}

/**
 * hover efekt na menu, tlacitka v paticce a tlacitka plus v cycle banneru
 **/
function bind_hover ()
{
	$(".menu li, .path li, .presentation-link, .gallery .image, .preview-study, .preview-home").hover(
		function() {
			$(this).addClass("sfhover");
		},
		function() {
			$(this).removeClass("sfhover");
		}
	);
	
	$(".footer-service .ico-print").hover(
		function() {$(".footer-service .popup-print").addClass("sfhover");},
		function() {$(".footer-service .popup-print").removeClass("sfhover");}
	);
	$(".footer-service .ico-mail").hover(
		function() {$(".footer-service .popup-mail").addClass("sfhover");},
		function() {$(".footer-service .popup-mail").removeClass("sfhover");}
	);
	$(".footer-service .ico-pdf").hover(
		function() {$(".footer-service .popup-pdf").addClass("sfhover");},
		function() {$(".footer-service .popup-pdf").removeClass("sfhover");}
	);
	$(".footer-service .ico-share").hover(
		function() {$(".footer-service .popup-share").addClass("sfhover");},
		function() {$(".footer-service .popup-share").removeClass("sfhover");}
	);
	
	$(".banner .item .plus .button").hover(
		function() {$(this).parent().addClass("sfhover");},
		function() {$(this).parent().removeClass("sfhover");}
	);

	$("a.media").hover(
		function() {$(this).addClass("sfhover");},
		function() {$(this).removeClass("sfhover");}
	);


}

/**
 * smazani textu v inputech
 **/
function bind_search ()
{
	// vynulovani pole pri focusu
	$(".clear-input").focus(function() {
		$(this).val('');
	});
}

/**
 * gallery overlay - oranzove oramovani a tlacitko plus
 * overlay pres obrazky v clanku 
 **/
function bind_gallery_overlay ()
{
	$(".gallery .image").prepend('<div class="overlay"></div>');
	$(".benefit .media").prepend('<div class="overlay"></div>');
	
	$(".content .article .preview-study").prepend('<div class="overlay"></div>');	
	$(".home .content .panel .preview-home").prepend('<div class="overlay"></div>');
	
	
	
	
	/*$(".content .article img.preview-right, .content .panel img.preview-right").before('<div class="overlay"></div>');*/	
}

/**
 * tabulka - liche a sude bunky
 **/
function bind_table ()
{
	var i=0;
	var j=0;
	$(".content table tr").each(function(){
		// radky
		if(i==1) {
			$(this).addClass("even");
			i=0;
		}
		else {
			$(this).addClass("odd");
			i++;
		}
		// sloupce
		j=0;
		$("td, th", this).each(function(){
			if(j==1) {
				$(this).addClass("even");
				j=0;
			}
			else {
				$(this).addClass("odd");
				j++;
			}
			// true a false
			if($(this).html() == "true") $(this).html('<img src="/img/image-true.png" alt="ano" title="ano" />');
			if($(this).html() == "false") $(this).html('<img src="/img/image-false.png" alt="ne" title="ne" />');
		});
	});
}

/**
 * rotujici novinky
 **/
function bind_news ()
{
	$('#news dd').cycle({ 
    	fx: 'scrollDown',
    	speed:    1000, 
    	timeout:  6000,
    	pause:  1 
	});
}

/**
 * cycle banner
 **/
function bind_banner ()
{
	$('#banner div.sub').cycle({ 
    	fx: 'fade',
    	speed:    1200, 
    	timeout:  6000,
    //	nowrap: 1,
    	pause:  1,
    	pager:  '#idea' 
	});
}

/**
 * idea tlacitka pro cycle banner
 **/
function bind_idea ()
{
	var i=1;
	$("#idea a").each(function(){
		$(this).addClass("item"+i);
		$(this).html("<span>" + $(this).html() + "</span>");
		i++;
	});
}

/**
 * graficky font cufon
 **/
function bind_cufon ()
{
	Cufon.replace(".banner h2");
	Cufon.replace(".content h1.title");
}

/**
 * path - posledni viditelny prvek
 **/
function bind_path ()
{
	$(".path ul li:visible:last").addClass("end");
}

/**
 *	Binduje colorbox tam, kde to neni resene jako galerie
 */
function bind_colorbox() {

	$('a.media').each(function () {
		$(this).colorbox();
	});
	
	$('.templates a').each(function () {
		$(this).colorbox();
	});

}


/**
 *	Spusti cycle jquery plugin
 *	napr.: http://beta.pagio.cz/tvorba-www-stranek/
 */
function bind_cycle() {

	$('.cycle').cycle({
		speed:500,
		timeout:4000,
		pause:1,
		nowrap:1,
		pager:'#cyclenav'
	});
}

/**
 *	Efekt u poptavkoveho formulare pouziteho na strance 
 *	http://beta.pagio.cz/tvorba-www-stranek/
 */
function bind_requestform()
{
	$('#requestbutton').click(function ()
	{
		var email		= $('input[name=email]').val();
		var company		= $('input[name=company]').val();
		var www			= $('input[name=www]').val();
		var phone		= $('input[name=phone]').val();
		var solution	= $('input[name=solution]').val();
		var values		= new Array();

		$.each($("input[@name='products[]']:checked"), function() {
			values.push($(this).val());
		});

		var data = 'form=1&solution='+solution+'&company='+company+'&www='+www+'&phone='+phone+'&email='+email+'&products[]='+values.join(', ');

		$.post(window.location.pathname, data,
			function(data) {
				$('.rthanks').slideDown();
				return false;
			}
		);

		return false;
	});
	
	$('#requestnew').click(function () {

		$('form.request').clearForm();
		$('.rthanks').slideUp();
		return false;
	});

	$('.pdata input[type=text]').each(function () {
		$(this).focus(function () {
			$(this).val("").css("color", "#000");
		});
	});
}

/**
 *	Prvnim panelum prida .firstpanel
 */
function bind_panel() {

	//$('.col dl.panel:visible:first').addClass('firstpanel');
	$('.col:not(.nodt) dl.panel:visible:first-child').each(function () { 
		$(this).addClass('firstpanel'); 
	});

}

/**
 *	Vymaze pouze textove inputy
 */
$.fn.clearForm = function() {
  // iterate each matching form
  return this.each(function() {
    // iterate the elements within the form
    $(':input', this).each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
    });
  });
};


/**
 *	Vsem odkazum, ktere v sobe nemaji pagio.cz a vedou na jine weby, nastavi target blank
 */
function bind_targets() {

	$('a').each(function () {
		if($(this).attr('href')) {	//osetri chybu pri prochazeni kotvy ve strance, ktera ma jen atribut name
			// pokud neobsahuje pagio.cz a zacina na http (tim se eliminvaly odkazy s lomitkem nebo otaznikem na zacatku)
			if( !$(this).attr('href').match(/evidio.cz/g) && $(this).attr('href').match(/^http/g) ) {
				$(this).attr('target', "_blank");
			}
		}
	})

}

function bind_modernizr() {

	Modernizr._fontfaceready(function(bool){

		// if @font-face isn't supported, you may want to employ Cufón
//		if (!bool) 
//			alert('bool');
		//getScript('cufon.withfont.min.js',function(){
			//Cufon.now();
			
		//});
	});	
} 

function bind_home_konfigurator () {
    $('.home-konfigurator').hover(
        function() {
            $(this).css('z-index','1');
            $(this).animate({
                height: '320px',
                top: '28px'
              }, 300);
        },
        function() {
            $(this).animate({
                height: '108px',
                top: '240px'
              }, 300,
            function() {$(this).css('z-index','-1');} );
        }
    );
}
