// attempt to find the club id 
// - either from the cid, or the sid if unavailable
function getCid() {
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split("&");
    var cid;

    for (var i=0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) { continue; }

        var argname = pairs[i].substring(0, pos);
        var value   = pairs[i].substring(pos + 1);
        args[argname] = unescape(value);

    }
    if (args.cid) { 
        cid = args.cid;
    }
    if ((args.sid) && (args.sid != "milb")) {
        cid = args.sid;
    }
    if (cid) {
       if (cid.charAt(0) != "t") { cid = "t" + cid };  // make sure we're always matching against full club ids
       return cid; 
    } else { 
        return false;
    }
}

function processDateSelection(startDate, nextDate, nextMonth, dateString, dateSelector) {
        // this is a bit of a hack - we need to know which objects to use
        // based on what name we're created with.
        // 
        // this is to handle the fact that we can have multiple 
        // media grids, date selectors, and xmlhttprequest managers.

        var gridObj, dateId, mediaType, endDate;
        if (dateSelector.indexOf("video") > -1) {
            gridObj   = videoGrid;
            dataObj   = videoDataSource;
            dateId    = "videoDateInfo";
            mediaType = "video";
        } else if  (dateSelector.indexOf("audio") > -1) {
            gridObj   = audioGrid;
            dataObj   = audioDataSource;
            dateId    = "audioDateInfo";
            mediaType = "audio";
        } 

        if (gridObj.period == "month") {
            endDate = nextMonth;
        } else { 
            endDate = nextDate;
        }

        // insert the new date in to the grid tab, 
        // show the data as loading 
        // and clear out the grid data from the MediaGrid object
        document.getElementById(dateId).innerHTML = dateString;
        document.getElementById(gridObj.target).innerHTML = "<div style='padding: 10px; padding-left: 40px; font-weight: bold; border: 1px solid #666666;'>Loading ...</div>";
        // clear out the media data 
        gridObj.data = null;

        // setup the data fetcher and invoke it

        // setup query string of param name/value pairs
        var requestURL = "/multimedia/fetchgriddata.jsp?arrayName=media";
        var param = new Object();
        param.names  = new Array("startDate", "endDate", "mediaType");
        param.values = new Array(startDate, endDate, mediaType);

        // if the grid is for a specific team, then append the team id to
        // the query
        if (gridObj.clubid) {
			var teamId = gridObj.clubid;
			
	        if (teamId.indexOf("l") == -1) {
	            param.names[param.names.length]   = "teamId";
	        }
			else if (teamId.indexOf("l") == 0){
				teamId = teamId.substring(1);
	            param.names[param.names.length]   = "leagueId";
			}

            param.values[param.values.length] = teamId;
			
			// all video will be made available in 2008, was restricted to home
			// team in previous seasons
			/* 
            if (mediaType == "video") {
                param.names[param.names.length]   = "teamLocation";
                param.values[param.values.length] = "home";
            }
			*/
        }
		

        // append them to the base url - we have already appended one, 
        // so we can just "&" the rest
        for (var i=0; i<param.names.length; i++) {
            requestURL += "&" + param.names[i] + "=" + param.values[i];
        }
        // go get it!
        dataObj.request(requestURL);
}

try {  // setup date selector for the date popup 

    DateSelector.prototype.processSelection = function(date) {

        var startDate  = this.getDataDateString(this.selectedDates[0]);
        var nextDate   = this.getNextDateString(this.selectedDates[0]);
        var nextMonth  = this.getNextMonthString(this.selectedDates[0]);
        var dateString = this.getDateString(this.selectedDates[0]);

        processDateSelection(startDate, nextDate, nextMonth, dateString, this.name);
    
        // for single date selection
        this.selectedDates = new Array(); // clear array
        this.hide();    
    }

    DateSelector.prototype.getDataDateString = function(date) {
        return date.substr(6,2) + "/" + date.substr(4,2) + "/" + date.substr(0,4);
    }

    DateSelector.prototype.getNextDateString = function(date) {
        var nextDate = new Date();
        nextDate.setUTCFullYear(date.substr(0,4), (this.removeLeadingZero(date.substr(4,2)) -1), this.removeLeadingZero(date.substr(6,2)));
        with (nextDate) setDate(getDate()+1);
        return this.getTwoDigit(nextDate.getUTCDate()) + "/" + this.getTwoDigit(nextDate.getUTCMonth() +1) + "/" + nextDate.getUTCFullYear();
    }
		
		DateSelector.prototype.getSecondDayAfterDateString = function(date) {
        var nextDate = new Date();
        nextDate.setUTCFullYear(date.substr(0,4), (this.removeLeadingZero(date.substr(4,2)) -1), this.removeLeadingZero(date.substr(6,2)));
        with (nextDate) setDate(getDate()+2);
        return this.getTwoDigit(nextDate.getUTCDate()) + "/" + this.getTwoDigit(nextDate.getUTCMonth() +1) + "/" + nextDate.getUTCFullYear();
		}

    DateSelector.prototype.getNextWeekString = function(date) {
        var nextWeek = new Date();
        nextWeek.setUTCFullYear(date.substr(0,4), (this.removeLeadingZero(date.substr(4,2)) -1), this.removeLeadingZero(date.substr(6,2)));
        with (nextWeek) setDate(getDate()+7);
        return this.getTwoDigit(nextWeek.getUTCDate()) + "/" + this.getTwoDigit(nextWeek.getUTCMonth() +1) + "/" + nextWeek.getUTCFullYear();
    }

    DateSelector.prototype.getNextMonthString = function(date) {
        var year  = date.substr(0,4); 
        var month = parseInt(this.removeLeadingZero(date.substr(4,2)), 10) + 1;
        var day   = date.substr(6,2);
        if (month == 13) {
            month = 1;
            year++;
        }
        return this.getTwoDigit(day) + "/" + this.getTwoDigit(month) + "/" + year;
    }

    DateSelector.prototype.doFirstThenShow = function(obj, event) {
        this.showResetLink = false;
        this.show(this.month, this.year);
    }

    // on click, set the mouse position to show the dateSelector
    DateSelector.prototype.setPosition = function(obj, event) {
        if (typeof window.scrollX != "undefined") {     // the firefox way
            this.node.style.top  = event.clientY + window.scrollY + "px";
            this.node.style.left = event.clientX + window.scrollX + "px";
        } else if (typeof document.body.scrollLeft != "undefined") {
            this.node.style.top  = event.clientY + document.documentElement.scrollTop  + document.body.scrollTop  + "px";
            this.node.style.left = event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft + "px";
        }
    }

} catch (e) { /* failed to customize DateSelector - is it already created? */ };

try {

    MonthSelector.prototype.doFirstThenShow = function(obj, event) {
        this.show();
    }

    MonthSelector.prototype.processSelection = function(month){
        var months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]          
        var year = this.curYear;
        var startDate  = "01/" + this.getTwoDigitValue(month) + "/" + year;
        var dateString = months[month] + "&nbsp;" + year;

        month++;
        if (month == 13) {
            month = 1;
            year++;
        }

        var nextDate   = "01/" + this.getTwoDigitValue(month) + "/" + year;
        var nextMonth  = "01/" + this.getTwoDigitValue(month) + "/" + year;

        processDateSelection(startDate, nextDate, nextMonth, dateString, this.selfName);
    
        this.hide();
    }

    MonthSelector.prototype.setPosition = function(obj, event) {
        var container = document.getElementById(this.containerId);
        if (typeof window.scrollX != "undefined") {     // the firefox way
            container.style.top  = event.clientY + window.scrollY + "px";
            container.style.left = event.clientX + window.scrollX + "px";
        } else if (typeof document.body.scrollLeft != "undefined") {
            container.style.top  = event.clientY + document.documentElement.scrollTop  + document.body.scrollTop  + "px";
            container.style.left = event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft + "px";
        }
    }


} catch(e) { /* failed to customize MonthSelector - is it already created? */ };
