$(document).ready(function() {
	
	//fit horizental menu
	fitMenuItems('#menuMain');
	
	//transform emails
	parseEmailAddressToLink('span.peatl');
	
	// index functions - see index.js
	if($('body').hasClass('index')) {
		makeBoxClickable('.indexBottom .indexBox');
		insureSelectInit('#insureSelect');
	}

	// insure item pop hover effect
	if($('#insureHolder').size()>0) {			
		insureImagesInit('#insureHolder .insureList');		
		if ($.browser.msie) {
			$('#insureHolder .insureList img').attr('alt', '');
		}		
	}
	// faq show/hide
	if($('.faqItem').size()>0) {
		initShowHide('.faqItem', '.q', '.a');
		initShowHide('.faqItem .infobox', '.infoboxLabel', '.infoboxContent');
		
		if (location.hash != "") {
			// fix page scroll offset after hididing contents
			location.hash = location.hash;
			//open item from address hash
			$('#'+location.hash.substr(1)+" .a").show();
		}
	}
			
	// faq show/hide
	if($('#rnvNavod').size()>0) {
		initShowHide('#rnvNavod', '.rnvNavodLink', '.rnvNavodContent');
	}			
});

/** Main menu exact fit **/
function fitMenuItems(selector) {
	
	var menu = $(selector);
	var menuWidth = menu.width();
	var li = menu.find('li');
	var liWidth = 0;
	li.each(function () {
		liWidth += $(this).outerWidth();
	});
	
	var plusWidth = Math.floor((menuWidth - liWidth)/li.size())
	var restWidth = (menuWidth-liWidth) % li.size();

	li.find('a span').each(function () {
		$(this).width($(this).width()+plusWidth);	
		if(restWidth>0) {
			$(this).width($(this).width()+1);		
			restWidth--;
		}			
	});
}

/**
* Show/Hide funtion
* @holder:String - selector
*	@button:String - selector inside holder
*	@content:String - selector inside holder
*
**/
function initShowHide(selHolder, selButton, selContent) {
	var holder = $(selHolder);

	holder.each(function() {
		var button = $(this).find(selButton);	
		var content = $(this).find(selContent);		
		var hash = $(this).attr('id');
		
		content.hide();
		
		button.click(function() {
			// if(hash != "") location.hash = hash; - posouva stranku
			content.stop(true, true).slideToggle(250);
			return false;
		});
			
	});
}

/** 
* insure image+menu pop 
*
**/
function insureImagesInit(selector){
	var holder = $(selector);
	var items = holder.children('li');

	items.mouseenter(function() {
		$('#insureItemHover').remove();		
		// hide selectbox if is opened
		$('#insureSelect .selectOptions').hide();
		$('#insureSelect .selectBox').removeClass('selectBoxOpen');
		appendInsureImageItem(this);
	});

}

function appendInsureImageItem(insureItem) {

	var insureItem = $(insureItem);
	var popHolder = $('<div id="insureItemHover"><div class="wrap">'+insureItem.html()+'</div></div>');
	
	if($('body').hasClass('index')) {
		// horizontal
		popHolder.css({
			left: insureItem.offset().left-6, // padding 4px; width: +2
			top: insureItem.offset().top-5,
			width: insureItem.width()+4,		
			display: 'none'
		});
		popHolder.addClass('popHorizontal');
		
		var strlen = popHolder.find('a.portal').text().length;		
		var popHolderContent = popHolder.find('.content');
		if(strlen>20 && strlen<=30) {
			popHolderContent.width(150);
		} else
		if(strlen>30) {
			popHolderContent.width(181);			
		}
		
	} else {
		// vertical
		popHolder.css({
			left: insureItem.offset().left-4, // padding 4px; width: +2
			top: insureItem.offset().top-4,
			width: 202,
			display: 'none'
		});		
		popHolder.addClass('popVertical');		
		if ($.browser.msie) {		
			var popHolderContent = popHolder.find('.content');
			popHolderContent.width(popHolder.find('a.portal').text().length + 'ex');
		}
	}
	
	
	$('body').append(popHolder);
	if ($.browser.msie) {
		popHolder.show();
	} else {
		popHolder.fadeIn(250);
	}
	popHolder.mouseleave(function() {
		popHolder.remove();
	});
}

/** 
* parse emails from format 
* @selector:String
*
* from:	<selector>
					<span class="pea1">portalservis</span>
					<span class="pea2">seznam</span>
					<span class="pea3">cz</span>
				</selector>
* to:	  <a href="mailto:portalservis@seznam.cz">portalservis@seznam.cz</a>
**/

function parseEmailAddressToLink(selector) {
	var items = $(selector);
	items.hide();

	items.each(function() {
		var address = $(this).children('.pea1').text()+'@'+$(this).children('.pea2').text()+'.'+$(this).children('.pea3').text();
		var itemLink = $('<a href="mailto:'+address+'">'+address+'</a>'); 									
		$(this).after(itemLink);								 

	});
	
	items.remove();
}


