$(document).ready(function(){
    bind_article_contact_form ();
    bind_comments_seolab();
    bind_clanek2_path ();
    bind_clanek2_header ();
    bind_clanek2_banner ();
    bind_reference ();
    bind_content ();
    bind_hover ();
    bind_search ();
    bind_news ();
    bind_banner ();
    //	bind_cufon ();
    bind_gallery_overlay ();
    bind_table ();
    bind_path ();
    bind_animate_box3 ();
    bind_colorbox();
    bind_cycle();
    bind_requestform();
    bind_panel();
    //	bind_targets();
    //	bind_modernizr();
    $("#bottom-print").click(function(){
        window.print();
        return false;
    })
});

function bind_article_contact_form ()
{
    if ($('#article-contact').length > 0) {
        $('#article-contact form').submit(function() {
            var isAble = true;
            $(this).find('input[type="text"]').each(function() {
                if ($(this).val() == "") isAble = false;
            });
            if ($(this).find('textarea').val() == "") isAble = false;
            
            if (!isAble) alert('Je zapotřebí vyplnit všechny údaje.');
            return isAble;
        });
    }
}

function bind_comments_seolab ()
{
    var x = $('.commentlist .comment').length;
    $('.comments-number').text('Počet komentářů: ' + x);
}

function bind_clanek2_path ()
{
    $('#path-clanek2').find('li:eq(1)').hide();
}

function bind_clanek2_header ()
{
    var str = $('.seolabh1').text();
    if (str) {
        var index = str.indexOf(" ");
        var str2 = "";
        
        if (index > 0) {
            var str2 = str.substr(index);
            str = str.substring(0, index);
        }
        
        $('.seolabh1').html("<strong>" + str + "</strong>" + str2);
    }
}

function bind_clanek2_banner ()
{
    var str = $('#image-clanek2 img').attr('src');
    if (str) {
        var index = str.lastIndexOf('/');
        var str2 = str.substr(index);
        str = str.substr(0, index);
        str2 = str2.replace(/-/g, '_');
        $('#image-clanek2 img').attr('src', str + str2);
    }
}

function bind_reference ()
{
    $("#path li.depth1:contains('Reference') + li").parent().parent().parent().find(".reference-loga2").hide();
    $("#path li.depth1:contains('Reference') + li").parent().parent().parent().find(".reference-zakaznici-wrap").hide();

}

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").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");}
	);
	
	$(".b-block .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 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 ()
{
	$('#b-block .sub .items').cycle({
    	fx: 'fade',
    	speed:    1200, 
    	timeout:  5000/*,
    	nowrap: 1,
    	pause:  0 */
	});
	
	$('#b-block .sub .cycletext').cycle({
    	fx: 'fade',
    	speed:    2000, 
    	timeout:  7000/*,
    	nowrap: 1,
    	pause:  0 */
	});
}

/**
 * graficky font cufon
 **/
function bind_cufon ()
{
	Cufon.replace(".b-block 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(/pagio.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();
			
		//});
	});	
} 
