// Create homepage namespace
var MiLBHome = (typeof MiLBHome=="object") ? MiLBHome : {};

// prints milb headlines from array
function milbHeadlines(numHeads) {
    if (!headlines) { return ; }
    if (headlines[0]) { 
        document.write('<ul class=\"headlines\">');
        for (i=0; i<numHeads; i++) {
            if (headlines[i]) {
                document.write('<li>' + headlines[i] + '</li>');
            }
        }
        document.write('</ul>');
    }
}

function writeSelect(arr, name, onchange, cssClass, initialElementlabel, selectedValue) {
    var d = document;
    d.writeln("<select onchange=\"" + onchange + "\" class=\"" + cssClass + "\" name=\"" + name + "\">");
    if (initialElementlabel!=null) {
        d.writeln("<option value=\"\">" + initialElementlabel + "</option>");
    }
    if (name == "clubs") {
        d.writeln("<option value=\"\" disabled>----------</option>");
        d.writeln("<option value=\"/milb/info/affiliations.jsp\">By Affiliation</option>");
        d.writeln("<option value=\"/milb/info/classifications.jsp\">By Class</option>");
        d.writeln("<option value=\"/milb/info/geographical.jsp\">By Geography</option>");
        d.writeln("<option value=\"\" disabled>----------</option>");
    }
    
    for (var i=0; i<arr.length; i++) {
        d.writeln("<option " + (selectedValue!=null && arr[i][0]==selectedValue?"selected":"") + " value=\"" + arr[i][0] + "\">" + arr[i][1] + "</option>");
    }
    d.writeln("</select>");
}

function getClubPage(selectObj, rootUrl) {
		if (typeof(rootUrl) == "undefined" || rootUrl == null){ rootUrl = ""; }
//	var is_milb=rootUrl.indexOf('milb.com');
//	if (is_milb==-1){rootUrl = "http://www.milb.com";}
rootUrl = "http://www.milb.com";
    if (selectObj.options[selectObj.selectedIndex].value!="") {
        if (selectObj.options[selectObj.selectedIndex].value.indexOf("/")!=0) {
            location.href=rootUrl+"/clubs/index.jsp?cid="+selectObj.options[selectObj.selectedIndex].value;
        }
        else {
            location.href=rootUrl+selectObj.options[selectObj.selectedIndex].value;
        }
    }
    else {
        selectObj.selectedIndex=0;
    }
}

function getLeaguePage(selectObj, rootUrl) {
		if (typeof(rootUrl) == "undefined" || rootUrl == null){ rootUrl = ""; }
//	var is_milb=rootUrl.indexOf('minorleaguebaseball.com');
//	if (is_milb==-1){rootUrl = "http://www.milb.com";}
rootUrl = "http://www.milb.com";
    if (selectObj.options[selectObj.selectedIndex].value!="") {
        if (selectObj.options[selectObj.selectedIndex].value.indexOf("/")!=0) {
            location.href=rootUrl+"/leagues/index.jsp?lid="+selectObj.options[selectObj.selectedIndex].value;
        }
        else {
            location.href=rootUrl+selectObj.options[selectObj.selectedIndex].value;
        }
    }
    else {
        selectObj.selectedIndex=0;
    }
}

function sortDropdown(a,b) { 	 
     if (a[1] > b[1]) { 
	 	return  1 
	}; 	 
     if (a[1] < b[1]) { 
	 	return -1 
	}; 	 
     return 0; 	 
}


if (MPlayer) { 
    MPlayer.preLaunch = function(o){
        
        var arl_w_video = "mms://a1272.v10869.g.vm.akamaistream.net/7/1272/10869/v0001/mlb.download.akamai.com/10869";
        var arl_w_audio = "mms://a1272.v10869.g.vm.akamaistream.net/7/1272/10869/v0001/mlb.download.akamai.com/14668";
        var live_arl_w  = "mms://a1503.l10869[PORT].c10869.g.lm.akamaistream.net/D/1503/10869/v0001/reflector:[PORT]";
        
        if( o["w"] && o["w"].indexOf("reflector:")>-1 ){
            var reflector = o.w.match(/reflector\:(\d{5})/);
            var url = live_arl_w.replace(/\[PORT\]/g, reflector[1]);
            o.w = url;
        } else if ((o.w) && (o.w.indexOf("://")<0)) {   // if we have url w/o a server in the path add on the 'magic string'!
            if (o.type) { 
                if (o.type == "v_free") { o.w = arl_w_video + o.w; }
                if (o.type == "a_free") { o.w = arl_w_audio + o.w; }
            } 
        }
        return true;
        
    }
}


MiLBHome.initAroundTheHorn = function(){
	var pageNumberColor_on   = "#000000",
		pageNumberColor_off  = "#B3B3B3",
		maxItemsInCarousel   = 9;
	var listItems = $("#AroundTheHornItems li");
	if(listItems.length>maxItemsInCarousel){
		var extraListItems = listItems.slice(maxItemsInCarousel);
		extraListItems.each(function(){$(this).remove();});
	}
    $("#AroundTheHornItems").jCarouselLite({
        btnPrev  : "#AroundTheHornItems_news_prev",
        btnNext  : "#AroundTheHornItems_news_next",
		scroll   : 1,
		visible  : 1,
		speed    : 350,
		btnGo: [
			"#AroundTheHornItems_pagination .p1",
			"null",
			"null"
		],
		beforeStart : function(item, isAutoScroll){
			$("#AroundTheHornItems_pagination a").css({ "color" : pageNumberColor_off });
		},
		afterEnd : function(item, isAutoScroll){
			var itemsPerPage = 1; //should be same as visible setting
			var allItems = $(item[0]).parent().children();
			var length = allItems.length;
			var itemNum = allItems.index(item[0])+1;
			itemNum = itemNum<=itemsPerPage
						? length-itemNum-itemsPerPage*2
						: itemNum>=length-itemsPerPage
							? itemNum-(length-itemsPerPage) 
							: itemNum-itemsPerPage;
			var page = parseInt(itemNum/itemsPerPage) + (itemNum%itemsPerPage ? 1 : 0);
			$("#AroundTheHornItems_pagination .p"+page).css({ "color" : pageNumberColor_on });
		}
    });	
	// Hover button change.
	$("#AroundTheHornItems_news_prev").hover(
		function(){ $(this).css({ "background-position" :"0 -21px" }); },
		function(){ $(this).css({ "background-position" :"0 0" }); }
	);
	$("#AroundTheHornItems_news_next").hover(
		function(){ $(this).css({ "background-position" :"-48px -21px" }); },
		function(){ $(this).css({ "background-position" :"-48px 0" }); }
	);	
};

