$(document).ready(function(){
	bind_content ();
	bind_hover ();
	bind_search ();
	bind_news ();
	bind_bn_item ();
	bind_idea ();
	bind_gallery_overlay ();
	bind_table ();
	bind_path ();
	bind_animate_box3 ();
	bind_lightbox();
	bind_cycle();
	bind_requestform();
	bind_panel();
	bind_targets();
});

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");}
	);
	
	$(".bn-item .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 ()
{

	if ($('#news .content').length) {
		$('#news .content').cycle({ 
			fx: 'scrollDown',
			speed:    1000, 
			timeout:  6000,
			pause:  1 
		});
	}
	
}

/**
 * cycle bn-item
 **/
function bind_bn_item ()
{
	if ($('#bn-item div.sub').length) {
		$('#bn-item div.sub').cycle({
			fx: 'fade',
			speed:    1200, 
			timeout:  6000,
			/*nowrap: 1,*/
			pause:  0,
			pager:  '#idea' 
		});
	}
}

/**
 * idea tlacitka pro cycle bn-item
 **/
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(".bn-item h2");
	Cufon.replace(".content h1.title");
}

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

function api_gallery_lightbox (o)
{
    $(o).lightBox({
        overlayBgColor: '#000',
        overlayBgImage: '',
        overlayOpacity: 0.6,
        imageLoading: '/plugins/gallery-lightbox/img-cs/lightbox-ico-loading.gif',
        imageBtnClose: '/plugins/gallery-lightbox/img-cs/lightbox-btn-close.gif',
        imageBtnPrev: '/plugins/gallery-lightbox/img-cs/lightbox-btn-prev.gif',
        imageBtnNext: '/plugins/gallery-lightbox/img-cs/lightbox-btn-next.gif',
        containerResizeSpeed: 350,
        txtImage: 'Obrázek',
        txtOf: '/'
    });
}

/**
 *	Binduje colorbox tam, kde to neni resene jako galerie
 */
function bind_lightbox()
{
    api_gallery_lightbox ('.gallery a');
    api_gallery_lightbox ('.lightbox');

/*    $('.gallery,.images').each(function(){
        $('a[rel=lightbox]', this).each(function(){
            api_gallery_lightbox (this);
        });
    });*/

	$('a.media').each(function () {
	    api_gallery_lightbox (this);
//		$(this).lightbox();
	});
	
//  $('.templates a').each(function () {
//      api_gallery_lightbox (this);
// //       $(this).lightbox();
//  });

}


/**
 *	Spusti cycle jquery plugin
 *	napr.: http://beta.pagio.cz/tvorba-www-stranek/
 */
function bind_cycle() {
	if ($('.cycle').length) {
		$('.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) .panel:visible:first-child').each(function () { 
 		$(this).addClass('firstpanel'); 
 	});
 	*/
 	
 	$('.col div.panel:visible:first-child').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();
			
		//});
	});	
} 
