jQuery.submitConnect = {
	options : {
		url : '',
		wwwroot: ''
	},
	
	init : function(options){
		if (options)
			jQuery.extend(jQuery.submitConnect.options, options);

		if (jQuery.browser.msie) {

		}
		
		var type = {'name':$("#name").val(),'email':$("#email").val(),'phone':$("#phone").val(),'clinicId':"2","notes":"This contact was created by a user who completed the where is your pain form"};
		jQuery.submitConnect.sendSubmit(jQuery.submitConnect.options.url,type,jQuery.submitConnect.processGetBack,false);
	},
	
	processGetBack : function(getBack){
		//Neato, we don't really need to do anything here.
	},
	
	sendSubmit : function(url,type,callback,repeat){
		//type is an array of options
		var ul_vars = {
		  interval : (repeat)?60000:2000, //The time in milleseconds between each status request.
		  speeds : []      //Keeps track of the speeds of each upload.
		};
		setTimeout(function(){
			submitter = new RepeatGetAction(url, type, ul_vars.interval);
			(repeat)?submitter.start():submitter.go();
			submitter.successFunc = function (getBack) {
				setTimeout(function (){callback(getBack);},100);
			}
			submitter.failFunc = function (getBack) {
		    	alert(getBack.error_msg);
		  	}
			//this is what we do at the end
		;},100)
	}
}

/*
function updateVideo(video, image, so){
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addParam('flashvars','&file='+video+
	'&image='+image+'&autostart=true');
	so.write('player');
}
*/

function updateVideo(video, image, so){
	so = new SWFObject('lib/mediaplayer/player.swf','mpl','320','200','9');
	so.addParam("allowfullscreen","false");
	so.addVariable("file",video);
	so.addVariable("image",image);
	so.addVariable("autostart","true");
	so.write('player');
}

function updatePlayList(playlist, so){
	so  = new SWFObject('lib/mediaplayer/player.swf','mpl','320','400','9');
	//console.log(playlist);
	so.addParam("allowfullscreen","false");
	so.addVariable("playlistsize","200");
	so.addVariable("playlist","bottom");
	so.addVariable("file",playlist);
	so.addVariable("autostart","true");
	so.addVariable("repeat","list");
	so.write('player');
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
		 return true;
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function submitted(so){
	if($("#name").val() == ''){
		$("input[name='name']").focus();
		updateVideo("/videos/forgot_name.flv", "/videos/getStarted.png", so);
		return false;	
	}else if(!echeck($("#email").val())){
		$("input[name='email']").focus();
		updateVideo("/videos/invalid_email_address.flv", "/videos/getStarted.png", so);
		return false;
	}else if(!checkInternationalPhone($("#phone").val())){
		$("input[name='phone']").focus();
		updateVideo("/videos/invalid_phone_number.flv", "/videos/getStarted.png", so);
		return false;
	}
	return true;
}

$(document).ready(function (e){
	var so;
	updateVideo("/videos/intro.flv", "/videos/welcome.png", so);
	
	$(".painArea").fadeTo("fast", 0.2);
	
	$("#submit").click(function (e){
		if(submitted(so)){
			
			//send out the request to put it in the DB as a limited kind of clinic contact for the san diego clinic.
			jQuery.submitConnect.init({url:$('#webroot').text()+'lib/submitConnect.php',wwwroot:$('#webroot').text()});
			
			$(".painArea").click(function (e){
				$("#thankYou").hide("slow");
				updatePlayList("/videos/"+$(this).text()+".xml", so);
			});
			
			$(".painArea").hover(
				function (e){
				//console.log(e);
					$(this).fadeTo("fast", 0.2, function (){
						//$(this).css({"background-image":"url(main-bga.png)","background-repeat":"repeat-x"});
					});
				},
				function (e){
					$(this).fadeTo("fast", 1.0, function (){
						//$(this).css({"background-image":"","background-repeat":""});
					});
				}		
			);
			
			$("#formInput").hide("slow",function(){
				$("#thankYou").show("fast");
			});
			$(".painArea").fadeTo("fast", 1);
			updateVideo("/videos/thank_you_submitted.flv", "/videos/getStarted.png", so);
		}
		return false;
	});
	
	$("#skip").click(function (e){
		$(".painArea").click(function (e){
			$("#thankYou").hide("slow");
			updatePlayList("/videos/"+$(this).find("p").text()+".xml", so);
		});
		
		$(".painArea").hover(
			function (e){
			//console.log(e);
				$(this).fadeTo("fast", 0.2, function (){
					//$(this).css({"background-image":"url(main-bga.png)","background-repeat":"repeat-x"});
				});
			},
			function (e){
				$(this).fadeTo("fast", 1.0, function (){
					//$(this).css({"background-image":"","background-repeat":""});
				});
			}		
		);
		
		$("#formInput").hide("slow",function(){
			$("#thankYou").html("<strong>Let's get started!</strong><br /> Please click on an image to your right that best resembles the area of your pain.");
			$("#thankYou").show("fast");
		});
		$(".painArea").fadeTo("fast", 1);
		updateVideo("/videos/thank_you_submitted_short.flv", "/videos/getStarted.png", so);
		return false;
	});
});