

$(function() {		
	
	// email and embed tab actions
	$("#shareTabs a").click(function(e)  { 
		$("#vmix_p").hide();  
		
		// toggle tab class
		var tab = $(this); 
		$("#panes #shareTabs a").removeClass("current");
		tab.addClass("current");
		
		// show / hide panes
		$(tab.attr("href")).show();
//		var other = (tab.attr("href") == "#emailPane") ? $("#embedPane") : $("#emailPane");
//var other = (tab.attr("href") == "#emailPane") ? ((tab.attr("href") == "#embedPane") ? $("#embedPane") : $("#sharePane")) : (tab.attr("href") == "#embedPane") ? $("#sharePane") : $("#embedPane");


		switch(tab.attr("href")){
			case '#emailPane':
				$("#embedPane").hide();
				$("#sharePane").hide();
            break;
			case '#embedPane':
				$("#emailPane").hide();
				$("#sharePane").hide();
            break;
			case '#sharePane':
				$("#emailPane").hide();
				$("#embedPane").hide();
            break;
			default:
				$("#sharePane").hide();
				$("#embedPane").hide();
				$("#emailPane").hide();
			break;
		}



//		other.hide();		
		
		// prevent link's default behaviour
		return e.preventDefault();
	});
	
	// close buttons
	$("#panes div.close, #panes button.close, #panes div.closeHome").click(function() {
		$("#shareTabs a").removeClass("current");
		$("#emailPane, #embedPane, #sharePane").hide();
		$("#vmix_p").show();
	});
	

	/*
		email form setup. depends on simple JSON responses of the form
			- on error: {message: 'error message'}
			- otherwise it's successs
	*/
		
});

//var test="This is the embed code";

	$(function() {
		//$f("player", "flowplayer.commercial-3.1.5/flowplayer/flowplayer.commercial-3.1.5.swf"); 
		var finalEmbedCode = generateEmbedCode();
		$("#embedPane textarea").html(finalEmbedCode);				
	});




/*function showContent(contentId) {
	
	var displayStatus=document.getElementById(contentId).style.display;
	//alert(displayStatus);
	if((displayStatus=="none") || (displayStatus==""))
	{
		document.getElementById(contentId).style.display = 'block';
	}
	else
	{
		document.getElementById(contentId).style.display = 'none';
	}
}*/

/**************************************** Validation Starts here ***************************************/

function enableSendBtn() {
	document.getElementById('btnSend').disabled = false;
	}
function disableSendBtn() {
	document.getElementById('btnSend').disabled = true;
}

function validate() {
	disableSendBtn() ;
	var error = 0;

	if (!validateMultiEmail('toAddress')) {		/* Parameter passed is the Id of the input element */
		alert("Invalid Email address or Addresses !")
		error++;
		enableSendBtn()
		return false;
	}
	
	var fromEmail = document.getElementById('fromEmail').value ;
	if (fromEmail != "") {
		//alert("Inside email Validate ()");
		if (!validateEmail(fromEmail)) {		/* Parameter passed is a String  */
			alert("Your Email address that you have entered is invalid !");
			enableSendBtn() ;
			error++;
			return false;
		}
	}
	
	if (error < 1) {
		//alert("Validation Successful ! Now you can send email");
		$("#shareTabs a").removeClass("current");
		$("#emailPane, #embedPane").hide();
		$("#vmix_p").show(); 
		setParam();
	}

	else	{
		//alert("Error ! Mail cannot be sent !");
		enableSendBtn() ;
		return false;
	}
}
		

function validateMultiEmail(addresses) {
	var email_field = document.getElementById('toAddress').value ;
	//var email_field = addresses;
	var emails = email_field.split(',');
	var emailsCount = emails.length ;
	var err = 0;
	for (var i = 0; i < emails.length; i++) {
		emails[i] = emails[i].replace(/^\s+|\s+$/g,""); //trim left and right white spaces.
		if (!validateEmail(emails[i]) ) {
			err++;
			//alert('One or more email addresses entered is invalid. Err = ' + err + ' : ' + emails[i]);
			  //alert('Invalid Email Address');
	
			}
		}
	if (emailsCount > 5) {
		alert("Cannot send to more than 5 addresses !");
		err++;
		return false;
		}
	if(err<1) {
		//alert("Email Validation Successful !");
		return true;
		}
	else
		return false;
	
} 

function validateEmail(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var address = document.forms[form_id].elements[email].value;
	//address = document.getElementById('toAddress').value ;
	if(reg.test(address) == false) {
      		//alert('Invalid Email Address : ' + address);
		return false;
    		}
	else {
		//alert("Email Validated : " + address);
		return true;
	}

}


/**************************************** Validation Ends here ***************************************/


/**************************************** Ajax Starts here ***************************************/

function initxmlhttp()
{
	var xmlhttp ;
	if ( !xmlhttp && typeof XMLHttpRequest!='undefined' )
	{
		try
		{
			xmlhttp = new XMLHttpRequest() ;
		}
		catch (e)
		{
			xmlhttp = false ;
		}
	}
	return xmlhttp ;
}

function setParam()
	{	
		//alert("Inside SetPAram");
		var toAddress = document.getElementById('toAddress').value ;
		var toAddressArr = toAddress.split(',');
		//alert("toAddressArr" + toAddressArr);
		var fromName = document.getElementById('fromName').value ;
		var fromEmail = document.getElementById('fromEmail').value ;
		if (fromEmail == "")
			fromEmail = "notify@musicmatrix.com";
		var personalMsg = document.getElementById('personalMsg').value ;
		sendEmail(toAddress, fromName, fromEmail, personalMsg) ;
	}

function sendEmail(toAddress, fromName, fromEmail, personalMsg){
		//alert("Inside SendEmail");
		//var url = "http://<?=$_SERVER['HTTP_HOST']?>emailTest.php?path="+path+"&type="+type+"&name="+name;
		
		var currentUrl = getCurrentUrl();
 		//alert("Current Url : " + currentUrl);
		var url = "ajax_playerEmail.php?toAddress="+toAddress+"&fromName="+fromName+"&fromEmail="+fromEmail+"&personalMsg="+personalMsg+"&currentUrl="+currentUrl ;

			var xmlreload = initxmlhttp() ;
			xmlreload.open( "GET", url, true ) ;
			xmlreload.onreadystatechange=function()
			{
				if (xmlreload.readyState==4)
				{
					ProData = xmlreload.responseText;
					
					if(ProData != "")
					{
 						if(ProData=="sent"){
							alert("Mail Sent");
							resetInputs();
							enableSendBtn() ;
						}
						else if(ProData=="notSent"){
							alert("Mail Not Sent");
							enableSendBtn() ;
						}


					}
				}
			}

			xmlreload.send(null) ;
}

/**************************************** Ajax Ends here ***************************************/


/**************************************** Input Reset Starts here ***************************************/

function resetInputs() {
	document.getElementById('toAddress').value = "" ;
	document.getElementById('fromName').value = "" ;
	document.getElementById('fromEmail').value = "" ;
	document.getElementById('personalMsg').value = "" ;
}
