/*
telus scripts
Last edit: 2009-06-26
*/

/* ########################################################### */

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
	var j = i + alen;    
	if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}


function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie (name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : 365;  // always permanent
        var path = (argc > 3) ? argv[3] : '/';
//        var domain = (argc > 4) ? argv[4] : 'telus.com';
        
        var domain;
	if (argc > 4) {
	  domain = argv[4];
	}
	else {
          if (window.location.hostname.match(/telus.com/) ) {
            domain = 'telus.com';
          }  
          else {
            domain = window.location.hostname;
          }  
        }
        
        var secure = (argc > 5) ? argv[5] : false;

        // always permanent
        var today=new Date();
        var expTime=new Date();
        expTime.setTime(today.getTime()+3600000*24*expires*10)

        document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expTime.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
 
	// This cookie is history  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function regionInit(){
// read existing cookie
var ck = GetCookie('telusPortalRegion') + '';
ck = ck.toUpperCase();
if(ck=='NULL'){ck='';}
var pg = bpDoc.region.toUpperCase();
// do values for page and cookie match?

if(ck == pg && !(ck=='' && pg=='')){
// ...match...
// do nothing
// ...no match....
} else {
    // is the page a 'national' page?
    if(pg == 'NATIONAL'){
        // is the cookie value blank? If yes, set default value
        if(ck==''){
            SetCookie('telusPortalRegion', 'NS');
            ck = 'NS';
        }
    } else {
        if(ck=='' && pg==''){
            SetCookie('telusPortalRegion', 'NS');
            ck = 'NS';
        } else if (ck=='AB' && pg=='BC'){
            // leave the AB cookie if this is a BC page
            ck = 'AB';
        } else {
        // otherwise make the cookie equal to whatever the page is
        ck = pg;
        SetCookie('telusPortalRegion', ck);
        }
    }    
}
//set hidden vars
if(document.getElementById('telusPortalRegion')){
    document.getElementById('telusPortalRegion').value = ck.toUpperCase();
}
// This should already be set
/*if(document.getElementById('telusPortalSegment')){
    document.getElementById('telusPortalSegment').value = bpDoc.segment;
}*/
// set the drop down nav to the value of 'ck'
if(document.getElementById('dropRegion')){
    var rd = document.getElementById('dropRegion');
    for (var i = 0; i < rd.options.length; i++){
        if(rd.options[i].innerHTML.toUpperCase() == ck.toUpperCase()){
            rd.options[i].selected = "selected";
        }
    }
}

SetCookie('CustomerType','business');

if (document.location.href.indexOf('fr_CA') > -1)
    SetCookie('lang','fr');
else
    SetCookie('lang','en');
    
}

function doRegionJump(frm,sel){
    // set region cookie
    var rgn = sel.options[sel.selectedIndex].innerHTML.toUpperCase();
    // Set region cookie
    SetCookie('telusPortalRegion', rgn);
    // Set segment cookie
    SetCookie('telusPortalSegment', bpDoc.segment);
	// jump to the selected URL
	//document.location = sel.options[sel.selectedIndex].value;
    frm.action = sel.options[sel.selectedIndex].value;
	frm.submit();
    //return false;
}

/* ########################################################### */
/* vars */
var isIe = false;
var isOp = false;
var isSaf = false;

/* variable to know if IE */
if ($.browser.msie) { isIe = true; }
/* variable to know if Opera */
if ($.browser.opera) { isOp = true; }
/* variable to know if Safari */
if ($.browser.safari) { isSaf = true; }

/* ########################################################### */
/* sousmenu */
var sousmenu = {
	load : function() { // add action on submenu links
		$("#header .item-sm").hover(sousmenu.over, sousmenu.out);
		$("#header .sm .ls-sm li").click(sousmenu.smLink);
	},
	over : function() { // add class on hover on submenu links
		$(this).addClass("open");
	},
	out : function() { // remove class on hover on submenu links
		$("#header .item-sm").removeClass("open");
	},
	smLink : function () {
		leLink = $(this).children("h4").children("a").get("0");
		window.location = $(leLink).attr("href");
	}
};


/* ########################################################### */
/* business */

var business = {
	load : function () {
		var anchor = unescape(self.document.location.hash.substring(1));
		var bElem = 0;
		if (anchor == "Grandes-entreprises") {
			bElem = 1;
		} else if (anchor == "Industries") {
			bElem = 2;
		}
		
		$("#box-business .business-content").hide();
		$("#box-business .business-content:eq(" + bElem + ")").show();
		$("#box-business #business-nav li:eq(" + bElem + ")").addClass("selected");
		$("#box-business #business-nav li a").click(business.show);
	},
	show : function () {
		var anchor = unescape(self.document.location.hash.substring(1));
		if (anchor == "Industries") {
			$("#box-business #business-nav").addClass("indus");
		}else if($(this).attr("href") == "#Gouvernement") {
			/* document.location.href="http://business.telus.com/fr_CA/QC/sector/Medium_And_Large_Business/Government/qcMlbGovernment.html"; */
			document.location.href="http://business.telus.com/fr_CA/QC/sector/Medium_And_Large_Business/Public_Sector/qcMlbPublicSectorHome.html";
		}else{
			$("#box-business #business-nav").removeClass("indus");
		}
		
		if($(this).attr("href") != "#Gouvernement"){
			$("#box-business #business-nav li").removeClass("selected");
			$(this).parent().addClass("selected");
			
			$("#box-business .business-content").hide();
			var leindex = $("#box-business #business-nav li").index($(this).parent());
			$("#box-business .business-content:eq(" + leindex + ")").show();
		}
		return false;
	}
};

/* ########################################################### */
/* scrollbox */

var scrollbox = {
	load : function () {
		$(".scrollbox .items ul").hide();
		$(".scrollbox .items .action").click(scrollbox.show);
	},
	show : function () {
	
		leUl = $(this).parent().children("ul");
		if($(leUl).get("0") === undefined) { // if no list to show, follow link
			return true;
		}
		
		leLi = $(this).parent();
	
		if($(leLi).hasClass("open")) {
			$(leLi).removeClass("open");
			$(leUl).slideUp("fast");
			return false;
		}
		
		if($(".scrollbox .items li.open ul").get("0") == undefined) {
			$(leLi).addClass("open");
			$(leUl).slideDown("fast");
		} else {
			$(".scrollbox .items li.open ul").slideUp("fast", function () {
				$(".scrollbox .items li").removeClass("open");
				$(leLi).addClass("open");
				$(leUl).slideDown("fast");
			});
		}
		
		return false;
	}
};
	
/* ########################################################### */
/* ready fonction */

$(document).ready(function () {
	if($("#header .item-sm").get("0")) {
		sousmenu.load();
	}
	if($("#business-nav").get("0")) {
		business.load();
	}
	if($(".scrollbox").get("0")) {
		scrollbox.load();
	}
});


