/* EPG channel object */

function EPG_Channel(chName,chKey,chNumber,chTag,chShortDesc,chLink,chLogo,chBigLogo,epgObj){
	this.channelName = chName;
	this.channelKey = chKey;
	this.channelNumber = chNumber;
	this.channelTag = chTag;
	this.channelShortDesc = chShortDesc;
	this.channelLongDesc = '';
	this.channelLink = chLink;
	this.channelLogoPath = chLogo;
	this.programCollection = new Object();
	this.detailsLoaded = false;
	this.parentObj = epgObj;
	this.detailsVisible = false;
	this.channelBigLogoPath=chBigLogo;
}

EPG_Channel.prototype.isDetailsLoaded = function() {
	if(this.detailsLoaded == true)
		return true;
	else
		return false;
}

EPG_Channel.prototype.getChannelsDetails = function(div_id) {

	var obj = this;

	var handlerFunc = function(t) {
		var JSON_string = t.responseText;	
		var myObject = eval('(' + JSON_string + ')');
		var number = myObject.channel.number;
		obj.channelLongDesc = myObject.channel.shortDescription;		
		obj.printDetails(div_id);			
	}
	
	var errFunc = function(t) {
	    alert('Error ' + t.status + ' -- ' + t.statusText);
	}

	new Ajax.Request('/epg.channel_details.xmc', {asynchronous:true, parameters:'channelNum=' + this.channelNumber, onSuccess:handlerFunc, onFailure:errFunc});
	this.detailsLoaded = true;
}

EPG_Channel.prototype.printDetails = function(div_id) {

	$(this.channelNumber + '-channel-cell').style.background = "url('/images/epg/bkgd/channel-bg-selected.gif')";    

	parent_div = $(div_id);
	parent_div.innerHTML = '';
	parent_div.style.backgroundColor = '';

	detailsTitle = document.createElement('span');
	detailsTitle.className = 'details-title';
	detailsTitle.innerHTML = this.channelTag;
	
	hideDetails = document.createElement('span');
	hideDetails.className = 'hide-details';
	clickevent = "document.getElementById('" + div_id + "').style.display = 'none'; unselectCells();";	
	hideDetails.innerHTML = '<img onClick="' + clickevent + '" src="/images/epg/btn/x.jpg">';
	hideDetails.src="/images/epg/btn/details-arrow.gif";
	
	parent_div.appendChild(detailsTitle);
	parent_div.appendChild(hideDetails);

	detailsInfo = document.createElement('p');
	detailsInfo.className = 'details-info';
	detailsInfo.innerHTML = this.channelLongDesc;
	parent_div.appendChild(detailsInfo);
	
	detailsNav = document.createElement('p');
	detailsNav.className = 'details-nav';
	
	viewChannelPage = document.createElement('a');
	viewChannelPage.innerHTML = 'View Channel Page';
	viewChannelPage.href = this.channelLink;

	pipe = document.createElement('span');
	pipe.innerHTML = ' | ';

	if (isFavorite(this.channelNumber))
		addFav_html = 'Already a Favorite: <a href="#nogo" onclick="addRemoveFavoriteChannel(' + this.channelNumber + ')" id="' + this.channelNumber + '_favorite_link">Edit My Favorite Channels</a>';

	else
		addFav_html = '<a href="#nogo" onclick="addRemoveFavoriteChannel(' + this.channelNumber + ')" id="' + this.channelNumber + '_favorite_link">Add To My Favorite Channels</a>';

	
	detailsNav.appendChild(viewChannelPage);
	detailsNav.appendChild(pipe);	
	detailsNav.innerHTML += addFav_html;
	
	parent_div.appendChild(detailsNav);
	
	var scrollValue = document.getElementById('epg_grid_scroll').scrollTop;	

	if((parent_div.offsetTop - scrollValue) >= 150) {
		document.getElementById('epg_grid_scroll').scrollTop = scrollValue + ((parent_div.offsetTop - scrollValue) - 150);
	}
}


EPG_Channel.prototype.showDetails = function(div_id) {

	hideAllChannels();
    unselectCells();

	parent_div = document.getElementById(div_id);
	parent_div.style.display = 'block';
	parent_div.innerHTML = '<center><img src="/images/epg/60x51_loading.gif"></center>';
	parent_div.style.backgroundColor = '#333';
	if (this.detailsLoaded == false)
		this.getChannelsDetails(div_id);
	else
		this.printDetails(div_id);
		
	this.detailsVisible = true;

	
}

EPG_Channel.prototype.addProgram = function(pgName,pgKey,pgScheduleId,pgLink,pgShortDesc,pgLongDesc,pgStartTime,pgEndTime,pgDuration) {
	if(typeof this.programCollection[pgKey] == "undefined"){
		this.programCollection[pgKey] = new EPG_Program(pgName,pgKey,pgScheduleId,pgLink,pgShortDesc,pgLongDesc,pgStartTime,pgEndTime,pgDuration,this);
	}	
}

EPG_Channel.prototype.getProgram = function(pgKey) {
	return this.programCollection[pgKey];
}