/* CONSTANTS */
var FLASH_VERSION_SUPPORTED = '10', FLASH_VIDEO_VERSION_SUPPORTED = '9', _swfTracker = function(value){};

/* UTILITIES */
var PGUtil = {
	basePath:  '/en/',
	isIE:  $.browser.msie,
	isIE6: $.browser.msie && (parseInt($.browser.version) === 6),
	isFF2: $.browser.mozilla && (parseFloat($.browser.version) < 1.9),
	hasFlash: typeof(swfobject) !== 'undefined' && swfobject.hasFlashPlayerVersion(FLASH_VERSION_SUPPORTED),
	trackPage: function (url) {
		try { pageTracker._trackPageview(url); } catch(err) {}	
		return;
	},
	trackEvent: function (category, action, label) {
		try { pageTracker._trackEvent(category, action, label); } catch(err) {}	
		return;
	},
	loadJS: function () {
		for (var i = 0; i < arguments.length; i++){
			$('<script />').attr({ 'type': 'text/javascript', 'src': arguments[i] }).appendTo('head');
		}
	},
	loadCSS: function () {
		for (var i = 0; i < arguments.length; i++){
			$('<link />').attr({ 'type': 'text/css', 'rel': 'stylesheet', 'href': arguments[i] }).appendTo('head');
		}
	},
	trigger: function (linkId) {
		$('#'+linkId).trigger('click');
	},
	embedFlash: function(containerId, swf, width, height, flashvars, params, attributes) {
		//allow overlay calls to get through (videos use Flash 9)
		var isVideoOverlay = containerId === 'overlay-video';
		if (!PGUtil.hasFlash && !isVideoOverlay) {
			return;
		}
		//check version (allows for overlay videos which use Flash 9)	
		var _version = isVideoOverlay ? FLASH_VIDEO_VERSION_SUPPORTED : FLASH_VERSION_SUPPORTED;			
		
		//jigger the size for brands landing grids
		if(containerId === 'brands-content') {
			height = 500 + (Math.ceil($('.brand-group li').length/4) * 36);
		}
		
		//check and set vars
		var	fvars = flashvars;
		var	fparams = params || {};
			fparams.wmode = 'transparent';
		var	fattrs = attributes || {};
		
		//prevent load flicker
		if (containerId === 'lead') {
			$('#lead').css({'width': width+'px','height': height+'px'})
		}
		
      	//build wrappers
		var flashId = 'flash-'+ Math.floor(Math.random()*100000);
		var flashContainer = $('<div class="flash_"></div>').append('<div id="'+ flashId + '" />');
	   	$('#'+containerId)
	   		.addClass('containsFlash_')
	   		.wrapInner('<div class="noflash_"></div>')
	   		.prepend(flashContainer);
       	//embed it
	   	swfobject.embedSWF(swf, flashId, width, height, _version, null, fvars, fparams, fattrs);
	},
	embedWorldwideMapFlash: function(containerId, swf, width, height, flashvars, params, attributes) {
		//load JS for ajax requests
		PGUtil.loadJS(PGUtil.basePath + '_scripts/lib/swfaddress.js');
		var regions = ['asia', 'ce_europe_mideast_africa', 'latin_america', 'north_america', 'western_europe'];
		var start = '';
		$.each(regions, function() {
			if($('body').hasClass(this)) {
				start = this;
			}
		});
		var fvars = flashvars;
			fvars.start =  start;
		this.embedFlash(containerId, swf, width, height, fvars, params, attributes);
	}
};


$(document).ready(function(){
	//Run the JS styling on Select Boxes
	$('.select').sSelect();

	//open external links in new window
	$('a.external').attr('target','_blank');


	/*************Call Center Dropdown**************/
	//Reset for page reloads
	$('#call-'+$('#call-center-select').val()).show();

	//When the Call Center Drop down is Selected
	$('#call-center-select').change(function(){
		$('.call-center-info').hide();
		$('#call-'+$(this).val()).show();
	});

	//Popup for the Invoicing Page
	$("#country-specific-invoicing").change(function(){
		if ($(this).val()!='') {
			popup($(this).val(),'pgpop');
		}
	});
	$("a.popup").click(function(){
		popup($(this).attr('href'),'pgpop');
		return false;
	});

	
	//validation
	//m06	
	$('#submitter-portal-login').bind('submit',function() {
		var $form = $(this),
			ok = true,
			msg = {
				'username':'Please enter your username',
				'password':'Please enter your password'
			};
		//hide previous errors
		$('.error', $form).hide();
		//go through
		$form.find('input.text').each(function() {
			var $el = $(this),
				val = $el.val(),
				valid = !!val;	
			//set ok
			if(ok) {
				ok = valid;
			}		
			//handle error
			if(!valid) {
				var $err = $el.next('.error');
				if(!$err.length) {
					$('<span />').addClass('error').html(msg[$el.attr('id')]).insertAfter($el)
				} else {
					$err.show();
				}
			}
		});
		return ok;
	});
		
	//m07
	$('#portal-login').bind('submit',function() {
		var $form = $(this),
			ok = true,
			msg = {
				'username':'Please enter your username',
				'password':'Please enter your password'
			};
		//hide previous errors
		$('.error', $form).hide();
		//go through
		$form.find('input.text').each(function() {
			var $el = $(this),
				val = $el.val(),
				valid = !!val;	
			//set ok
			if(ok) {
				ok = valid;
			}		
			//handle error
			if(!valid) {
				var $err = $el.next('.error');
				if(!$err.length) {
					$('<span />').addClass('error').html(msg[$el.attr('id')]).insertAfter($el)
				} else {
					$err.show();
				}
			}
		});
		return ok;
	});
	
	/*$('#contact-form').bind('submit',function() {
		var $form = $(this),
			ok = true,
			msg = {
				'contact-name':'Please enter your name',
				'contact-email':'Please enter a valid email address',
				'contact-country':'Please select a country',
				'contact-topic':'Please select a topic',
				'contact-question':'Please enter a question or comment'
			} 
		//hide previous errors
		$('.error', $form).hide();
		//go through each
		$form.find('.required').each(function() {
			var $el = $(this),
				val = $el.val(),
				valid = true;				
			//test email
			if($el.attr('id') === 'contact-email') {
				var regexEmail = /^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$/;
				valid = regexEmail.test(val);
			} else {
				valid = !!val;
			}
			//set ok
			if(ok) {
				ok = valid;
			}
			//handle error
			if(!valid) {
				var $err = $el.siblings('.error');
				if(!$err.length) {
					$('<span />').addClass('error').html(msg[$el.attr('id')]).insertAfter($el)
				} else {
					$err.show();
				}
			}
		});
		return ok;
	}); 
	*/
	//set initial hide state
	$('div.expander > div').hide();	
	$('div.expander > .expander-header').addClass('expanding_');
	//toggle first tab on and switch header class.
	$('div.expander > .expander-header').click(function() {
		$(this).next().slideToggle('fast');
		//if an expanding class is on, turn it off, if it's off turn it on.
		$(this).toggleClass('expanding_');
		$(this).toggleClass('expanded_');
  });
	
	
	
});

function popup(mylink, windowname) {
	if (! window.focus)
		return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, windowname, 'width=680,height=550,scrollbars=yes');
	return false;
}

