/*
 * Copyright 2011 1fb.net Financial Services.
 *
 * This document may not be reproduced, distributed or used in any manner whatsoever without the expressed written
 * permission of 1st Financial Bank USA.
 */

/**
 * @fileOverview This file contains common processes for all College Profile pages.
 * @author Mike Yao, Garrett Li, Ray Chen
 * @version 1.2
 * @since 6.00, 2009-09-10
 */

var SpotLight = new Class({
	initialize: function() {
		if($('textWrap') != null)
		{
			this.tabIndex = location.pathname.charAt(28);
			this.showText();
		}
		if($('showcase') != null)
		{
			this.index = 1;
			this.current = 1;
			this.allPhotos = allPhoto.split(";");
			this.max = this.allPhotos.length;
			if(this.max>1)
			{
				this.pre = $('sp_btns').getFirst();
				this.pre.addClass("disbtn");
				this.next = $('sp_btns').getLast();
				this.page = $('sp_btns').getElement('b');
				this.pre.addEvent('click',this.changePic.bindWithEvent(this));
				this.next.addEvent('click',this.changePic.bindWithEvent(this));
			}
			else
			{
				this.pre = null;
				this.next = null;
				this.page = null;
			}

			if(window.navigator.userAgent.indexOf('MSIE 6') != -1)
			{
				for(var i =1; i< this.max; i++)
				{
					this.loadNextPic();
				}
			}
			else
			{
				this.loadNextPic();
				this.loadNextPic();
				this.loadNextPic();
			}
		}
	},
	changePic: function(e){
		if(this.current >= 1 && this.current <=this.max){
			if(e.target==this.next){
				if(this.current == this.max) return;
				$('imgs').getElements('img')[this.current-1].setStyle("display","none");
				$('imgs').getElements('img')[this.current].setStyle("display","block");
				if(window.navigator.userAgent.indexOf('MSIE 6') == -1)
				{
					this.loadNextPic();
				}
				this.current+=1;
			}
			else if(e.target==this.pre){
				if(this.current == 1) return;
				$('imgs').getElements('img')[this.current-1].setStyle("display","none");
				$('imgs').getElements('img')[this.current-2].setStyle("display","block");
				this.current-=1;
			}

			if(this.current>1&&this.current<this.max){
				this.pre.removeClass("disbtn");
				this.next.removeClass("disbtn");
			}
			else if (this.current == 1){
				this.pre.addClass("disbtn");
				this.next.removeClass("disbtn");
			}
			else if (this.current == this.max){
				this.pre.removeClass("disbtn");
				this.next.addClass("disbtn");
			}
			this.page.setText(this.current+"/"+this.max);
		}
	},
	loadNextPic: function()
	{
		if(this.index<this.allPhotos.length)
		{
		     var img = new Element("img");
		     img.setStyle("width", "246px");
		     img.setStyle("height", "164px");
		     img.setStyle("display","none");
		     img.setProperty("src", this.allPhotos[this.index]);
		     img.injectInside($('imgs'));
		     this.index+=1;
		}
	},
	showText: function()
	{
		$$('.spotlighttext').each(function(ele){
			ele.setStyle("display","none");
		});
		$('textWrap').setStyle("display","block");

		if(this.tabIndex == "1" && $$('.college_spotlight_overview').length>0)
		{
			var overviewElement = $$('.college_spotlight_overview')[0];
			overviewElement.getChildren('h4')[0].setHTML('What ' + collegeName + ' Says...');
			if(logoPath != "")
			{
				var logo = new Element('img');
				var temp = overviewElement.getChildren('h4')[0].getText();
				temp = temp.substring(5, temp.indexOf(" Says..."));
				logo.setProperty('src',logoPath).setProperty('alt', temp).setProperty('title',temp);
				logo.inject(overviewElement.getChildren('h4')[0], 'after');
			}
			overviewElement.setStyle("display","block");
		}
		else if(this.tabIndex == "2" && $$('.college_spotlight_admission').length>0)
		{
			var admissionElement = $$('.college_spotlight_admission')[0];
			admissionElement.getChildren('h4')[0].setHTML('What ' + collegeName + ' Says...');
			if(logoPath != "")
			{
				var logo = new Element('img');
				var temp = admissionElement.getChildren('h4')[0].getText();
				temp = temp.substring(5, temp.indexOf(" Says..."));
				logo.setProperty('src',logoPath).setProperty('alt',temp).setProperty('title',temp);
				logo.inject(admissionElement.getChildren('h4')[0], 'after');
			}
			admissionElement.setStyle("display","block");
		}
		else if(this.tabIndex == "3" && $$('.college_spotlight_money_matters').length>0)
		{
			var moneyMattersElement = $$('.college_spotlight_money_matters')[0];
			moneyMattersElement.getChildren('h4')[0].setHTML('What ' + collegeName + ' Says...');
			if(logoPath != "")
			{
				var logo = new Element('img');
				var temp = moneyMattersElement.getChildren('h4')[0].getText();
				temp = temp.substring(5, temp.indexOf(" Says..."));
				logo.setProperty('src',logoPath).setProperty('alt',temp).setProperty('title',temp);
				logo.inject(moneyMattersElement.getChildren('h4')[0], 'after');
			}
			moneyMattersElement.setStyle("display","block");
		}
		else if(this.tabIndex == "4" && $$('.college_spotlight_academics').length>0)
		{
			var academicsElement = $$('.college_spotlight_academics')[0];
			academicsElement.getChildren('h4')[0].setHTML('What ' + collegeName + ' Says...');
			if(logoPath != "")
			{
				var logo = new Element('img');
				var temp = academicsElement.getChildren('h4')[0].getText();
				temp = temp.substring(5, temp.indexOf(" Says..."));
				logo.setProperty('src',logoPath).setProperty('alt',temp).setProperty('title',temp);
				logo.inject(academicsElement.getChildren('h4')[0], 'after');
			}
			academicsElement.setStyle("display","block");
		}
		else if(this.tabIndex == "5" && $$('.college_spotlight_campus_life').length>0)
		{
			var campusLifeElement = $$('.college_spotlight_campus_life')[0];
			campusLifeElement.getChildren('h4')[0].setHTML('What ' + collegeName + ' Says...');
			if(logoPath != "")
			{
				var logo = new Element('img');
				var temp = campusLifeElement.getChildren('h4')[0].getText();
				temp = temp.substring(5, temp.indexOf(" Says..."));
				logo.setProperty('src',logoPath).setProperty('alt',temp).setProperty('title',temp);
				logo.inject(campusLifeElement.getChildren('h4')[0], 'after');
			}
			campusLifeElement.setStyle("display","block");
		}
		else if(this.tabIndex == "6" && $$('.college_spotlight_students').length>0)
		{
			var studentsElement = $$('.college_spotlight_students')[0];
			studentsElement.getChildren('h4')[0].setHTML('What ' + collegeName + ' Says...');
			if(logoPath != "")
			{
				var logo = new Element('img');
				var temp = studentsElement.getChildren('h4')[0].getText();
				temp = temp.substring(5, temp.indexOf(" Says..."));
				logo.setProperty('src',logoPath).setProperty('alt',temp).setProperty('title',temp);
				logo.inject(studentsElement.getChildren('h4')[0], 'after');
			}
			studentsElement.setStyle("display","block");
		}
		else if($('showcase') == null)
		{
			$$('.spotlightwrap')[0].setStyle("display","none");
		}
	}
});

function initializeSectionEvents(secbar){
	var index = parseInt(secbar.getNext().getProperty('id').replace(/section/g, ""));
	secbar.addEvent('click',function(){
		var settings = getVisibilitySettings();
		if(this.hasClass('opened')){
			this.removeClass('opened');
			this.getNext().setStyle('display','none');
			settings[index] = 'false';
		}else{
			this.addClass('opened');
			this.getNext().setStyle('display','block');
			settings[index] = 'true';
		}
		setSectionToolTip(this);
		setVisibilitySettings(settings);
	});
}

/**
 * Set a section's tool tip.
 *
 * @param {Object} section The section to initialize.
 */
function setSectionToolTip(section) {
    if (section.hasClass("opened")) {
        section.setProperty("title", "Click to hide section contents");
    } else {
        section.setProperty("title", "Click to show section contents");
    }
}

function getVisibilitySettings(){
	var reg = new RegExp("(?:; )?" + "COLLEGE_PROFILE_VISIBILITY_SETTINGS" + "=([^;]*);?");
	if(reg.test(document.cookie)) {
		var value = RegExp["$1"];
		return value.split("-");
	} else {
		return ["true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true", "true"];
	}
}

function setVisibilitySettings(settings){
	document.cookie = "COLLEGE_PROFILE_VISIBILITY_SETTINGS" + "=" + settings.join('-') + ";path=/";
}

window.addEvent('domready',function(){
	if($$('.spotlight').length > 0)
	{
		var myspotlight = new SpotLight();
	}
	$('tabcontwrap').getElements('h3').each(function(secbar){
		initializeSectionEvents(secbar);
		setSectionToolTip(secbar);
	});
})
