
var psacode = '<object width="420" height="280"><param name="movie" value="http://www.youtube.com/v/N6RRjrXVmyw&hl=en_US&fs=1&color1=0x3a3a3a&color2=0x999999&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/N6RRjrXVmyw&hl=en_US&fs=1&color1=0x3a3a3a&color2=0x999999&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="420" height="280"></embed></object>';

var telephonecode = '<object id="myExperience24574103001" class="BrightcoveExperience"><param name="bgcolor" value="#000000" /><param name="width" value="486" /><param name="height" value="412" /><param name="playerID" value="69540139001" /><param name="publisherID" value="1079000770"/><param name="isVid" value="true" /><param name="optimizedContentLoad" value="true" /><param name="@videoPlayer" value="ref:VMUSUV71000407" /><param name="autoStart" value="true" /></object><script type="text/javascript">brightcove.createExperiences();</script>';

var agecode = '';
var date;

var homepagetimer;
var currenttout = 0;

function clearEmail(what, check) {
	
	whatVal = document.getElementById(what).value;
	
	if(whatVal == check) {
		document.getElementById(what).value = "";
		//$('#signupnote').html('');
	}
	
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function checkEmail(fld) {
    var error="";
    var tfld = trim(fld);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld == "") {
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "Please enter a valid email address.\n";
    } else if (fld.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
    }
    //return error;
	if(error == "") {
		return true;
	} else {
		return false
	}
}

function checkPhone(fld) {
    var error = "";
    var stripped = fld.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld == "") {
        error = "You didn't enter a phone number.\n";
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
    }
    //return error;
	if(error == "") {
		return true;
	} else {
		return false
	}
}

function showpsa() {
	$('#psa').html(psacode);	
}

function showAgeVer() {
	$('#vidImg').remove();
	$('#agever').css({'visibility':'visible','display':'block'});	
}

function showTelephone() {
	$('#telephonevideo').html(telephonecode);	
}

$(function() {
	$('#psa').html('<a href="javascript:showpsa();" title="Lady GaGa Message"><img src="http://1trickpony.cachefly.net/VMU/ladyvirgin/_images/_global/psa.jpg" alt="Lady GaGa Message" /></a>');	   
	
	/*
	$('#tour').hover(
	  function () {
		$('#comingsoon').css("visibility", "visible");
	  },
	  function () {
		$('#comingsoon').css("visibility", "hidden");
	  }
	);
	*/
	
	
	
	$('#submitage').click(function(e) {
		
		stopEvent(e);
		
		var month = $('#month').val();
		var day = $('#day').val();
		var year = $('#year').val();
		var dob = year+'-'+month+'-'+day;
		
		var dif = 0;
		
		if(month != '00' || day != '00' || month != '0000') dif = dateCheck(today, dob);
						
		if(dif < 13) $('#notvalid').css({'visibility':'visible','display':'block'});
		else showTelephone();
		
		return false;
		
	});	
	
	toutupdate();
	
});

function emailsubmit() {
	// PASS THE EMAIL ADDRESS TO SIGNUP PHP
		
	$('#signupnoteemail').html('').css('display', 'none'); 
	$('#signupnotephone').html('').css('display', 'none'); 
	$('#signupnote').html('').css('display', 'none'); 
		
	currEmail = $('#emailsignup').val();
	currPhone = $('#emailsignupphone').val();
	
	var onlyPhone;
	var onlyEmail;
	
	
	// validate phone
	if(currEmail == "" && currPhone != "") {
	   	onlyPhone = true;
	}
	validPhone = checkPhone(currPhone);
	
	// validate email
	if(currPhone == "" && currEmail != "") {
	   	onlyEmail = true;
	}
	validEmail = checkEmail(currEmail);
	
	submitForm = false;
	if((onlyPhone == true) && (validPhone == true)) {
		submitForm = true;
	}
	
	if((onlyEmail == true) && (validEmail == true)) {
		submitForm = true;
	}
	
	if((submitForm != true) && ((validPhone == true) && (validEmail == true))) {
		submitForm = true;
	}
	
	if(submitForm == true) {
		
		var response = $.ajax({
			url:		'_scripts/recieveUpdates.php',
			type:		'POST',
			cache:		false,
			async:		false,
			data:		"email="+currEmail+"&phone="+currPhone
		});
		
		//$('#emailsignup').val('Enter your email address to receive newsletters');
		$('#signupnote').html('Thanks for signing up.').css('display', 'block'); 
		
		//alert(response.responseText);
		
		return response.responseText;
	
	} else {
		
		// error on email
		if((onlyPhone != true) && (validEmail != true)) {
			$('#signupnoteemail').html('Please enter a valid email address.').css('display', 'block'); 
		}
		
		// error on phone
		if((onlyEmail != true) && (validPhone != true)) {
			$('#signupnotephone').html('Please enter a valid phone number.').css('display', 'block'); 
		}
		
	}
}


function stopEvent(event) {
    event.preventDefault();
    event.stopPropagation();
    if ($.browser.msie) {
        event.originalEvent.keyCode = 0;
        event.originalEvent.cancelBubble = true;
        event.originalEvent.returnValue = false;
    }
}

function dateCheck(S1, S2) { // JRS, using Y M D
  var D1 = ReadISO8601date(S1) ; if (D1<0) return 0;
  var D2 = ReadISO8601date(S2) ; if (D2<0) return 0;
  return (D1[0]-D2[0]) - (D1[1]*100 + D1[2] < D2[1]*100 + D2[2]); 
}

function ReadISO8601date(Q) { var T // adaptable for other layouts
  if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(Q)) == null)
    { return -2 } // bad format
  for (var j=1; j<=5; j+=2) T[j] = +T[j] // some use needs numbers
  if (!ValidDate(T[1], T[3]-1, T[5])) { return -1 } // bad value
  return [ T[1], T[3], T[5] ]; }
  
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
  with (new Date(y, m, d, 6)) // 6 for Opera 9.2x
    return (getMonth()==m && getDate()==d) /* was y, m */; }
	

function toutupdate() {
	homepagetimer = window.setInterval(function() {
		if(currenttout + 1 > $('#toutbtns a').length - 1) {
			currenttout = 0;
		} else currenttout++;
		movetout(currenttout);
	}, 10000);	
}
	
function showtout(n) {
	movetout(n);
	clearInterval(homepagetimer);
}

function movetout(n) {
	$('#toutbtns a.active').removeClass('active');
	$('#toutbtns #pos' + n).addClass('active');
	
	$('#thetouts').animate({left:-962 * n}, 600);		
}

/*****
	
	GALLERY SHARING
	
*****/

function showshare(hide) {
	if($('#share').css('visibility') == 'visible' || hide == true) {
		$('#share').css('visibility', 'hidden');
		showtwitter(true);
		showfacebook(true);
	} else {
		showemail(true);
		showtwitter(true);
		showfacebook(true);
		
		var videopath = videopage == 'monsteryourself' ? 'http://ladyvirgin.com/monsteryourself.php?vid='+currentvideo : 'http://ladyvirgin.com/alejandro.php?vid='+currentvideo;
					
		$('#sharefacebook').bind('click', showfacebook).css('cursor', 'pointer'); //attr('href', 'http://www.facebook.com/sharer.php?u='+videopath+'&t=VIRGIN%20MOBILE%20PRESENTS%20LADY%20GAGA%20MONSTER%20BALL%20TOUR');
		$('#sharetwitter').bind('click', showtwitter).css('cursor', 'pointer'); //attr('href', 'http://www.twitter.com/home?source=ladyvirgin.com&status=Monster+Video+'+videopath);
		$('#sharemyspace').attr('href', 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent('LADY GAGA MONSTER VIDEO') + '&c=' + encodeURIComponent('View this Lady Gaga Monster Video') + '&u=' + encodeURIComponent(videopath));
		
		//$('#tweetit').click(sendtweet);
		
		$('#share').css('visibility', 'visible');
				
	}
}

function showemail(hide) {
	if($('#email').css('visibility') == 'visible' || hide == true) $('#email').css('visibility', 'hidden');
	else {
		showshare(true);
		showtwitter(true);
		showfacebook(true);
		$('#email').css('visibility', 'visible');
	}
}

function showtwitter(hide) {
	if($('#twitter').css('visibility') == 'visible' || hide == true) $('#twitter').css('visibility', 'hidden');
	else {
		showemail(true);
		showfacebook(true);
		$('#twitter').css('visibility', 'visible');
	}
}

function showfacebook(hide) {
	if($('#facebook').css('visibility') == 'visible' || hide == true) $('#facebook').css('visibility', 'hidden');
	else {
		showemail(true);
		showtwitter(true);
		$('#facebook').css('visibility', 'visible');
	}
}

function sendtweet(event) {
	event.preventDefault();
	
	var thetweet = $('#thetweet').val();
	var followgaga = $('#ladygaga').attr('checked') == 'checked' ? 'true' : 'false';
	var followvmobile = $('#vmobile').attr('checked') == 'checked' ? 'true' : 'false';
	var dataString = 'ladygaga='+followgaga+'&vmobile='+followvmobile+'&tweet='+thetweet;
	
	//console.log(dataString);
	
	$.ajax({
		type: 'POST',
		url: '_scripts/tweet.php',
		data: dataString
	});
	
	return false;
}	