/*
Programs:
	1 - READ 180
	2 - System 44
	3 - Scholastic Achievement Manager (SAM)               
	4 - FASTT Math
	5 - Scholastic Reading Counts!
	6 - Scholastic Reading Inventory                
	7 - Scholastic Phonics Inventory
	8 - Scholastic Math Inventory
	9 - Expert 21
	10 - ReadAbout
	11 - Other
	
Type:
	1 - How-Tos
	2 - Archived Training Webinars
	3 - Archived Author Webinars
	4 - Digital Overviews
	5 - Professional Articles
	6 - Teacher Resources
	7 - Leadership Resources
	
Topic:
	 1 - Instructional Routines
	 2 - Program Quick Tips
	 3 - Program Reference
	 4 - Classroom Management and Planning
	 5 - Assessment and Data Driven Instruction
	 6 - Instructional Software and Technology
	 7 - Teaching Strategies
	 
CA:
	1 - Reading
	2 - Writing
	3 - Phonics
	4 - Math
	5 - Social Studies
	6 - Science
	7 - ELA               
                    
*/


function Program(id,title,desription,assetType,assetLocation,questions,transcript,logo,program,type,topic,contentArea,related,available){
	this.id = id;  
	this.title = title;  
	this.description = desription;  
	this.assetType = assetType;  
	this.assetLocation = assetLocation;  
	this.questions = questions;  
	this.transcript = transcript;  
	this.logo = logo;  
	this.program = program;  
	this.type = type;  
	this.topic = topic;  
	this.contentArea = contentArea;  
	this.related = related;   
	this.available = available;
	this.priority = program.split(';').length;
}

Program.prototype.toHTML = function(index){
	var link = this.assetType == 'VIDEO' ? 'video.asp?video=' + this.id : this.assetLocation;
	var info1 = '<div class="info1"><img alt="" src="' + (this.assetType == 'VIDEO' ? 'public/images/ico-video.png' : 'public/images/ico-pdf.png') + '" class="ico"/>';
	//onclick = '<img src=images/DTZ_SAM_logo.jpg><img usemap=#login class=popup_login src=images/login_popup.jpg />';
	info1 += '<div class="pic"><img alt="" src="' + this.logo + '"/></div>';
	info1 += '<h2>' + this.type + '</h2>';
	info1 += '<span>'+ this.title +'&nbsp</span></div>';
	var info2 = '<p class="info2">' + this.description + '</p>';
	var html = (this.available == 0 ? '<div' : ('<a onclick=location.href="' + link + '"')  ) +' style="z-index:'+index+';" class="sample' + (this.available == '' ? '' : ' enabled_sample' ) +'">';
	html = html + '<div class="in'+((index==1 && this.available == '1')? ' in_right' : '')+'">';
	
	if(index==1) {
		html += info2;
		html += info1;
	} else {
		html += info1;
		html += info2;
	}
	html += '</div>';
	html += (this.available == 0 ? '</div>' : '</a>' );

	return html;
}

Program.prototype.toHTMLforVideo = function(position){
	var link = this.assetType == 'VIDEO' ? ('video.asp?video=' + this.id) : this.assetLocation;
	var html = (this.available == 0 ? '<div' : ('<a onclick=location.href="' + link + '"')  ) +' style="top:'+position+'px;left:38px;" id=program_' + this.id + ' class="sample'+(this.available == '' ? '' : ' enabled_sample' ) +'">';
    html = html + '<img alt="" src="' + (this.assetType == 'VIDEO' ? 'public/images/ico-video.png' : 'public/images/ico-pdf.png') + '" class="ico"/>';
	html = html + '<div class="pic"><img alt="" src="' + this.logo + '"/></div>';
	html = html + '<h2>' + this.type + '</h2>';
	html = html + '<span>'+ this.title +'</span>';
	html = html + (false && this.available == 0 ? '</div>' : '</a>' );
	return html;
}

Program.prototype.getQuestions = function(id) {
	return this.questions.split(';');
}

function Multibrowse(){
	this.programs = new Array();
	this.displayedPrograms = new Array();
}

Multibrowse.prototype.setPrograms = function(programsArray){
	this.displayedPrograms = programsArray;
	for(var i = 0;i < programsArray.length; i++){
		this.programs[programsArray[i].id] = programsArray[i];
	}
}

Multibrowse.prototype.displayAll = function(){
	var programs_array = new Array();
	for (var i = 0; i < this.programs.length; i++) {
		if (this.programs[i] != undefined) {			
			programs_array.push(this.programs[i]);
		}
	}
	this.displayedPrograms = programs_array;
}

Multibrowse.prototype.getProgramById = function(id){
	return this.programs[id];
}

Multibrowse.prototype.getProgramsByType = function(type){
	var programs_array = new Array();
	for (var i = 0; i < this.displayedPrograms.length; i++) {
		if (this.displayedPrograms[i] != undefined) {
			if (this.displayedPrograms[i].type.match(type)) {
				programs_array.push(this.displayedPrograms[i]);
			}
		}
	}
	this.displayedPrograms = programs_array;
	return programs_array;
}

Multibrowse.prototype.getProgramsByTopic = function(topic){
	var programs_array = new Array();
	for (var i = 0; i < this.displayedPrograms.length; i++) {
		if (this.displayedPrograms[i] != undefined) {
			if (this.displayedPrograms[i].topic.match(topic)) {
				programs_array.push(this.displayedPrograms[i]);
			}
		}
	}
	this.displayedPrograms = programs_array;
	return programs_array;
}

Multibrowse.prototype.getProgramsByProgramName = function(programName){
	var programs_array = new Array();
	for (var i = 0; i < this.displayedPrograms.length; i++) {
		if (this.displayedPrograms[i] != undefined) {
			if (this.displayedPrograms[i].program.match(programName)) {
				programs_array.push(this.displayedPrograms[i]);
			}
		}
	}
	this.displayedPrograms = programs_array;
	return programs_array;
}

Multibrowse.prototype.updateProgramsList = function(event) {
	var program_name = $('#program_dropdown ul li a.active:first').attr('title');
	var type = $('#type_dropdown ul li a.active:first').attr('title');
	var topic = $('#topic_dropdown ul li a.active:first').attr('title');
	var content_area = $('#ca_dropdown ul li a.active:first').attr('title');
	
	this.displayedPrograms = new Array();
	for(var i=0; i<this.programs.length; i++){
		if(this.programs[i]){
			this.displayedPrograms.push(this.programs[i]);
		}
	}

	if(program_name) {
		multibrowse.getProgramsByProgramName(program_name);
	}
	if(type) {
		multibrowse.getProgramsByType(type);
	}
	if(topic) {
		multibrowse.getProgramsByTopic(topic);
	}
	if(content_area) {
		multibrowse.getProgramsByContentArea(content_area);
	}
	if(!(program_name || type || topic || content_area)){
		return false;
	}else{
		this.sortPrograms();
		return true;
	}
}

Multibrowse.prototype.getRelatedForProgram = function(id){
	var related_array = new Array();
	if(this.getProgramById(id).related != null && this.getProgramById(id).related != ''){
		var related_ids_array = this.getProgramById(id).related.split(';');
		
		for (var i = 0; i < related_ids_array.length; i++) {
			related_array.push(this.getProgramById(related_ids_array[i]));
		}
	}
	return related_array;
}

Multibrowse.prototype.getProgramsByContentArea = function(contentArea){
	var programs_array = new Array();
	for (var i = 0; i < this.displayedPrograms.length; i++) {
		if (this.displayedPrograms[i] != undefined) {
			if (this.displayedPrograms[i].contentArea.match(contentArea)) {
				programs_array.push(this.displayedPrograms[i]);
			}
		}
	}
	this.displayedPrograms = programs_array;
	return programs_array;
}

Multibrowse.prototype.getProgramsForUsersLoggedIn = function(){
	var programs_array = new Array();
	for (var i = 0; i < this.programs.length; i++) {
		if (this.programs[i] != undefined) {
			if (this.programs[i].loggedIn == 1) {
				programs_array.push(this.programs[i]);
			}
		}
	}
	this.displayedPrograms = programs_array;
	return programs_array;
}

Multibrowse.prototype.compareProgramPriorities = function(p1,p2){
	if(p1 && p2){
		return (p1.available == p2.available) ? (p1.priority - p2.priority) : ((p1.available - p2.available)*(-1));		
		
		//return p1.priority - p2.priority;
	}else{
		return 1;
	}
}

Multibrowse.prototype.sortPrograms = function(){
	this.displayedPrograms = this.displayedPrograms.sort(this.compareProgramPriorities);
}
"T. 11/10/2009"
function getDateObject(date_string) {
	var date = new Date();
	if(date_string.split(' ')[1] != undefined) {
    var date_array = date_string.split(' ')[1].split('/');
		date.setDate(date_array[1]);
		date.setMonth(parseInt(date_array[0]) - 1);
		if(date_array[2].length > 2) {
			date.setYear(date_array[2]);
		} else {
			date.setYear('20'+date_array[2]);
		}
	} else {
		var date_array = date_string.split('/');
	
		date.setDate(date_array[2]);
		date.setMonth(parseInt(date_array[1]) -1);
		date.setYear(date_array[0]);
	}
	return date;
}

function sortBy_topic(a,b) {
	return (a.topic > b.topic ? 1 : -1 );
}

function sortBy_program(a,b) {
	return (a.program > b.program ? 1 : -1);
}

function sortBy_author(a,b) {
	return (a.author > b.author ? 1 : -1 );
}

function sortBy_date(a,b) {
	var a_date = getDateObject(a.date);
	var b_date = getDateObject(b.date);
	return (a_date > b_date ? 1 : -1);
}

Multibrowse.prototype.displayCalendarEntries = function(){
	var calendarContainer = $('#calendar_entries_container tbody');
	for(var i=0; i<calendarEntries.length; i++){
        calendarContainer.append(calendarEntries[i].toHTML(i));
    }
	
	$('.more').mouseover(showMore);
	$('.more').mouseout(hideMore);
}

Multibrowse.prototype.displayAuthorWebinars = function(){
	
	if(authorWebinarEntries.length > 0) {
		var authorWebinarContainer = $('#author_webinar_container tbody');
		for(var i=0; i<authorWebinarEntries.length; i++){
			authorWebinarContainer.append(authorWebinarEntries[i].toHTML(i));
		}
	} else {
		var authorWebinarContainer = $('#author_webinar_container');
		authorWebinarContainer.append('<p>There are no events scheduled at this time. Please check back to register for future events.</p>');
	}
		
	$('.more').mouseover(showMore);
	$('.more').mouseout(hideMore);
}

Multibrowse.prototype.displayAuthorWebinarsPromos = function() {
	var authors_on_the_list = new Array();

	if(authorWebinarEntries.length > 0) {
		var authorWebinarPromosContainer = $('#author_info');
		for(var i=0; i<authorWebinarEntries.length; i++){
			authorWebinarPromosContainer.append(authorWebinarEntries[i].toHTML_forPromoSpace(i,false));
			authors_on_the_list.push(authorWebinarEntries[i].author);
		}
		$('#author_space').show();
	}
}

Multibrowse.prototype.sortCalendarEntries = function(columnName){
	var calendarContainer = $('#calendar_entries_container tbody');
	calendarContainer.empty();
	eval('calendarEntries.sort(sortBy_'+columnName+')');

	multibrowse.displayCalendarEntries();
}

Multibrowse.prototype.sortAuthorWebinars = function(columnName){
	var authorWebinarContainer = $('#author_webinar_container tbody');
	authorWebinarContainer.empty();
	eval('authorWebinarEntries.sort(sortBy_'+columnName+')');

	multibrowse.displayAuthorWebinars();
}
