/*********************************************************************************
*	Page object
*	created: 01/10/07
*	modified:
*
*	 
*	
* 	
*
*********************************************************************************/
Page = {};
page = Page;

function element(id) {
	return document.getElementById(id);
}

Page.show = function() {
	for(var i = 0; i < arguments.length; i++){
		try{
			document.getElementById(arguments[i]).style.visibility = 'visible';
		}
		catch(e){
			try{
				System.out.println('ERROR Page.show('+arguments[i]+');');
			}
			catch(e){
				System.out.println('ERROR Page.show');
			}
		}
	}
}
	
Page.hide = function() {
	for(var i = 0; i < arguments.length; i++){
		try{
			document.getElementById(arguments[i]).style.visibility = 'hidden';
		}
		catch(e){
			try{
				System.out.println('ERROR Page.hide('+arguments[i]+');');
			}
			catch(e){
				System.out.println('ERROR Page.hide');
			}
		}
	}
}

Page.href = function(href){
	// display change action graphic
	document.location.href = href;	
}
	
Page.fadeout = function(layer,rate) {
	
}
	
Page.fadein = function(layer,rate) {

}

Page.onload = function(func) {
	if(window.attachEvent){
		window.attachEvent('onload',eval(func));
	}
	else if(document.addEventListener){
		// safari
		if (/WebKit/i.test(navigator.userAgent)) { 
			var safari_timer = setInterval(function() {        
				if (/loaded|complete/.test(document.readyState)) {            
					clearInterval(safari_timer);    
					//alert(func);        
					eval(func+'()');  
				}    
			}, 10);
		}
		else // not safari
			document.addEventListener('DOMContentLoaded',eval(func),null);
	}
}

Page.e = function(name) {
	return document.getElementById(name);
}



Page.linkcall = function(obj,area,pntype) {
	return true;
	
	if(area == '')
		return true;
	// for now assume the link is going offsite
	if(obj.target){
		//System.out.println('Target is defined');
		return true;
	}
	// the link is in an iframe	- let it do its thing
	if(window.parent != window.self){
		//System.out.println('Window is not the parent');
		return true;
	}

	var old_href = obj.href;

	if(old_href.indexOf('javascript')>-1){
		//System.out.println('javascript detected');
		return true;
	}
	
	// test for file extention BROKEN
	//if(old_href.indexOf('.jhtml')<0){
		// this is a directory - make sure it has a following slash
		//if(old_href.substring(old_href.length-1)!='/'){
	//		System.out.println('No Slash Detected');
		//	old_href+='/';
	//	}
	//}

	var pn = "source";
	
	if(pntype){
		pn = pntype;
	}
	//////////////////////////////////
	// this block for links in the playlist of the video page
	/*
	var pa = location.pathname.split("/")
	if(pa.length){
		if(pa[1] == 'video')
			pn = "name"
	}
	*/
	////////////////////////////////
	//System.out.println('param name='+pn);
	//System.out.println('area = '+area);
	var new_href;
	if(old_href.indexOf('?')>-1){
		new_href = old_href+"&"+pn+"="+area;
	}
	else{
		new_href = old_href+"?"+pn+"="+area;
	}
	//System.out.println('new_href='+new_href);
	
	//obj.href = new_href;
	try{
		window.location.href = new_href;
		return false;
		
	}catch(e){
		System.out.println('ERROR: setting document.location');
		return true;
	}
	
}


Page.writeTo = function(layer,str){
	try{
		document.getElementById(layer).innerHTML = str;
	}
	catch(e){
		System.out.println('ERROR Page.writeTo()');
		//System.out.println('ERROR: layer = '+layer);
		//System.out.pringln('ERROR: str = '+str);
	}
}

Page.removeChar = function(str,charToRemove){
	regExp = new RegExp("["+charToRemove+"]","g");
	return str.replace(regExp,"");
}

Page.replaceChar = function(str,charToReplace,charToReplaceWith){

	regExp = new RegExp("["+charToReplace+"]","g");
	return str.replace(regExp,charToReplaceWith);
}


Page.txtImg = function(layer,img, alt){
	//System.out.println('layer='+layer);
	document.getElementById(layer).innerHTML = '<img src="'+img+'" border="0" alt="'+alt+'" />';
}
//Page.txtSwf('content_home','/video/swf/head.swf','contenthead',650,30)
Page.txtSwf = function(layer,swf,name,w,h,flashVars){
	//return;
	//System.out.println('Page.txtSwf');
	//System.out.println('layer='+layer);
	//System.out.println('swf='+swf);
	//System.out.println('name='+name);
	//System.out.println('w='+w);
	//System.out.println('h='+h);
	//System.out.println('flashVars='+flashVars);
	
	var contents = document.getElementById(layer).innerHTML;
	//System.out.println('contents='+contents);
	
	// strip HTML tags from contents
	var strippedContents = contents.replace(/(<([^>]+)>)/ig,"");
	//System.out.println('strippedContents: '+strippedContents);
	
	// cannot pass ampersands will need to replace with |~|
	var replacedContents = Page.replaceChar(strippedContents,"&","~")
	//System.out.println('replacedContents: '+replacedContents);
	
	// remove leading and trailing blank space
	var fixedContents=(replacedContents.replace(/^\W+/,'')).replace(/\W+$/,'');
	//System.out.println('fixedContents: '+fixedContents);
	
	if(flashVars){
		flashVars += "&amp;";
	}
	else {
		flashVars = '';
	}
	flashVars += "inputText="+fixedContents;

	
	dhtml = 0;
	var so = new SWFPlayerObject("so", name, w, h, "8", "none");
	so.setPath(swf);
	//so.addVariable("CONFIG_URL", '/player/embed/mtv/configuration.jhtml%3Fartist%3D1098%26vid%3D113086%26allowFullScreen%3Dtrue' + channelName);
	//so.addVariable("clientEnvironmentVars", "testmode="+playertestmode);
	//so.addParam("allowFullScreen", "true");
	so.addParam("wmode", "transparent");
	so.addParam("FlashVars", flashVars);
	
	so.write(layer);	
	
	//System.out.println('---------------------------------------');
	//System.out.println(Page.e('content_head').innerHTML);
	//System.out.println('---------------------------------------');
	
}

Page.imgErr = function(imgObj, defaultImg){
	// Used in the img onerror event
	// will return a default image if specified
	// if the default errors, or is not specified - a transparent gif is returned
	if(!defaultImg){
		imgObj.src = '/img/spc.gif';
	}
	else {
		var replacementImage = new Image();
		replacementImage.src = defaultImg;
		replacementImage.onload = function(){
			imgObj.src = replacementImage.src;
		}
		replacementImage.onerror = function(){
			imgObj.src = '/img/spc.gif';
		}
	}
}

Page.sendToMobile = function(tSource, tStoryID){
	var url="http://www.vh1.com/mobile/sendtomobile/SendToMobile.jhtml?source=" + tSource + "&storyID=" + tStoryID; 
	STMPopup = window.open(url,"sendtomobile","width=250, height=180, top=300, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
}

Page.setInput = function(elementId,defaultText) {
	var theInput = document.getElementById(elementId);
	if (theInput.value == '') {
		theInput.value = defaultText;
	}
}

Page.clearInput = function(elementId,defaultText) {
	var theInput = document.getElementById(elementId);
	if (theInput.value == defaultText) {
		theInput.value = '';
	}
}

Page.validateSearchOnSubmit = function(elementId, blankFieldWarning) {
	var fieldValue = document.getElementById(elementId).value;
	if (fieldValue=="") {
		Page.setInput(elementId, blankFieldWarning);
		return false;
	} else if(fieldValue==blankFieldWarning) {
		return false;
	}
}

Page.thumbOver = function(elementObj){	
	elementObj.style.borderColor="#fe4db7";
}

Page.thumbOut = function(elementObj){	
	elementObj.style.borderColor="#aeaeae";
}

Page.setTemplateConfigs = function() {	
	var width = 147;
	var height = 27;
	var soRockAutism = new SWFObject("http://www.vh1.com/sitewide/globalnav/swf/classicautism.swf", 'mainswf', width, height, "8", "#ffffff");
	soRockAutism.addParam("quality", "high");
	soRockAutism.addParam("wmode", "transparent");
	soRockAutism.addParam("salign", "t");
	soRockAutism.write("vh1classic_rock_autism");
	
	var width = 131;
	var height = 28;
	var soGlobalFund = new SWFObject("http://www.vh1.com/sitewide/globalnav/swf/globalfund.swf", 'mainswf', width, height, "8", "#ffffff");
	soGlobalFund.addParam("quality", "high");
	soGlobalFund.addParam("wmode", "transparent");
	soGlobalFund.addParam("salign", "t");
	soGlobalFund.write("globalfund");
}