
//check if we autolaunch the popups for the video remixer
var baseurl = window.location.href;

if(baseurl.search('#') != -1){
	if(baseurl.search('loadbadly') != -1){
		showPopup('createvideo/badlysubtitledvideo',622,926,20,20);
	}
	if(baseurl.search('loadremix') != -1){
		showPopup('createvideo/videoremixer',622,926,20,20);
	}
}
else{
	if(baseurl.search('loadbadly') != -1){
		showPopup('/createvideo/badlysubtitledvideo',622,926,20,20);
	}
	if(baseurl.search('loadremix') != -1){
		showPopup('/createvideo/videoremixer',622,926,20,20);
	}
}




function popup(strTemplate,evt,param1,param2){
	
	var obj = document.getElementById('divPopup');


	var posx = 0;
	var posy = 0;

	if(evt){
		var e = evt;
	}
	else{
		var e = window.event;
	}
	if(e){
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
	}
		
	else{
		posx = param1;
		posy = param2;
	}

	posx -= 300;
	posy -= 20;
	if(posx <0){
		posx = "5";
	}
	
	obj.style.top = posy + "px";
	obj.style.left = posx + "px";

	
	var myWidth = 0, myHeight = 0, myBrowser = "";
   if( typeof( window.innerWidth ) == 'number' ) {
   //Non-IE
   //   document.getElementById(divPopup).style.position = 'fixed';
      document.getElementById('divFadeBackground').style.height = document.body.parentNode.scrollHeight + 'px';
      document.getElementById('divFadeBackground').style.width  = document.body.parentNode.scrollWidth  + 'px';
   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
//      document.getElementById(divDimmer).style.height = document.body.parentNode.scrollHeight + 'px';
//      document.getElementById(divDimmer).style.width  = document.body.parentNode.scrollWidth  + 'px';
    myHeight = Math.max(document.body.parentNode.scrollHeight, document.documentElement.clientHeight);
    myWidth  = Math.max(document.body.parentNode.scrollWidth, document.documentElement.clientWidth);

    document.getElementById('divFadeBackground').style.height = myHeight + 'px';
    document.getElementById('divFadeBackground').style.width  = myWidth  + 'px';
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      document.getElementById('divFadeBackground').style.height = (document.body.parentNode.scrollHeight-20) + 'px';
      document.getElementById('divFadeBackground').style.width  = (document.body.parentNode.scrollWidth-20)  + 'px';
   } 

	
	//Effect.Appear('divFadeBackground',{from:0.0, to:0.1, duration:0.3});
	
	
	setTimeout("Effect.Appear('divPopup',{duration:0.3})",300);
	xajax_showPopup(strTemplate,param1,param2);
	
	
	return false;
}

function closePopup(){
	//setTimeout("Effect.Fade('divFadeBackground',{from:0.1, to:0})",50);
	Effect.Fade('divPopup',{duration:0.2});
	document.getElementById('divPopup').innerHTML = "";
	return false;
}

function sort(){
	Sortable.create('sortable_list',{onUpdate: 
						function(){
							xajax_setSort(Sortable.serialize('sortable_list'));
						}
						}); 
}

/**Rating**/
function submitRating(intVideoId,intRating){
	document.getElementById('Rating_'+intVideoId).value = intRating;
	document.getElementById("frmRating_" + intVideoId).submit();
}
function overRating(i,VideoId,strFolder){
	for(var j = 1; j <= 5; j++){
		//document.getElementById(j+"_"+VideoId).style.border = "2px solid red";
		if(j <= i){
			document.getElementById(j+"_"+VideoId).src = "/images/skins/"+strFolder+"/rating_unit_over.gif";
		}
		else{
			document.getElementById(j+"_"+VideoId).src = "/images/skins/"+strFolder+"/rating_unit.gif";
		}
	}
}
function outRating(i,VideoId,strFolder,strImg1,strImg2,strImg3,strImg4,strImg5){
	for(var j = 1; j < 6; j++){
		document.getElementById(j+"_"+VideoId).style.border = "";
		switch(j){
			case 1:
			strImg = strImg1;
			break;
			case 2:
			strImg = strImg2;
			break;
			case 3:
			strImg = strImg3;
			break;
			case 4:
			strImg = strImg4;
			break;
			case 5:
			strImg = strImg5;
			break;
		}
		document.getElementById(j+"_"+VideoId).src = "/images/skins/"+strFolder+"/"+strImg+".gif";
	}
}

function showPopup(url,height,width,top,left) {
	if(!height){
		var height = 500;
	}
	if(!width){
		var width = 400;
	}
	if(!top){
		var top = 200;
	}
	if(!left){
		var left = 300;
	}

	/*var baseurl = window.location.href;
	//alert(baseurl);
	if(url == "createvideo/videoremixer/" || url == "createvideo/badlysubtitledvideo/" || url == "createvideo/viralvideo"){
		
		var http = baseurl.split('#')[0];
		url = http + url;
	}*/
	newwindow=window.open(url,'name','height='+height+',width='+width+',top='+top+',left='+left+',toolbar=no,resizable,scrollbars=1');
	if (window.focus) {newwindow.focus()}

} 

function validEmail( strEmail ){
	var pair = strEmail.split('@');
	if( pair.length !== 2 ){
		return false;
	}
	// validate local part
	var s = pair[0];
	if( /[^\!#\$%&'\*\+-\/\=\?\^_`\{\|\}~a-zA-Z0-9\.]/.test(s) ){
		return false;
	} 
	// "." not allowed as first or last character
	if( s.substr(0,1) === '.' || s.substr(s.length-1,1)  === '.' ){
		return false;
	}
	// Validate domain part
	var host = pair[1];
	var names = host.split('.');
	for( var n = 0; n < names.length; n++ ){
		switch(  names[n].substr(0,1) ){
		case '-':
			// may not start with a hyphen
			return false;
		}
		if(  names[n].length < 2 ){
			// minimum length of 2 characters
			return false;
		}
	}
	if( /[^-\.a-z0-9]/i.test(host) ){
		// bad character found
		return false;
	}
	// all tests passed
	return true;	
}


function checkLetterCount(){
	var strCountIntro = document.getElementById('Intro').value;
	var strCountBody = document.getElementById('Body').value;
	var strLinkName = document.getElementById('LinkName').value;
	var strLink = document.getElementById('ExternalLink').value;
	/*
	if(empty($Item['Content']) && empty($Item['ExternalLink'])){
					//no plus, no link
					$Item['Link'] = null;
					$Item['ExternalLink'] = null;
				}
				elseif(!empty($Item['Content']) && (empty($Item['ExternalLink'])) || $Item['ExternalLink'] == "no"){
					//plus opens, no link
					$Item['Link'] = "open";
					$Item['ExternalLink'] = null;
				}
				elseif (empty($Item['Content']) && !empty($Item['ExternalLink']) && strpos($Item['ExternalLink'],'http://') === false){
					//plus is a link
					$Item['Link'] = "link";
				}
	
	*/
	
	if(strCountBody.length == 0 && (strLink.length == 0 || strLink == "no") && strCountIntro.length <= 55){
		document.getElementById('divPlusMessage').innerHTML = "No plus is shown, does not expand, does not link anywhere";
	}
	else if(strCountBody.length > 0 && strLink.length == 0){
		document.getElementById('divPlusMessage').innerHTML = "Plus opens, no link shown";
	}
	else if((strCountBody.length > 0 || strCountIntro.length >= 55)&& strLink.length > 0 && strLink.substr(0,4) == 'http'){
		document.getElementById('divPlusMessage').innerHTML = "Plus opens, external link shown";
	}
	else if(strCountBody.length > 0 && strLink.length > 0 && strLink.substr(0,1) == '/'){
		document.getElementById('divPlusMessage').innerHTML = "Plus opens, internal link shown";
	}
	else if(strCountBody.length == 0 && strCountIntro.length <= 55 &&  strLink.substr(0,1) == '/'){
		document.getElementById('divPlusMessage').innerHTML = "Plus doesn't open but links to internal page";
	}
	else if(strCountBody.length == 0 && strCountIntro.length <= 55 && strLink.substr(0,4) == 'http'){
		document.getElementById('divPlusMessage').innerHTML = "Plus doesn't open but links to external page";
	}
}




/**
 * Special AJAX replay function.
 * parameters should be set in global scope by mtv_BreadCrumb::getHTML
 */
function xajax_replay (){
	if( ! xajax_replay_func ){
		return;
	}
	if( ! xajax_replay_args ){
		xajax_replay_args = [];
	}
	eval("xajax_"+xajax_replay_func).apply( window, xajax_replay_args );
}




/**
 * reverse process of xajax.geFormValues
 */
function setFormValues( FormId, Data ){
	try {
		// hack to disallow arrays
		// this is due to json_encode not being able to know that we intended the array to be associative
		if( Data.push !== undefined ){
			return;
		}
		var elForm = document.getElementById( FormId );
		if( elForm == null ){
			throw 'No form found with id ' + FormId;
		}
		for( var FieldName in Data ){
			var elField = elForm[FieldName];
			if( elField == null ){
				throw 'No field ' + FieldName;
			}
			if( elField.type == null && elField.item != null ){
				// Field is actually a node list
				for( var s in elField ){
					setMultiElementFieldValue( elField, Data[FieldName] );
				}
			}
			else {
				var FieldType = elField.type.toLowerCase();
				switch( FieldType ){
				case 'text':
				case 'hidden':
				case 'button':
				case 'submit':
				case 'reset':
					setGenericFieldValue( elField, Data[FieldName] );
					break;
				case 'select-one':
					setSelectFieldValue( elField, Data[FieldName] );
					break;
				default:
					throw 'Unsupported field type '+FieldType+' in '+FieldName;
				}
			}
		}
	}
	catch( e ){
		// ignore // alert( e.toString() );
	}
}


/**
 * Generic field value setter
 */
function setGenericFieldValue( elField, value ){
	if( typeof value !== 'string' ){
		throw elField.type + ' field '+elField.name+' expects string only';
	}
	elField.value = value;
}


/**
 * Select field value setter
 * @todo support falling back on option text node
 */
function setSelectFieldValue( elSelector, value ){
	// value must match at least one <option>
	for( var i = 0; i < elSelector.options.length; i++ ){
		var elOption = elSelector.options[i];
		if( elOption.value === value ){
			elSelector.selectedIndex = i;
			return;
		}
	}
	// failure try the node text instead of the values.
	// todo
}


/**
 * Set multiple elemement field value, like radio, checkbox
 */
function setMultiElementFieldValue( elFieldList, value ){
	for( var i = 0; i < elFieldList.length; i++ ){
		var elField = elFieldList[i];
		switch( elField.type.toLowerCase() ){
		case 'radio':
		case 'checkbox':
			setRadioButtonValue( elField, value );
			break;
		default:
			throw 'Unexpected field type ' + elField.type;
		}
	}
}


/**
 * Radio and Checkbox value setter
 */
function setRadioButtonValue( elRadio, value ){
	elRadio.checked = elRadio.value === value;
}


/**
* checks if there is a /#/ deeplink
*/
function checkUrl(Version){
	
	alert('here');
	var Url = window.location.href;
	
	alert(Url);
	if(Url.match("/#/")){
		
	}
	else{
		alert(Url);
	}

	//if(CurrentVersion != booCheck){
	
	document.getElementById('Version').value = 'Flash';
	
	document.frmVersion.submit();
	/*}
	else{
		//window.location.href="Url";
	}*/
	//xajax_switchFlash(Url,booCheck);
}

function checkDeepLink(){
}


/**
 * Track an external link, via urchinTracker.
 * Paramaters TrackingUserStatus & TrackingSiteVersion should be available globally
 * @param string
 * @param bool
 * @return bool false will cancel link href action
 */
function trackLink( externalURL, trackLocalPages ){
	try {
		var localDomain = window.location.protocol + '//' + window.location.host;
		var isLocal = externalURL.indexOf('http') !== 0 || externalURL.indexOf(localDomain) === 0;
		var isPage = /\.\w{2,5}$/.test(externalURL) && ! /\.(html?|php|aspx?|jsp)$/.test(externalURL);
		if( isLocal && ! isPage && ! trackLocalPages ){
			throw 'Page view will be tracked on view';
		}
		else if( isLocal ){
			var trackingURI = externalURL.replace( localDomain, '' );
			if( trackingURI.substr(trackingURI.length-1) !== '/' ){
				trackingURI += '/';
			}
			var trackingPath = '/'+ trackingUserStatus + trackingURI + trackingSiteVersion;
		}
		else {
			var trackingURI = window.location.pathname + window.location.search;
			if( trackingURI.substr(trackingURI.length-1) !== '/' ){
				trackingURI += '/';
			}
			if( externalURL.substr(externalURL.length-1) !== '/' ){
				externalURL += '/';
			}
			var trackingPath = '/'+ trackingUserStatus + trackingURI +'outgoing/'+ externalURL + trackingSiteVersion;
		}
		// alert( trackingPath );
		urchinTracker( trackingPath );
	}
	catch( Er ){
		// alert( Er.toString() );
	}
	return true;
}



/**
 * Track any path from flash
 * @param string
 * @return bool
 */
function trackFlash( trackingPath  ){
	if( trackingPath.substr(0,1) !== '/' ){
		trackingPath = '/' + trackingPath;
	}
	return trackLink( trackingPath, true );
}


/**
 * Change tracking user status
 * @param string new status
 * @return string previous status
 */
function trackChangeStatus( strStatus ){
	var oldStatus = trackingUserStatus;
	if( strStatus != null ){
		trackingUserStatus = strStatus;
	}
	return oldStatus;
}





