﻿var Cookie = {
	path: '',
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + this.path);
  },
  setPath: function(value) {
		this.path = '; path=' + escape(value);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

var activeProgram = null;

function initPrograms(suffix) {
	if (suffix == undefined)
		suffix = '';
	
	var programs = $$('#programs .item .info');
	var cookieName = 'activeProgram' + suffix;
	
	if (programs.length == 0)
		Cookie.erase(cookieName);
	else if (programs.length == 1)
		Cookie.set(cookieName, programs[0].id);
	
	programs.each(function(element) {
		element.hide();
		Event.observe(element.up('.item').down('h3 a'), 'click', function() {
			if (Effect.Queues.get('programAccordion').effects.length > 0)
				return;
			
			if (element.visible()) {
				Cookie.erase(cookieName);
				activeProgram = null;
			} else {
				if (activeProgram != null)
					Effect.BlindUp(activeProgram, {queue: {scope: 'programAccordion'}});
				Cookie.set(cookieName, element.id)
				activeProgram = element;
			}
			
			Effect.toggle(element, 'blind', {queue: {scope: 'programAccordion'}});
			
			element.blur();
		});
	});
	
	activeProgram = $(Cookie.get(cookieName));
	
	if (activeProgram != null) {
			activeProgram.show();
			
			if (Cookie.get(cookieName + 'Scroll') != null) {
				Cookie.erase(cookieName + 'Scroll');
				var offset = Prototype.Browser.IE ? -32 : 0;
				Effect.ScrollTo(activeProgram.up(), {offset: offset});
			}
	}
}

function setProgram(id, suffix) {
	if (suffix == undefined)
		suffix = '';
	
	Cookie.set('activeProgram' + suffix, 'program' + id);
	Cookie.set('activeProgram' + suffix + 'Scroll', true);
}

var activeListing = null;

function initListings() {
	var listings = $$('#listings .item .info');
	
	if (listings.length == 0)
		Cookie.erase('activeListing');
	else if (listings.length == 1)
		Cookie.set('activeListing', listings[0].id);
	
	listings.each(function(element) {
		element.hide();
		
		Event.observe(element.up('.item').down('h3 a'), 'click', function() {
			if (Effect.Queues.get('listingAccordion').effects.length > 0)
				return;
			
			if (element.visible()) {
				Cookie.erase('activeListing');
				activeListing = null;
			} else {
				if (activeListing != null)
					Effect.BlindUp(activeListing, {queue: {scope: 'listingAccordion'}});
				Cookie.set('activeListing', element.id)
				activeListing = element;
			}
			
			Effect.toggle(element, 'blind', {queue: {scope: 'listingAccordion'}});
			
			element.blur();
		});
	});
	
	activeListing = $(Cookie.get('activeListing'));
	
	if (activeListing != null)
		activeListing.show();
}


// Crisis Number Flipping - Prototype
var numbers = null;
var current = null;

function flipNumber()
{
	if (current == null)
		current = numbers[0];;
	
	new Effect.Appear(current, { duration: 0.5, afterFinishInternal: function() {
		new Effect.Fade(current, { duration: 0.5, delay: 10, afterFinishInternal: function() {
			current.hide();
			current = current.next();
			flipNumber();
		}});
	}});
}

function initNumberFlipping()
{
	numbers = $$('#crisisnumber div');
	numbers.each(function(number) {
		number.hide();
		number.style.width = 'auto';
	});
	flipNumber();
}


// Upload Progress Bar - Prototype, AJAX
var updater;

function StartUpload(formObj)
{
	formObj = $(formObj);
	
	var hasNonEmptyFile = false;
	for (var i=0; i<formObj.elements.length; i++) {
		if (formObj.elements[i].type == 'file' && formObj.elements[i].value.length > 0)
			hasNonEmptyFile = true;
	}
	if (!hasNonEmptyFile)
		return;
	
	var uniqueID = Math.floor(Math.random() * 10000000) + (new Date()).getTime() % 1000000000;
	thePos = formObj.action.indexOf('uploadid');
	if (thePos >= 1)
		formObj.action = formObj.action.substring(0, thePos-1);
	if (formObj.action.indexOf('?')==-1)
		formObj.action += '?uploadid=' + uniqueID
	else
		formObj.action += '&uploadid=' + uniqueID;
	
	var modal = new Control.Modal(false, {
		fade: true,
		opacity: 0.7,
		overlayCloseOnClick: false,
		contents: '<div id="progress"></div>',
	  width: 400
	});
	modal.open();
	
	if (updater)
		updater.stop();
	updater = new Ajax.PeriodicalUpdater('progress', '../upload/progress.aspx?ajax=true&uploadid=' + uniqueID, { frequency: 1, evalScripts: true });
}

function CancelUpload()
{
	if (window.stop)
		window.stop();
	else if (window.document && window.document.execCommand)
		window.document.execCommand('Stop');
	
	if (updater)
		updater.stop();
		
	Control.Modal.close();
}

function ClearUpload(control)
{
	var control = $(control);
	var attr = control.attributes;
	
	var element = document.createElement('input');
	for (var i=0; i<attr.length; i++) {
		element.setAttribute(attr[i].name, attr[i].value);
	}
	
	control.replace(element);
}