var theord = Math.ceil( Math.random() * 1000000000 ).toString();
//document.domain="muchmusic.com";
var brand = 2;
var vWin
function openwindow(){
	vWin = window.open('http://webservices.muchmusic.com/connect/frontend/update.php?brand=2','profile','scrollbars=1,resizable=0,width=790, height=420');
}
function tWin() {
	if (vWin.closed) {
		try{authenticationChange();}catch(err){}
		try{headerSessionChange();}catch(err){}
	}
	else {
		setTimeout("tWin()", 500);
	}
}
var vWin2
function openwindow2(){
	vWin2 = window.open('http://webservices.muchmusic.com/connect/frontend/login.php?brand=2','login','scrollbars=1,resizable=0,width=790, height=420');
}
function tWin2() {
	if (vWin2.closed) {
		try{authenticationChange();}catch(err){}
		try{headerSessionChange();}catch(err){}
	}
	else {
		setTimeout("tWin2()", 500);
	}
}
function logout(){
	delCookie("mml");
	delCookie("mmd");
	delCookie("ts");
	delCookie("tt");
	$("#loginState").replaceWith("<a id=\"loginState\" href=\"#\" onclick=\"openwindow2();tWin2(); return false\">Login or Logout</a>");
	$("#loginState").css('background-image', "url(http://www.muchmusic.com/mmconnect/images/login-top.png)");
	$("#loginState").css('width', "145px");

	//reloadpage();
	try{headerSessionChange();}catch(err){}
	//sessions.js session change function
	try{authenticationChange();}catch(err){}
	return false;
}
function headerSessionChange(){
	$("#NavigationBar #Search #searchKeyword").css("width", "137px"); 
	$("#NavigationBar #Search #searchKeyword").css("margin-right", "20px"); 
	$("#NavigationBar #Search #searchKeyword").css("float", "left"); 

	if (getCookie("mml")!=null && getCookie("mml")!=""){
		var username=getCookie("mmd").substring(0,getCookie("mmd").indexOf('|'));
		var userimage=getCookie("mmd").substring(getCookie("mmd").indexOf('|')+1);

		$("#loginState").replaceWith("<div id='loginState' style='text-indent: 0;'><img src='" + userimage + "' style='width:22px; height: 22px; padding: 1px 0 0 1px; margin-right: 3px;'> <div class='welcome' style='width: 61px;'>"+username+"<br/><a onclick=\"openwindow();tWin();return false\" style='color: #ff1f44;' href=\"#\">Edit profile</a> </div> <a href=\"#\" onclick='logout()' class='logout' style='display: block; float: left; text-indent: -9999px; width: 50px; height: 24px;'></a> </div>");
		$("#loginState").css('background-image', "url(http://www.muchmusic.com/mmconnect/images/logout.png)");
		$("#loginState").css('width', "145px");
	} else{
		//if the user is not logged in
		$("#loginState").replaceWith("<a id=\"loginState\" href=\"#\" onclick=\"openwindow2();tWin2(); return false\">Login or Logout</a>");
		$("#loginState").css('background-image', "url(http://www.muchmusic.com/mmconnect/images/login-top.png)");  
		$("#loginState").css('width', "145px");
	}

}
$(document).ready(function(){
	headerSessionChange();
});	
