/*
	Airfront vs StudioSkylab
	Philip Lawrence site js
*/

var $j = jQuery.noConflict();

var PLA = {
	
	querypaths : {
		denotesRequired : "span.required",
		contactForm : "#contactPla",
		cf_name_field : "#cf_name",
		cf_email_field : "#cf_email",
		cf_message_field : "#cf_message",
		contactFormSubmit : "#contactPlaSubmit",
		contactErrorMsgClass : "error-msg",
		contactErrorFieldClass : "error-field"
	},
	
	emailReg : /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/,
	
	jsLabels : {
		contactErrorMsg : "Ooops it appear you've missed out a field. Highlighted below."
	},
	
	soEmbed : function(passedConfigVals) {
		
		// default config vals
		var configVals = {
			theSWFid : "mpl",
			theSWF : "/mediaplayer/player-licensed.swf",
			theFile : "/home/mediafeed",
			autostart : false,
			cbar : 'over',
			targetDiv : "mediaplayer",
			swfWidth : "920",
			swfHeight : "346",
			swfVersion : "9",
			allowFullScreen : 'true',
			allowScriptAccess : 'always',
			wmode : 'opaque',
			playListSize : '300',
			displayClick : 'link',
			playListPos : 'right',
			smoothing : 'true',	//true,false
			stretching : 'uniform', // uniform, exactfit, none, fill
			backcolor : '251b3c',
			frontcolor : 'ffffff',
			lightcolor : '664b98',
			abouttext : "PLAnet",
			aboutlink: "/about/"
		};

		// override the defaults if necessary 
		for (var argName in passedConfigVals) {
			configVals[argName] = passedConfigVals[argName];
		}
		
		var flashInstalled = deconcept.SWFObjectUtil.getPlayerVersion();
		
		// set up the SWFObject and apply our configVals
		var so = new SWFObject(configVals.theSWF,configVals.theSWFid,configVals.swfWidth,configVals.swfHeight,configVals.swfVersion,configVals.smoothing,configVals.stretching,configVals.backcolor,configVals.frontcolor,configVals.lightcolor, configVals.abouttext, configVals.aboutlink);
		so.addParam('allowfullscreen',configVals.allowFullScreen);
		so.addParam('allowscriptaccess',configVals.allowScriptAccess);
		so.addParam('wmode',configVals.wmode);
		so.addVariable('playlistsize', configVals.playListSize);
		so.addVariable('file', configVals.theFile);
		so.addVariable('playlist', configVals.playListPos);
		so.addVariable('displayclick', configVals.displayClick);
		//so.addVariable('linktarget', '_blank');
		so.addVariable('controlbar', configVals.cbar);
		so.addVariable("autostart", configVals.autostart);
		so.addVariable("stretching", configVals.stretching);
		so.addVariable("backcolor", configVals.backcolor);
		so.addVariable("frontcolor", configVals.frontcolor);
		so.addVariable("lightcolor", configVals.lightcolor);
		so.addVariable("smoothing", configVals.smoothing);
		so.addVariable("abouttext", configVals.abouttext);
		so.addVariable("aboutlink", configVals.aboutlink)
	
		if (flashInstalled['major'] == 0) {
			$j("#" + configVals.targetDiv).html('<p class="inc140">It appears you have JavaScript Enabled but not Adobe Flash Player. Please install Adobe Flash Player</p>')
		} else {
			so.write(configVals.targetDiv);
		}
		
	},
	
	_contactHandler : function () {
		
		if($j(PLA.querypaths.contactForm).length){
			
			var cf_NameVal, cf_EmailVal, cf_MessageVal;
			
			$j(PLA.querypaths.contactFormSubmit).click(function(e){
				
				var formAction = $j(PLA.querypaths.contactForm).attr("title");
				
				hasError = false;
				$j("." + PLA.querypaths.contactErrorMsgClass).remove();
				
				j_cf_Name = $j(PLA.querypaths.cf_name_field);
				j_cf_Email = $j(PLA.querypaths.cf_email_field);
				j_cf_Message = $j(PLA.querypaths.cf_message_field);
								
				cf_NameVal = j_cf_Name.val();
				cf_EmailVal = j_cf_Email.val();
				cf_MessageVal = j_cf_Message.val();
					
				$j(PLA.querypaths.contactForm).prepend('<div class="' + PLA.querypaths.contactErrorMsgClass +'"><p>'+ PLA.jsLabels.contactErrorMsg + '</p></div>');
				
				if (cf_NameVal == "") {
					hasError = true;
					j_cf_Name.addClass(PLA.querypaths.contactErrorFieldClass);
				} else {
					j_cf_Name.removeClass(PLA.querypaths.contactErrorFieldClass);
				}
				
				if (!PLA.emailReg.test(cf_EmailVal)) {
					hasError = true;
					$j("." + PLA.querypaths.contactErrorMsgClass).append("<br /><p>Please enter a valid email address</p>");
					j_cf_Email.addClass(PLA.querypaths.contactErrorFieldClass);
				} else if (cf_EmailVal == "") {
					hasError = true;
					j_cf_Email.addClass(PLA.querypaths.contactErrorFieldClass);
				} else {
					j_cf_Email.removeClass(PLA.querypaths.contactErrorFieldClass);
				}
				
				if (cf_MessageVal == "") {
					hasError = true;
					j_cf_Message.addClass(PLA.querypaths.contactErrorFieldClass);
				} else {
					j_cf_Message.removeClass(PLA.querypaths.contactErrorFieldClass);
				}
				
				if(hasError == true) {
					e.preventDefault();
					$j("." + PLA.querypaths.contactErrorMsgClass, $j(PLA.querypaths.contactForm)).fadeIn("slow");
				} else {
					$j("." + PLA.querypaths.contactErrorMsgClass, $j(PLA.querypaths.contactForm)).html("<p>Thank you! Redirecting</p>");
					$j(".frow", $j(PLA.querypaths.contactForm)).hide();
					$j("." + PLA.querypaths.contactErrorMsgClass, $j(PLA.querypaths.contactForm)).fadeIn("normal");
					return true;
				}
			})
			
		}
		
	},
	
	_init : function() {
		
		var self = this;
		// make a few things nice with js
		$j("hr").replaceWith('<div class="hr"></div>');
		$j(".col-a .article:last-child").addClass("last-article");
		
		$j(PLA.querypaths.denotesRequired).text("*");
		
		PLA._contactHandler();
		
	}
	
}

$j(document).ready(function(){
	PLA._init();
});

