// History Vars
var HistorySlider = null;
var history_texts = null;
var history_timeline = null;

// Base Domain
var base_domain = "";

var current_inline_anchor = "";

window.addEvent('domready', function(){	
	
	base_domain = $('base_domain').value;
	cid = $('cid').value;
	// Current anchor, like sae.edu/news#master
	var current_url = window.location.href;
	var current_url_array = current_url.split("#");
	if (current_url_array.length > 1) { current_inline_anchor = current_url_array[1]; }
	
	// Adding external classes
	$("wrap").getElements('a[href^="http://"]').each( function (element, i) { 
		if(!element.href.contains(base_domain) && !$(element).getElement('img') && !element.href.contains(".pdf")) {
			 element.addClass('external'); 
		} else if (element.href.contains("www2."+base_domain) && !$(element).getElement('img')) {
			element.addClass('external'); 
		}
		else if (element.href.contains(base_domain+".gr") && !$(element).getElement('img')) {
			element.addClass('external'); 
		}
	});
	
	
	// Home: News item fade
	
	var hs1 = new noobSlide({
			box: $('box1'),
			//items: [1,2],
			items: $ES('span','box1'),
			size: 965,
			autoPlay: true,
			startItem: 1,
			fxOptions: {
				duration: 600,
				transition: Fx.Transitions.Back.easeInOut,
				wait: false
			},
			interval: 5600,
			
		});
	
	
	
	
	if ($("newsticker")) {
		var newsticker = new noobSlide({
			box: $('newsticker'),
			startItem: 1,
			autoPlay: true,
			mode: 'vertical',
			items: $ES('li','newsticker'),
			size: 154,
			handles: $ES('span','newsbuttons'),
			fxOptions: {
				duration: 600,
				transition: Fx.Transitions.Expo.easeOut,
				wait: false
			},
			interval: 6000,
			onWalk: function(currentItem,currentHandle){
				$$(this.handles,newsbuttons).removeClass('active');
				$$(currentHandle,newsbuttons[this.currentIndex]).addClass('active');
			}
		});
		//add navigation
        for (i=1;i<=newsticker.items.length;i++)
		{
			var currentelement = new Element('span').setHTML(i);
			currentelement.injectInside($('newsbuttons'));
		}

		//handle buttons
		var newsbuttons = $ES('span','newsbuttons');
		newsticker.addHandleButtons(newsbuttons);
		//
		newsticker.walk(0)
	}

	
	
	// Flash Objects
	initFlashObject();
	
	//local time
	worldClockZone(cid);
	
	////////////////////////////////////////////////////////////////////////////////////////
	
	// Menu
	
	//IE dropdown menu fix
		if (document.all&&document.getElementById) {

			navRoot = document.getElementById("nav").getElementsByTagName("LI");
			for (var i=0; i<navRoot.length; i++) {
					navRoot[i].onmouseover=function() {
						this.className+=" over";
					}
					navRoot[i].onmouseout=function() {
						this.className=this.className.replace(new RegExp(" over\\b"), "");
					}
			}

		}
	//end IE fix
	
	////////////////////////////////////////////////////////////////////////////////////////
	
	// Location Select
	/*
	$("location_select").getElements("li").each(function(item, i) {
		item.onmouseover = function() { this.addClass("over"); }
		item.onmouseout = function() { this.removeClass("over"); }
	});*/	
	
	////////////////////////////////////////////////////////////////////////////////////////
	
	//school select
	if ($('toggle')) {
		var mySlide = new Fx.Slide('localselect');
		$('toggle').addEvent('click', function(e){
			e = new Event(e);
			mySlide.toggle();
			e.stop();
		});
		mySlide.hide();
	}
	////////////////////////////////////////////////////////////////////////////////////////
	//faq
	if ($('acc')) {
		
		var faq_acc = new Accordion('h4.atStart', 'div.atStart', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.addClass("acc_active");
		},
		onBackground: function(toggler, element){
			toggler.removeClass("acc_active");
			}

		}, $('acc'));
		
		faq_acc.togglers.each(function(toggler){
			toggler.addEvent('mouseover',function(){
				this.setStyle('color', '#ffffff');
				this.setStyle('background', '#ad0e24');
			});
			toggler.addEvent('mouseout',function(){
				this.setStyle('color', '#ad0e24');
				this.setStyle('background', 'none');
			});
		});
		
	 }
 
	////////////////////////////////////////////////////////////////////////////////////////
	
	////////////////////////////////////////////////////////////////////////////////////////
	//large accordion
	if ($('acc_h2')) {
		var acc = new Accordion('h2.atStart', 'div.atStart', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#363636');
		},
		
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#ad0e24');
			}
		}, $('acc_h2'));
	 }
 
	////////////////////////////////////////////////////////////////////////////////////////
	// History
	if ($('history')) {
		var history_texts = $('history_text');
		var history_timeline = $('history_timeline');
			
		//$('history_slider_knob').setOpacity(0.5);
		
		// Calculate Slider Area
		var slider_area_width = 0;
		history_timeline.getElements('li').each(function(item, index) {
			slider_area_width += item.getCoordinates().width.toInt();
			slider_area_width += item.getStyle('margin-right').toInt();
			slider_area_width += item.getStyle('margin-left').toInt();
			slider_area_width += 0.75;
		});
		
		$('history_slider_area').setStyle('width', slider_area_width);
		
		var myOffset = 0;
		var mySteps = history_timeline.getElements('li').length * 2; //18;
			
		HistorySlider = new Slider($('history_slider_area'), $('history_slider_knob'), {
			steps: mySteps,	
			offset: myOffset,
			onChange: function(pos){
				
				var current_history = pos / 2;
				current_history = Math.floor(current_history);
				
				var current_class = "";
				
				if (history_timeline.getElements('li')[current_history]) {
					//current_class = history_timeline.getElements('li')[current_history].getProperty('class');
					
					if (history_timeline.getElements('li')[current_history]) {
						current_class = history_timeline.getElements('li')[current_history];
						
						if (current_class.getElement('a')) {
							current_class = current_class.getElement('a').href;				
							current_class = current_class.substring(current_class.length-4, current_class.length);
						} else {
							current_class = "";
						}
					} else {
						current_class = "";
					}	
				}
				
				// Extracts the year from the classname
				current_class = current_class.match(/\d+/);
				
				// Set Timeline Options active/inactive
				history_timeline.getElements('li').each(function(item, index) {				
					if (index == current_history) {
						item.addClass('active');
					} else {
						item.removeClass('active');
					}				
				});
							
				// Show & hide the texts
				history_texts.getElements('li').each(function(item, index) {
					if (item.hasClass(current_class)) {
						item.removeClass("hide");
					} else {
						item.addClass("hide");
					}
				});			
			},
			
			onTick: function(pos) {
				$('history_slider_knob').setStyle('left', pos);
			}
		 
		}).set(0);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////
	
	// Layer Switching
	$$('a.switchContentLayer').each(function(item, index) {
		$(item).addEvent('click', function(e) {
			
			
			var href_array = item.href.split('#');
			
			if (href_array[1]) {
				var layer = href_array[1];
			} else { var layer = ""; }
			
			$$('a.switchContentLayer').each(function(anchor) {
				$(anchor).removeClass("active");
				
			});
			
			
			
			$(item).addClass("active");
						
			switchContentLayer(layer, item);
			//e.preventDefault();
			new Event(event || window.event).preventDefault();
		});
	});
	
	// Init correct anchor layer
	if (current_inline_anchor != "") {
		
		if ($('content').getElements('div.switchableContent')) {
		
			var switch_layers = $('content').getElements('div.switchableContent');
			
			switch_layers.each(function(item, index) {
				
				if ($(item).getElement('a[name=' + current_inline_anchor + ']')) {
					
					// Find Anchor Link button
					var buttons = $(item).getElements('a.switchContentLayer');
					
					var correct_button = null;
					
					buttons.each(function(button, count) {
						var href_array = button.href.split('#');
			
						if (href_array[1] && href_array[1] == current_inline_anchor) {
							$(button).addClass("active");
							
							correct_button = $(button);
						} else {
							$(button).removeClass("active");
							
						}
							
					});
					
					switchContentLayer(current_inline_anchor, $(correct_button));
					
				}
			});
			
		}
	}
}); 

// For History
function slideHistory(elem) {
	$('history_timeline').getElements('li').each(function(item, index) {
		if (item == elem.parentNode) {
			HistorySlider.set(index*2);
		}
	});
		
	return false;
}

// For Content Layering
function switchContentLayer(layername, item_obj) {
	var content = findParentNode(item_obj, "switchableContent")
	
	var count_switchable_layers = 0;
	var active_div = null;
	var active_div_nr = 0;
	var switchable_layers = new Array();
	
	if (content !== false) {
		$(content).getElements('div').each(function(item, index) {
			if (item.hasClass('switchableLayer')) {
				if (item.getElement('a[name=' + layername + ']')) {
					item.addClass("display");
					active_div = item;
					active_div_nr = index;
				} else {
					item.removeClass("display");
				}
				
				count_switchable_layers++;
				switchable_layers.push(item);
			}
		});
	}
	
	// Check for Pageination
	if (item_obj.parentNode.hasClass('page_navigation')) {
		
		item_obj.parentNode.getElements('a').each(function(item, index) {
			
			// Back Button
			if (item.hasClass("previous")) {
				if (switchable_layers.length > 0 && active_div_nr > 0 ) {
					if(switchable_layers[active_div_nr-1].getElement('a').name && switchable_layers[active_div_nr-1].getElement('a').name != "") {
						item.href = "#" + switchable_layers[active_div_nr-1].getElement('a').name;
					}
					
					item.setStyle('display', 'block');
				} else {
					item.setStyle('display', 'none');
				}
				
				item.removeClass('hide');
			}
			
			// Forward Button
			if (item.hasClass("next")) {
				if (switchable_layers.length > 0 && active_div_nr+1 < count_switchable_layers ) {
					if(switchable_layers[active_div_nr+1].getElement('a').name && switchable_layers[active_div_nr+1].getElement('a').name != "") {
						item.href = "#" + switchable_layers[active_div_nr+1].getElement('a').name;
					}
					
					item.setStyle('display', 'block');
				} else {
					item.setStyle('display', 'none');
				}
			}
			
		});
	}
	
	
}

function findParentNode(item, searchclass) {
	
	var parent_item = item.getParent();
	
	if(parent_item) {
		if (parent_item.hasClass(searchclass)) {
			return item.getParent();
		} else {
			return findParentNode(parent_item, searchclass);
		}		
		
	} else {
		return false;
	}
}

function setFrameSize(obj) {

	/*
	docHeight = iframe.document.height;
	
	if(docHeight > $(obj.style.height)) $(obj).style.height = docHeight + "px";
	*/
}

function initFlashObject() {
	document.getElements('div[class=flash_media_embed]').each(function(item, index) {
		var src = null;
		var media_player_src = null;
		var express_install_src = "/admin/flash/expressInstall.swf";
		var width = 100;
		var height = 100;
		
		var id = item.getProperty('id');
		
		// Media Src
		if ($(item).getElement('input[name=src]')) {
			src = $(item).getElement('input[name=src]').value;
		}
		
		// Flash Src
		if ($(item).getElement('input[name=flash_src]')) {
			media_player_src = $(item).getElement('input[name=flash_src]').value;
		}
		
		// Width
		if ($(item).getElement('input[name=width]')) {
			width = $(item).getElement('input[name=width]').value;
		}
		
		// Height
		if ($(item).getElement('input[name=height]')) {
			height = $(item).getElement('input[name=height]').value;
		}
		
		var flashvars = { file: src };
		
		swfobject.embedSWF(media_player_src, id, width, height, "9.0.0", express_install_src, flashvars);		
	});
}

function worldClock(zone, region) {
    var dst = 0
    var time = new Date()
    var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)
    var gmtTime = new Date(gmtMS)
    var day = gmtTime.getDate()
    var month = gmtTime.getMonth()
    var year = gmtTime.getYear()
    if (year < 1000) {
        year += 1900
    }
    var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August",
    "September", "October", "November", "December")
    var monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
    if (year % 4 == 0) {
        monthDays = new Array("31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
    }
    if (year % 100 == 0 && year % 400 != 0) {
        monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
    }

    var zoneM = zone % 1;

    if (zoneM == 0) {
            var hr = gmtTime.getHours() + zone
		    var min = gmtTime.getMinutes()
    } else {
	//add 30 mins for offset timezones
		if (gmtTime.getMinutes()<30) {
            var hr = gmtTime.getHours() + Math.floor(zone)
		    var min = gmtTime.getMinutes() + 30
		} else {
			//add 1hr
            var hr = gmtTime.getHours() + Math.floor(zone)+1
		    var min = gmtTime.getMinutes() - 30
		}
    }
   
    var sec = gmtTime.getSeconds()

    if (hr >= 24) {
        hr = hr - 24
        day -= -1
    }
    if (hr < 0) {
        hr -= -24
        day -= 1
    }
    if (hr < 10) {
        hr = " " + hr
    }
    if (min < 10) {
        min = "0" + min
    }
    if (sec < 10) {
        sec = "0" + sec
    }
    if (day <= 0) {
        if (month == 0) {
            month = 11
            year -= 1
        }
        else {
            month = month - 1
        }
        day = monthDays[month]
    }
    if (day > monthDays[month]) {
        day = 1
        if (month == 11) {
            month = 0
            year -= -1
        }
        else {
            month -= -1
        }
    }
    if (region == "NAmerica") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(3)
        startDST.setHours(2)
        startDST.setDate(1)
        var dayDST = startDST.getDay()
        if (dayDST != 0) {
            startDST.setDate(8 - dayDST)
        }
        else {
            startDST.setDate(1)
        }
        endDST.setMonth(9)
        endDST.setHours(1)
        endDST.setDate(31)
        dayDST = endDST.getDay()
        endDST.setDate(31 - dayDST)
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST && currentTime < endDST) {
            dst = 1
        }
    }
    if (region == "Europe") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(2)
        startDST.setHours(1)
        startDST.setDate(31)
        var dayDST = startDST.getDay()
        startDST.setDate(31 - dayDST)
        endDST.setMonth(9)
        endDST.setHours(0)
        endDST.setDate(31)
        dayDST = endDST.getDay()
        endDST.setDate(31 - dayDST)
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST && currentTime < endDST) {
            dst = 1
        }
    }

    if (region == "SAmerica") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(9)
        startDST.setHours(0)
        startDST.setDate(1)
        var dayDST = startDST.getDay()
        if (dayDST != 0) {
            startDST.setDate(22 - dayDST)
        }
        else {
            startDST.setDate(15)
        }
        endDST.setMonth(1)
        endDST.setHours(11)
        endDST.setDate(1)
        dayDST = endDST.getDay()
        if (dayDST != 0) {
            endDST.setDate(21 - dayDST)
        }
        else {
            endDST.setDate(14)
        }
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST || currentTime < endDST) {
            dst = 1
        }
    }
    if (region == "Cairo") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(3)
        startDST.setHours(0)
        startDST.setDate(30)
        var dayDST = startDST.getDay()
        if (dayDST < 5) {
            startDST.setDate(28 - dayDST)
        }
        else {
            startDST.setDate(35 - dayDST)
        }
        endDST.setMonth(8)
        endDST.setHours(11)
        endDST.setDate(30)
        dayDST = endDST.getDay()
        if (dayDST < 4) {
            endDST.setDate(27 - dayDST)
        }
        else {
            endDST.setDate(34 - dayDST)
        }
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST && currentTime < endDST) {
            dst = 1
        }
    }
    if (region == "Israel") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(3)
        startDST.setHours(2)
        startDST.setDate(1)
        endDST.setMonth(8)
        endDST.setHours(2)
        endDST.setDate(25)
        dayDST = endDST.getDay()
        if (dayDST != 0) {
            endDST.setDate(32 - dayDST)
        }
        else {
            endDST.setDate(1)
            endDST.setMonth(9)
        }
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST && currentTime < endDST) {
            dst = 1
        }
    }
    if (region == "Beirut") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(2)
        startDST.setHours(0)
        startDST.setDate(31)
        var dayDST = startDST.getDay()
        startDST.setDate(31 - dayDST)
        endDST.setMonth(9)
        endDST.setHours(11)
        endDST.setDate(31)
        dayDST = endDST.getDay()
        endDST.setDate(30 - dayDST)
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST && currentTime < endDST) {
            dst = 1
        }
    }
    if (region == "Baghdad") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(3)
        startDST.setHours(3)
        startDST.setDate(1)
        endDST.setMonth(9)
        endDST.setHours(3)
        endDST.setDate(1)
        dayDST = endDST.getDay()
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST && currentTime < endDST) {
            dst = 1
        }
    }
    if (region == "Australia") {
        var startDST = new Date()
        var endDST = new Date()
        startDST.setMonth(9)
        startDST.setHours(2)
        startDST.setDate(31)
        var dayDST = startDST.getDay()
        startDST.setDate(31 - dayDST)
        endDST.setMonth(2)
        endDST.setHours(2)
        endDST.setDate(31)
        dayDST = endDST.getDay()
        endDST.setDate(31 - dayDST)
        var currentTime = new Date()
        currentTime.setMonth(month)
        currentTime.setYear(year)
        currentTime.setDate(day)
        currentTime.setHours(hr)
        if (currentTime >= startDST || currentTime < endDST) {
            dst = 1
        }
    }


    if (dst == 1) {
        hr -= -1
        if (hr >= 24) {
            hr = hr - 24
            day -= -1
        }
        if (hr < 10) {
            hr = " " + hr
        }
        if (day > monthDays[month]) {
            day = 1
            if (month == 11) {
                month = 0
                year -= -1
            }
            else {
                month -= -1
            }
        }
        return hr + ":" + min + ":" + sec + " DST"
    }
    else {
        return hr + ":" + min + ":" + sec
    }
}

function worldClockZone(cid) {
	//set each campus's timezone offset - cid is current campus
	
	var timeoffset=[];
	//UK
	timeoffset[8] = [0, "Europe"];
	timeoffset[88] = [0, "Europe"];
	timeoffset[9] = [0, "Europe"];
	timeoffset[10] = [0, "Europe"];
	//Africa
	timeoffset[89] = [2, "Africa"];
	//Germany
	timeoffset[46] = [1, "Europe"];
	timeoffset[43] = [1, "Europe"];
	timeoffset[45] = [1, "Europe"];
	timeoffset[42] = [1, "Europe"];
	timeoffset[44] = [1, "Europe"];
	timeoffset[63] = [1, "Europe"];
	timeoffset[41] = [1, "Europe"];
	//NL
	timeoffset[33] = [1, "Europe"];
	timeoffset[13] = [1, "Europe"];
	//Austria
	timeoffset[37] = [1, "Europe"];
	//Switzerland
	timeoffset[30] = [1, "Europe"];
	timeoffset[38] = [1, "Europe"];
	//Turkey
	timeoffset[151] = [2, "Europe"];
	//Ljubljana
	timeoffset[87] = [1, "Europe"];
	//Belgium
	timeoffset[11] = [1, "Europe"];
	//France
	timeoffset[26] = [1, "Europe"];
	//Greece
	timeoffset[14] = [2, "Europe"];
	//Italy
	timeoffset[22] = [1, "Europe"];
	//Sweden
	timeoffset[35] = [1, "Europe"];
	//Spain
	timeoffset[62] = [1, "Europe"];
	timeoffset[19] = [1, "Europe"];
	//Australia
	timeoffset[73] = [10, "Brisbane"];
	timeoffset[12] = [10.5, "Australia"];
	timeoffset[77] = [8, "Australia"];
	timeoffset[78] = [10, "Australia"];
	//NZ
	timeoffset[15] = [12, "Australia"];
	//Japan
	timeoffset[102] = [9, "Japan"];
	//Dubai
	timeoffset[68] = [4, "Dubai"];
	//US
	timeoffset[25] = [-5, "NAmerica"];
	timeoffset[131] = [-8, "NAmerica"];
	timeoffset[84] = [-5, "NAmerica"];
	timeoffset[65] = [-8, "NAmerica"];
	timeoffset[21] = [-5, "NAmerica"];
	timeoffset[24] = [-6, "NAmerica"];
	
if (timeoffset[cid]) {
    document.getElementById("localtime").innerHTML = worldClock(timeoffset[cid][0], timeoffset[cid][1]);

    setTimeout("worldClockZone(cid)", 1000);
}
}

