var placement="";
var rootUrl="";

function buildGlobalNav(model)
{
	var html;     
	html = model.PrimaryNavItemListHtml;
	var globalnav_section = document.getElementById('primaryNavigation');
	globalnav_section.innerHTML = html;
	fixUrls();
}
function isInternalServer(){
	var serverList = ['172.20.20.24', 'betcma01', 'betcma01.web.bet.com', 'stage.bet.com',
				  'betdevimage01' ,'localhost' , '127.0.0.1'];
	var _IsInternalServer = false;
	for (i=0; i<serverList.length; i++){
		// test if the host server is internal or public.
		if(location.host == serverList[i]){
			_IsInternalServer = true;
		}
	}
	return _IsInternalServer;
}
function fixUrls(){
	if(rootUrl!="" && rootUrl!=null){
		jQuery.each(jQuery("#primaryNavigation a"), function(){
			var link = jQuery(this).attr("href");	
			if(navigator.appName.search("Microsoft")!=-1){
				var slashindex = link.substr(7).indexOf("/")+7;
				if(link.substring(0,slashindex).search("http://www.bet.com")!=-1){
					link = link.substr(slashindex);
					link = rootUrl+link;
					jQuery(this).attr("href", link);
				}
				else if((link.search("http://betboards.bet.com")>-1)||(link.search("http://mobile.bet.com")>-1)||(link.search("http://shine.bet.com")>-1)||(link.search("http://yaheard.bet.com")>-1)||(link.search("http://buppies.bet.com")>-1)){
					
				}
				else if(link.substring(0,slashindex).search("http://www.bet.com")==-1){
					link = link.substr(slashindex);
					link = rootUrl+link;
					jQuery(this).attr("href", link);
				}
			}
			else{
				if(link.search("http://")==-1){
					jQuery(this).attr("href", rootUrl+link);	
				}
			}
		});
	}
	else{
		if(isInternalServer()){
			jQuery.each(jQuery("#primaryNavigation a"), function(){
				var link = jQuery(this).attr("href");	
				if(navigator.appName.search("Microsoft")!=-1){
					var slashindex = link.substr(7).indexOf("/")+7;
					if((link.search("http://betboards.bet.com")>-1)||(link.search("http://mobile.bet.com")>-1)||(link.search("http://shine.bet.com")>-1)||(link.search("http://yaheard.bet.com")>-1)||(link.search("http://buppies.bet.com")>-1)){
						link = link.substr(slashindex);
						link = "/www.bet.com"+link;
						jQuery(this).attr("href", link);
					}
				}
				else{
					if(link.search("http://")==-1){
						jQuery(this).attr("href", "/www.bet.com"+link);	
					}
				}
			});
		}
	}

}
function get_GNav() {
	var script=document.createElement('script');
	script.src="http://www.bet.com/services/nav/navmodelservice.aspx?";
	script.src+="callback=buildGlobalNav";
	script.type="text/javascript";
	document.getElementsByTagName('head')[0].appendChild(script);
}
function get_GNav(channel) {
	if(channel!=null){
		placement=channel;
	}
	var script=document.createElement('script');
	script.src="http://www.bet.com/services/nav/navmodelservice.aspx?";
	script.src+="callback=buildGlobalNav";
	script.type="text/javascript";
	document.getElementsByTagName('head')[0].appendChild(script);
}
function get_GNav(channel, use_absolute) {
	if(channel!=null){
		placement=channel;
	}
	if(use_absolute!=null && use_absolute==true){
		rootUrl="http://www.bet.com";
	}
	var script=document.createElement('script');
	script.src="http://www.bet.com/services/nav/navmodelservice.aspx?";
	script.src+="callback=buildGlobalNav";
	script.type="text/javascript";
	document.getElementsByTagName('head')[0].appendChild(script);
}



function removeWhiteSpace(str){
	if (str!=null){	
		return str.replace(" ","");
	}
}
function ontvMarkupBuilder(){
	var bet_shows = jQuery('#primaryNavigation div.showsTable dl.betshows');
	bet_shows.wrap('<div class="col-2 betshows"</div>');
	bet_shows.before('<span class="title">BET Shows</span>');
	//bet_shows.after('<div class="linkContainer"><a class="more" href="/ontv/betshows/">All BET Shows</a></div>');
	bet_shows.addClass("secondCol");
	bet_shows.before('<dl class="level2 only betshows firstCol"><dt></dt><dd><ul></ul></dd></dl>');
	
	var bet_shows_col1 = jQuery('#primaryNavigation div.showsTable div.betshows dl.firstCol ul');
	var bet_shows_col2 = jQuery('#primaryNavigation div.showsTable div.betshows dl.secondCol ul');
	var rowCount=0;
	var counter=0;
	
	if(bet_shows_col2.children().size()%2==1){
		rowCount = (bet_shows_col2.children().size()+1)/2;
	}
	else{
		rowCount = bet_shows_col2.children().size()/2;
	}
	while(bet_shows_col2.children().size() > rowCount)
	{
		bet_shows_col1.append(jQuery('#primaryNavigation div.showsTable div.betshows dl.secondCol ul li:first'));
		counter++;
	}
	
	var betj_shows = jQuery('#primaryNavigation div.showsTable dl.betjshows')
	betj_shows.wrap('<div class="col-1 betjshows"</div>');
	betj_shows.before('<span class="title">BETJ Shows</span>');
	//betj_shows.after('<div class="linkContainer"><a class="more" href="/ontv/betjshows/">All BETJ Shows</a></div>');
	
	var specials_shows = jQuery('#primaryNavigation div.showsTable dl.specials')
	specials_shows.wrap('<div class="col-1 specials"</div>');
	specials_shows.before('<span class="title">Specials</span>');
	//specials_shows.after('<div class="linkContainer"><a class="more" href="/ontv/specials/">All Specials</a></div>');
	
	
	jQuery('#primaryNavigation div.showsTable').append('<div class="moreContainer"></div>');
	if(isInternalServer()){
		jQuery('#primaryNavigation div.showsTable div.moreContainer').append('<div class="linkContainer spans-2"><a class="more" href="/ontv/betshows/">All BET Shows</a></div>');
		//jQuery('#primaryNavigation div.showsTable div.moreContainer').append('<div class="linkContainer"><a class="more" href="/ontv/betjshows/">All BETJ Shows</a></div>');
		jQuery('#primaryNavigation div.showsTable div.moreContainer').append('<div class="linkContainer"><a class="more" href="/ontv/specials/">All Specials</a></div>');
	}
	else {
		jQuery('#primaryNavigation div.showsTable div.moreContainer').append('<div class="linkContainer spans-2"><a class="more" href="http://www.bet.com/ontv/betshows/">All BET Shows</a></div>');
		//jQuery('#primaryNavigation div.showsTable div.moreContainer').append('<div class="linkContainer"><a class="more" href="http://www.bet.com/ontv/betjshows/">All BETJ Shows</a></div>');
		jQuery('#primaryNavigation div.showsTable div.moreContainer').append('<div class="linkContainer"><a class="more" href="http://www.bet.com/ontv/specials/">All Specials</a></div>');
	}
	//var markup;
	//var betshow_count = howManyInList(bet_shows);
	
	
}

jQuery(document).ready(function() {
	var toplevel = jQuery("#primaryNavigation dl.level0 li").not(jQuery("dl.level1 li"));
	jQuery.each(toplevel, function(){
		var channel = removeWhiteSpace(jQuery(this).children().html()).toLowerCase();
		jQuery(this).addClass(channel);
		var children = jQuery(this).children();
		jQuery.each(children, function(){
			jQuery(this).addClass(channel);
		});
	});
	
	if(placement!="" && placement != null){
		jQuery("#primaryNavigation .navMenu ul:first li."+removeWhiteSpace(placement).toLowerCase()).addClass("selected");
		/*jQuery("#primaryNavigation .navMenu ul:first").addClass(removeWhiteSpace(placement).toLowerCase());*/
	}
	if(rootUrl!="" && rootUrl!=null){
		/*if(navigator.appName.search("Microsoft")!=-1){
			jQuery.each(jQuery("#primaryNavigation a"), function(){
				var link = jQuery(this).attr("href");
				alert(link);
				var slashindex = link.substr(7).indexOf("/")+7;
				link = link(slashindex);
				jQuery(this).attr("href", rootUrl+link);
			});
		}
		else{
			jQuery.each(jQuery("#primaryNavigation a"), function(){
				jQuery(this).attr("href", rootUrl+jQuery(this).attr("href"));
			});
		}*/
	}
    jQuery("#primaryNavigation ul li").hover(function () {
		jQuery(this).addClass("hover");            
    },
    function () {        
       	jQuery(this).removeClass("hover");
    });
        
    jQuery("#primaryNavigation ul li ul li").hover(function () {
		jQuery(this).addClass("hover");            
    },
    function () {        
       	jQuery(this).removeClass("hover");
    });
	
	jQuery.each(jQuery("#primaryNavigation dl.navMenu li:has(dl)"), function() {
			if((jQuery(this).html().toLowerCase()).indexOf('<li')!=-1)
			{
				jQuery(this).addClass("hasChildren");
			}	
			else
			{
				jQuery(this).children().next().remove();
			}
	});
	jQuery.each(jQuery("#primaryNavigation li.ontv li.hasChildren dl"), function(){
		var subchannel = removeWhiteSpace(jQuery(this).siblings().html()).toLowerCase();
		jQuery(this).addClass(subchannel);
	});
	jQuery("#primaryNavigation li.ontv dl.level1").after('<div class="showsTable"></div>');
	jQuery("#primaryNavigation li.ontv div.showsTable").append(jQuery("#primaryNavigation li.ontv dl.level2"));
	
	
	ontvMarkupBuilder();
});
