/* global MailUp scripts */
function countLines(obj) {
	el = obj;
    var divHeight = el.height();
    var lineHeight = parseInt(el.css('line-height'));
    var lines = divHeight / lineHeight;
    return lines;
}

function emailencode() {
	jQuery('a[href*="*"]').each(function() {
		jh=jQuery(this).attr('href');
		if(jQuery(this).text() == "") {
			jQuery(this).attr('href', "mailto:"+jh.replace("*", "@")).text(jh.replace("*", "@"));
		} else {
			jQuery(this).attr('href', "mailto:"+jh.replace("*", "@"));
		}
	});
}
	
jQuery(document).ready(function() {
	
	// email encoding
	emailencode();
	
	// tooltips
	jQuery('form img[title]').qtip({ style: { tip: true, classes: "ui-tooltip-dark", width: 520 }});
	jQuery('.nice-tooltip').qtip({ style: { tip: true, classes: "ui-tooltip-dark", width: 520 }});
	jQuery('a.link-locked').qtip({content: "Per accedere a questa risorsa devi essere registrato.", position: {	 adjust: { x: 24, y: 24 },	my: "top left",	at: "top left"	},	style: {tip: true,	classes: "ui-tooltip-dark"	}});
	jQuery('span.mailup-option-active').qtip({	style: {tip: true,classes: "ui-tooltip-dark"},position: {adjust: {x: -8,y: 2}}});
	
	
	// toggle hidden blocks
	jQuery('.box-collapsable').find('h3').click(function() {		
		if(jQuery(this).next().css('display')=="none") {
			jQuery(this).next().css({'display':'block'});
		} else {
			jQuery(this).next().css({'display':'none'});
		}
	});	
	
	// toggle hidden tables 
	jQuery('.collapsable-table').find('thead').click(function() {	
		if(jQuery(this).next().find('td,th').css('display')=="none") {
			if (jQuery.browser.msie && jQuery.browser.version ==7) { 
				jQuery(this).next().find('td,th').css({'display':'block'});
			} else {				
				jQuery(this).next().find('td,th').css({'display':'table-cell'});
			}
			jQuery(this).parent().removeClass('collapsed');			
		} else {
			jQuery(this).next().find('td,th').css({'display':'none'});
			jQuery(this).parent().addClass('collapsed');
		}			
	});
	
	if(jQuery('#pcMainArea10-LEFT').length) {
		jQuery('#pcMainArea10').addClass('pcMainArea10-twoCols');
	}
	
	// lightbox
	if($.fancybox) {	
		jQuery('a.video-lightbox').fancybox({type: 'iframe', overlayColor: '#000', width: 690, height: 520});
		jQuery('a.newsletter-lightbox').fancybox({overlayColor: '#000'});
		jQuery('a.privacy-lightbox').fancybox({type: 'iframe',overlayColor: '#000', width: 820, height: 520, autoDimensions: false, centerOnScroll: true});
		jQuery('a.map-lightbox').fancybox({type: 'iframe', overlayColor: '#000', width: 700, height: 520, autoDimensions: false});
		jQuery('a.image-lightbox').fancybox({overlayColor: '#000', titlePosition: 'inside'});
		jQuery('a.home-video-lightbox').fancybox({type: 'iframe', overlayColor: '#000',scrolling: 'no', width: 801, height: 550});
				
		jQuery('span.video-overlay').remove();		
		jQuery('a.video-lightbox').append('<span class="video-overlay"></span>');
		jQuery('a.video-lightbox img').each(function() {
			if(jQuery(this).size()) {
				jQuery(this).parents('a').find('.video-overlay').width(jQuery(this).width()).height(jQuery(this).height()).css({'opacity': '0.2', 'background-color' : '#000'});
			}
		});
		jQuery('a.video-lightbox').hover(
			function() { jQuery(this).find('.video-overlay').css({'opacity': '0.5', 'background-color' : '#000'}); }, 
			function() { 
						jQuery(this).find('.video-overlay').css({'opacity': '0.2', 'background-color' : '#000'});
			});
	}
	// adjust styles	
	jQuery(".half-column").find("h3").each(function() { if(jQuery(this).outerHeight() < 36 && (jQuery(this).next().hasClass('box-icon-text') || jQuery(this).parent().next().hasClass('box-icon-text'))) { jQuery(this).css({ 'margin-top': '28px' }); } else { jQuery(this).css({ 'margin-top': '10px' }); } });
	if(jQuery(".PageTitle").outerHeight() < 70) {jQuery('.PageTitle').css({ 'margin-top': '18px' });}
	//if(jQuery('#headerCart').length) {jQuery('body').css({'background-position':'0 41px'}); }
	
	jQuery('.option-opzioni-comuni, .option-opzioni-avanzate, .option-assistenza').parent().css({ 'background-color':'#fff' });
	jQuery('.PrdAddChargesContent').find('input[name="add"]').attr('src', 'images/sample/IT/pc_button_continue.gif');
	if(jQuery("#custprefPostit p").outerHeight() > 290) {jQuery('#custprefPostit').css({ 'background-image': 'url(images/mailup2011/postit_custpref2.png)' }).height(350);}
	
	// adjust columns' height	
	function adjustColsHeight() {
		if(jQuery('#pcMainArea10-LEFT').height() < jQuery('#pcMainArea10-PC').height()) {
			jQuery('#pcMainArea10-LEFT').height(jQuery('#pcMainArea10-PC').height());
		} else {
			jQuery('#pcMainArea10-PC').height(jQuery('#pcMainArea10-LEFT').height());
		}
	}	
	jQuery('a[href="javascript:Recaptcha.reload()"]').addClass('reloadcaptchalink').css({'position':'relative'}).append("<span class='reloadcaptcha'>Create new code</span>");
	jQuery('a[href="javascript:Recaptcha.reload()"]').click(function() { _gaq.push(['_trackPageview', 'clic-reload-captcha']); });
});


