// @author:Damien King
// @lastUpdated: 11/03/2009
// @Copyright: Virgin Media

//Search Tab - Tab onClick functions ( FOR COLOURED GOOGLE )
function search_tab(panel_id, selector) {
	//var searchPanels = new Array('VM-search-bar-web', 'VM-search-bar-vid', 'VM-search-bar-loc', 'VM-search-bar-vms', 'VM-search-bar-kel');
	var searchPanels = new Array('VM-search-bar-web', 'VM-search-bar-loc', 'VM-search-bar-vms', 'VM-search-bar-kel');
	var suggestionText = new Array('Business or service e.g. plumbers');
	//Find current value 
	var currPanelVal; var currPanel;
	for (var i=0; i < searchPanels.length; i++) {
		var panelForm = document.getElementById(searchPanels[i]).getElementsByTagName('form')[0];
		var display = document.getElementById(searchPanels[i]).style.display;
		if (!display) {
			currPanel = searchPanels[0];
			currPanelVal = document.getElementById(searchPanels[0]).getElementsByTagName('form')[0].q.value;
			break;
		}
		else if (display=='block'){
			currPanel = searchPanels[i];
			if (typeof panelForm.q == 'undefined' && typeof panelForm.siteSearchQuery != 'undefinded') {
				currPanelVal = panelForm.siteSearchQuery.value;
			} else {
				currPanelVal = panelForm.q.value;
			}
			break;
		}
	}
	for (var i=0; i < suggestionText.length; i++) {
		currPanelVal = (currPanelVal == suggestionText[i]) ? '' : currPanelVal;
	}
	// hide all the panels & reset tab class name
	var tmpTabElem = first(selector.parentNode.parentNode);
	for (i=0; i < searchPanels.length; i++) {
		document.getElementById(searchPanels[i]).style.display = 'none';
		tmpTabElem.className = (i==0) ? 'VM-search-tab-undivided' : 'VM-search-tab-normal';
		tmpTabElem = next(tmpTabElem);
	}
	// Switch Tabs
	selector.parentNode.className = 'VM-search-tab-selected';
	if (next(selector.parentNode) != null) {
		next(selector.parentNode).className = 'VM-search-tab-undivided';
	} else {
		first(selector.parentNode).className = 'VM-search-tab-undivided';
	}
	// display selected panel
	document.getElementById(panel_id).style.display = 'block';
	// populate newly selected form field with contents of previously selected field - submit form if web or site
	var newPanelVal;
	var newPanelForm;
	for (var i=0; i < searchPanels.length; i++) {
		var display = document.getElementById(searchPanels[i]).style.display;
		if (display=='block') {
			var newPanelForm = document.getElementById(searchPanels[i]).getElementsByTagName('form')[0];
			if (typeof newPanelForm.q == 'undefined' && typeof newPanelForm.siteSearchQuery != 'undefined') {
				var textForm = newPanelForm.siteSearchQuery;
			} else {
				var textForm = newPanelForm.q;
			}
			if (searchPanels[i]!='VM-search-bar-loc') { 
				textForm.focus(); 
			}
			if (currPanelVal) {
				textForm.style.color = '#333';
				textForm.value = currPanelVal;
				if (searchPanels[i] != 'VM-search-bar-loc' && currPanel != 'VM-search-bar-loc') {
					newPanelForm.submit();
				}
			}
			break;
		}
	}
	function next( elem ) { do { elem = elem.nextSibling; } while ( elem && elem.nodeType != 1 ); return elem; }
	function prev( elem ) { do { elem = elem.previousSibling; } while ( elem && elem.nodeType != 1 ); return elem; }
	function first( elem ) { elem = elem.firstChild; return elem && elem.nodeType != 1 ? next ( elem ) : elem; }
	function last( elem ) { elem = elem.lastChild; return elem && elem.nodeType != 1 ? prev ( elem ) : elem; }
}

/**
 * Google Search Panel - Tab onClick functions
 */
function google_type(panel_id) {
	var i;
	var display;
	var currPanelVal;
	var currPanel;
	var newPanelVal;
	var newPanelForm;
	
	// get value of current open panel
	var googlePanels = new Array('VM-google-bar-web', 'VM-google-bar-loc', 'VM-google-bar-vms', 'VM-google-bar-kel');
	
	for (i=0; i<=googlePanels.length; i++) {
		display = document.getElementById(googlePanels[i]).style.display;
		if (!display) {
			currPanel = 'VM-google-bar-web';
			currPanelVal = document.getElementById('VM-frmGoogleWeb').q.value;
			break;
		}
		else if (display=='block'){
			currPanel = googlePanels[i];
			
			if (typeof document.getElementById(googlePanels[i]).getElementsByTagName('form')[0].q == 'undefined') {
				if (typeof document.getElementById(googlePanels[i]).getElementsByTagName('form')[0].siteSearchQuery != 'undefinded') {
					currPanelVal = document.getElementById(googlePanels[i]).getElementsByTagName('form')[0].siteSearchQuery.value;
				}
			} else {
				currPanelVal = document.getElementById(googlePanels[i]).getElementsByTagName('form')[0].q.value;
			}
			break;
		}
	}
	currPanelVal = (currPanelVal == 'Business or service e.g. Argos, plumbers, hotels') ? '' : currPanelVal;

	// hide all the panels
	document.getElementById('VM-google-bar-web').style.display = 'none';
	document.getElementById('VM-google-bar-loc').style.display = 'none';
	document.getElementById('VM-google-bar-vms').style.display = 'none';
	document.getElementById('VM-google-bar-kel').style.display = 'none';

	// display selected panel
	document.getElementById(panel_id).style.display = 'block';
	
	// populate newly selected form field with contents of previously selected field - submit form if web or site
	for (i=0; i<=googlePanels.length; i++) {
		display = document.getElementById(googlePanels[i]).style.display;
		if (display=='block') {
			newPanelForm = document.getElementById(googlePanels[i]).getElementsByTagName('form')[0];
			
			//newPanelVal = newPanelForm.q.value;
		
			if (typeof newPanelForm.q == 'undefined' && typeof newPanelForm.siteSearchQuery != 'undefined') {
				var textForm = newPanelForm.siteSearchQuery;
			} else {
				var textForm = newPanelForm.q;
			}
			
			if (googlePanels[i]!='VM-google-bar-loc') {
				textForm.focus();
			}
			if (currPanelVal) {
				textForm.style.color = '#333';
				textForm.value = currPanelVal;
				if (googlePanels[i]!='VM-google-bar-loc') {
					newPanelForm.submit();
				}
			}
			break;
		}
	}
}

/* Drop Nav */
(function(){
	
	var subNavHeight;
	
	function init() {
		var subNav = document.getElementById('VM-secondary-nav');

		var isFirst = true;
		
		var moreButton = document.getElementById('VM-more-nav');
		if (moreButton && subNav) {
			moreButton.onclick = function() {
				if (isFirst) {
					isFirst = false;
				
					subNav.style.display = 'block';
					
					subNavHeight = subNav.offsetHeight; 
					
					//Hidden the nav by setting height to 0
					subNav.style.height = '0px'; 
					
					subNav.style.display = 'none';
					
					//Set the overflow properties to hidden
					subNav.style.overflow = 'hidden';
				}
			
				slide(subNav, subNavHeight);
				return false;
			};
		}
		
		var closeButton = document.getElementById('VM-close-nav');
		if (closeButton) {
			closeButton.onclick = function() {
				slide(subNav, subNavHeight);
				return false;
			};
		}
	}
	
	var inEffect = false;
	
	//Sliding Effect
	function slide( elem, elemHeight ) {
		if (elem.style.height == '0px') {
			elem.style.height = '1px'; 
			elem.style.display = 'block';
		} 
		
		var isDown = (elem.style.height == '1px') ? true : false;
		
		var h = elemHeight;
		
		if (!inEffect) {
			
			var frame = 100;
			
			//Animation
			for ( var i = 0; i <= frame; i += 5 ) { 
				//A closure to make sure that we have the right 'i' 
				(function(){ 
					var pos=i; 
					//Set the timeout to occur at the specified time in the future 
					setTimeout(function(){ 
						//Set the new height of the element 
						if (isDown) {
							elem.style.height = ((pos/frame) * h) + "px";
						} else {
							elem.style.height = (h - ((h/frame)*pos)) + "px";
							if ( (h - ((h/frame)*pos)) < 1 ) {
								elem.style.display = 'none';
							}
						}
						inEffect = (pos == frame) ? false : true;
					}, (pos+1)*2 ); 
				})(); 
			} 
		}
	}
	
	function addEvent(element, eventType, doFunction, useCapture){
		if (element.addEventListener) 
		{
			element.addEventListener(eventType, doFunction, useCapture);
			return true;
		} else if (element.attachEvent) {
			var r = element.attachEvent('on' + eventType, doFunction);
			return r;
		} else {
			element['on' + eventType] = doFunction;
		}
	}

	addEvent(window, 'load', init, false);
})();


		
// Core js definitions
var d=document;

// resolution for adserver
var resolution = screen.width;

// shortened document getElement
function $(id){
    return (d.getElementById(id)) ? d.getElementById(id) : (d.getElementsByTagName(id))? d.getElementsByTagName(id) : false;
}
var px = (document.getElementById) ? "px" : "";

ad_rf_banner=1;
ad_rf_mpu=1;
ad_rf_tower=1;
ad_rf_sbanner=1;

//Amend Links to pop where approriate
 function addEvents(o, e, f) { 
    if (o.addEventListener) return o.addEventListener(e, f, false); 
    else if (o.attachEvent) return o.attachEvent("on"+e, f); 
    else return false; 
} 

function setLinkTargetsvm() {
   if (!window.RegExp || !document.getElementsByTagName) return false;  
   var links = document.getElementsByTagName("a");
    
   var domain = 'virginmedia.com';
	var adServer = 'adserver.virginmedia.com';
	var living = 'livingtv.co.uk';
   var re = new RegExp("^http:\/\/(.*\.)?" + domain);
   var ad = new RegExp("^http:\/\/(.*\.)?" + adServer);
	var lv = new RegExp("^http:\/\/(.*\.)?" + living);
    for (var i = 0; links.length > i; i++) {  
        var link = links[i];
        if (link.href && String(link.href).indexOf('javascript:') == -1 && !link.target && !link.onclick && !re.test(link.href) && !lv.test(link.href) || ad.test(link.href)) {
            links[i].target = "_blank";
        }
    }  
}
if (String(location.href).indexOf("http://www.virginmedia.com") != -1) {
    addEvents(window, "load", setLinkTargetsvm); 
}


////////////////////////////////////////////////////
// Rotator
///////////////////////////////////////////////////

// set vars - change obj names for different
var currentDiv = 1;
var obj1 = 'lead-tab-area-1';
var obj2 = 'lead-tab-area-2';
var obj3 = 'lead-tab-area-3';
var rotateTime = 7500;
var rotatePause = 10000;

function swap(obj) {
	clearTimeout(rotSplashTimer1);
	var len = obj.length;
	val = obj.charAt(len-1);
	currentDiv = parseInt(val);
	switchDiv(currentDiv);
	rotSplashTimer1 = setTimeout("rotator()",rotatePause);
}

function rotator() {
	clearTimeout(rotSplashTimer1);
	currentDiv++;
	if(currentDiv>3) { currentDiv = 1; }
	switchDiv(currentDiv);	
	rotSplashTimer1 = setTimeout("rotator()",rotateTime);
}

function switchDiv(c) {
switch (c)
  {
    case 1: { 
			$(obj1).style.display = "block"; 
			$(obj2).style.display = "none"; 
			$(obj3).style.display = "none"; 
			break 
			}
     case 2: { 
	 		$(obj2).style.display = "block"; 
			$(obj1).style.display = "none"; 
			$(obj3).style.display = "none"; 
			break 
			}
	 case 3: {
	 		$(obj3).style.display = "block"; 
			$(obj1).style.display = "none"; 
			$(obj2).style.display = "none"; 
			break 
			}
    default: { 
			$(obj1).style.display = "block";  
			break 
			}
  }
}


///////////////////////////////////////////////
// stop and start on rollover - new feature
//////////////////////////////////////////////
function holdRotator() {
	clearTimeout(rotSplashTimer1);
}

function startRotator() {
	clearTimeout(rotSplashTimer1);
	rotSplashTimer1 = setTimeout("rotator()",rotateTime);
}

///////////////////////////////////////////////
// end rotator
///////////////////////////////////////////////

// Channel sub nav drop down fix for I.E. - acts as :hover psuedo class 

sfHover = function() {     
 var sfEls = document.getElementById("nav-super");
 if(sfEls) {
  sfEls = sfEls.getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
   sfEls[i].onmouseover=function() {
    this.className+=" sfhover";
   }
   sfEls[i].onmouseout=function() {
    this.className=this.className.replace(new RegExp(" sfhover\\b"), "");         
   }     
  } 
 }
} 

if (window.attachEvent) window.attachEvent("onload", sfHover); 


// News three panel control JavaScript Document
function three_panel_select(panel_id) {
	// hide all the panels
	$('threePanelContentPanel1').style.display = 'none';
	$('threePanelContentPanel2').style.display = 'none';
	$('threePanelContentPanel3').style.display = 'none';	

	// show the requested panels
	$(panel_id).style.display = 'block';
}


function two_panel_select(panel_id) {
	// hide all the panels
	$('twoPanelContentYourshout').style.display = 'none';
	$('twoPanelContentDiscuss').style.display = 'none';
	// show the requested panels
	$(panel_id).style.display = 'block';
}

function dual_panel_select(panel_id) {
	// hide all the panels
	$('twoPanelContentPanel1').style.display = 'none';
	$('twoPanelContentPanel2').style.display = 'none';
	// show the requested panels
	$(panel_id).style.display = 'block';
}

function setVMHOME(this_link){
	if (navigator.appVersion.charAt(navigator.appVersion.indexOf("MSIE") + 5) >= 5 && 
        navigator.platform.indexOf("Win16") == -1 && 
		navigator.platform.indexOf("Mac") == -1 ) {
		this_link.style.behavior='url(#default#homepage)';
		this_link.setHomePage('http://www.virginmedia.com');
		this_link.href="#";
		return true;
	} else if (window.sidebar) {
     	window.sidebar.addPanel("Virgin Media", this_link, "");
		return true;
   	} else if (window.sidebar&&window.sidebar.addPanel) {
    	 window.sidebar.addPanel("Virgin Media",this_link,"");
		 return true;
	} else {
		return false;
	}
}


/* new google search bar/local swap header and footer */
function google_search_type(search_id) {
	var isFooter = search_id.indexOf("footer");
	if(isFooter<1) {
		// hide all the panels
		if ($('search-panel-web')) $('search-panel-web').style.display = 'none';
		if ($('search-panel-local')) $('search-panel-local').style.display = 'none';
		// show the requested panels
		if ($(search_id)) $(search_id).style.display = 'block';
	} else {
		// hide all footer panels
		if ($('search-panel-web-footer')) $('search-panel-web-footer').style.display = 'none';
		if ($('search-panel-local-footer')) $('search-panel-local-footer').style.display = 'none';
		// show footer requested panels
		if ($(search_id)) $(search_id).style.display = 'block';
	}
}

function sage_ev () {}

/*/ Google Search panel control JavaScript Document
function google_type(panel_id) {
	// hide all the panels
	$('google-web-H').style.display = 'none';
	$('google-loc-H').style.display = 'none';
	//$('google-map-H').style.display = 'none';
	//$('google-dir-H').style.display = 'none';
	
	$(panel_id + '-H').style.display = 'block';
	
	if (document.getElementById('google-web-F')) {
  	$('google-web-F').style.display = 'none';
  	$('google-loc-F').style.display = 'none';
  	//$('google-map-F').style.display = 'none';
  	//$('google-dir-F').style.display = 'none';
  
  	// show the requested panels
  	$(panel_id + '-F').style.display = 'block';
	}
} */

function winPopMatchScore(myurl, wid, hei) {
	var url = myurl;
	var we = wid;
	var he = hei;
	var win = window.open(url,'winname','scrollbars=1,width=' + wid + ',height=' + hei); 
}