/**
 * Common Class
 * By: Miguel Chumillas (miguel[at]serverstone.com / (+34) 91 280 33 70)
 */

// Initiate cufon before document is ready for IE reasons
Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('h3');
Cufon.replace('.home-layout h4');
Cufon.replace('.submenu a', { hover: true });
Cufon.replace('.menu a', { hover: true });
Cufon.replace('.menu-locations a', { hover: true });

var common_class = new Common();

function Common() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};

Common.prototype.build = function() {
	var _l0 = this;
	_l0.is_mobile = false;
	
	// Add alpha background to teaser
	if ($('.teaser img', _l0.doc).length > 0 && $('.teaser .announcement', _l0.doc).length <= 0) {
		$('.teaser', _l0.doc).prepend('<div class="background" style="filter:alpha(opacity=50)!important; opacity:0.5;">&nbsp;</div>');
	} else
	if ($('.teaser .announcement', _l0.doc).length <= 0) { // Announcement just visible on homepage
		$('.teaser', _l0.doc).prepend('<img class="empty" src="/images/empty.gif" width="60" height="60" alt="" />'); // IE fix
	}
	if ($('.teaser .announcement', _l0.doc).length > 0) {
		$('.teaser .announcement', _l0.doc).prepend('<div class="background" style="filter:alpha(opacity=50)!important; opacity:0.5;">&nbsp;</div>');
	}
	
	// Check if the search box is empty and if so clean it up onclick
	$('#global-search-value', _l0.doc).mouseup(function() {
		if (!$.trim($(this).val()) || $.trim($(this).val()) == 'Search') {
			$(this).val('');
		}
	});
	
	// Arrow and pdf links: we add a custom span class rather than using padding for the sake of IE6
	$('.simple-arrow-link').each(function() {
		$(this).wrap('<div class="div-simple-arrow-link"><span></span></div>');
	});
	$('.arrow-link').each(function() {
		var new_arrow = $(this).wrap('<div class="div-arrow-link"><span></span></div>');
		new_arrow.ready(function() {
			$('.div-arrow-link').mouseover(function() { // We don't need special hover effect on the following modules
				if (['texts-links-list'].indexOf($(this).parent().attr('class')) >= 0) {
					$(this).parent().css('background-color', '#E5E9EB');
				}
			});
			$('.div-arrow-link').mouseout(function() {
				if (['texts-links-list'].indexOf($(this).parent().attr('class')) >= 0) {
					$(this).parent().css('background', '#FFFFFF');
				}
			});
		});
	});
	$('.hover').mouseover(function() {
		if (['rich-content', 'announcement'].indexOf($(this).parent().attr('class')) < 0) {
			$(this).parent().css('background-color', '#E5E9EB');
			if (['texts-links-list'].indexOf($(this).parent().parent().parent().attr('class')) >= 0) {
				$('h3', $(this).parent().parent().parent()).css('background-color', '#E5E9EB');
			}
		}
	});
	$('.hover').mouseout(function() {
		if (['rich-content', 'announcement'].indexOf($(this).parent().attr('class')) < 0) {
			$(this).parent().css('background', 'transparent');
			if (['texts-links-list'].indexOf($(this).parent().parent().parent().attr('class')) >= 0) {
				$('h3', $(this).parent().parent().parent()).css('background-color', 'transparent');
			}
		}
	});
	$('.pdf-link').wrap('<div class="pdf-link"><span></span></div>');
	
	// News and opinion: Fix height on single opinion boxes (in case they don't have the same height for the sake of the hover effect)
	$('.news-and-opinion-layout .bottom .column').each(function() {
		var _offset_height = $('.news-and-opinion-layout .bottom .col1').height() - $(this).height();
		if (_offset_height > 0) {
			$('.single-opinion .hover', this).last().height($('.single-opinion', this).last().height() + _offset_height);
		}
	});
	
	// Contact locator: Fix height on column boxes (in case they don't have the same height for the sake of the hover effect)
	var cl_max_column_height = 0;
	$('.generic-layout.four-cols .rich-content .column').each(function(){
		clThisHeight = $(this).height();
		if( clThisHeight > cl_max_column_height)
			cl_max_column_height = clThisHeight;
	});
	// set each items height to use the tallest value found
	$('.generic-layout.four-cols .rich-content .column').each(function(){
		$(this).height(cl_max_column_height);
	});
	
	// dropdown-extended: hover effect as the header is stretched
	$('.dropdown-extended div span').mouseover(function() {
		$('ul', $(this).parent().parent()).css('visibility', 'visible');
	});
	$('.dropdown-extended div span').mouseout(function() {
		$('ul', $(this).parent().parent()).css('visibility', 'hidden');
	});
	$('.dropdown-extended ul ul').mouseover(function() {
		$(this).css('visibility', 'visible');
	});
	$('.dropdown-extended ul ul').mouseout(function() {
		$(this).css('visibility', 'hidden');
	});
	
	// Detect mobile devices
	if (_l0.isMobileClient(navigator.userAgent)) {
		_l0.is_mobile = true;
	}
	
	// Add video player if any videos found
	if ($('.video', _l0.doc).length > 0) {
		try {
			$('.video', _l0.doc).each(function() {
				$(this).empty();
				if (_l0.is_mobile == true) {
					$(this).append('<video controls autoplay src="'+$(this).attr('href')+'" width="520" height="293">');
				} else {
					var old_id = $(this).attr('id');
					$(this).append('<div id="' + old_id + '"></div>');
					$(this).removeAttr('id');
					var script1   = document.createElement('script');
					script1.type  = 'text/javascript';
					script1.text  = 'var flashvars = {src:\"' + $(this).attr('href') + '&mode=side-by-side&backgroundColor=000000&scaleMode=scale&themeColor=4c7eb4&frameColor=000000&fontColor=4c7eb4&autostart=true&hideControlBar=false&hideFrameBorder=false\",link: \"\",embed: \"\"}; var params = {allowFullScreen: \"true\"}; var attributes = {id: \"' + old_id + '\",name: \"' + old_id + '\"}; swfobject.embedSWF(\"/swf/custom-video-player.swf\", \"' + old_id + '\", \"520px\", \"293px\", \"10.0.0\", \"expressInstall.swf\", flashvars, params, attributes);';
					document.body.appendChild(script1);
				}
				$(this).removeAttr('href');
			});
		} catch(e) {
			// Throw error...
		}
	}
};

Common.prototype.mobileClients = [
	"midp",
	"240x320",
	"blackberry",
	"netfront",
	"nokia",
	"panasonic",
	"portalmmm",
	"sharp",
	"sie-",
	"sonyericsson",
	"symbian",
	"windows ce",
	"benq",
	"mda",
	"mot-",
	"opera mini",
	"philips",
	"pocket pc",
	"sagem",
	"samsung",
	"sda",
	"sgh-",
	"vodafone",
	"xda",
	"iphone",
	"android",
	"ipad"
];

Common.prototype.isMobileClient = function(userAgent) {
	userAgent = userAgent.toLowerCase();
	for (var i in this.mobileClients) {
		if (userAgent.indexOf(this.mobileClients[i]) != -1) {
			return true;
		}
	}
    return false;
};
