/**
 * A Little Piece of Mind - global.js
 *
 * @author      Marque <digital@marquecreative.com>
 * @copyright   2008 Marque
 */
 
var bgNum = 0;	// Global to keep track of what background image we're on 

function init() {
	var divs = getAbsoluteDivs();

	for (i = 0; i < divs.length; i++) {
		var item = divs[i];
		if(item.className != "bg") item.onclick = function() { bringToFront(this.id); };
	}
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {         
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
    // Bring everything to the front except the background
    bringToFront('supporting');
    bringToFront('logos');
    bringToFront('digitallink');
    bringToFront('templateslink')
    bringToFront('mailinglist');
    bringToFront('marquelink');
    bringToFront('intro3');
    bringToFront('intro2');
    bringToFront('intro1');
    bringToFront('titlebox');
    if (document.getElementById('error') != null) bringToFront('error');
    if (document.getElementById('success') != null) bringToFront('success');
    
    // Start the rotation
    setInterval(changeBackground, 10000);
}

function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

function getAbsoluteDivs()  {  
    var arr = new Array();  
    var all_divs = document.body.getElementsByTagName("DIV");  
    var j = 0;  
  
    for (i = 0; i < all_divs.length; i++)  {
		if (all_divs.item(i).currentStyle) {
			if (all_divs.item(i).currentStyle.position == 'absolute') {			
				arr[j] = all_divs.item(i);  
				j++;
			}
		} else if (document.defaultView.getComputedStyle) {
			if (document.defaultView.getComputedStyle(all_divs.item(i),'').position == 'absolute') {			
				arr[j] = all_divs.item(i);  
				j++;  
            }
		}
	}
    return arr;  
}  

function bringToFront(id) {
	if (!document.getElementById || !document.getElementsByTagName) return; 
	
	var obj = document.getElementById(id);  
	var divs = getAbsoluteDivs();  
	var max_index = 0;  
	var cur_index;  
	
	for (i = 0; i < divs.length; i++) {  
		var item = divs[i];  
		if (item == obj || item.style.zIndex == '') continue;  
	
		cur_index = parseInt(item.style.zIndex);  
		if (max_index < cur_index) max_index = cur_index;
	}  
	obj.style.zIndex = max_index + 1;  
}

function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value==""||(field.defaultText && value==field.defaultText)) return alerttxt;
		else return '';
	}
}

function validate_email(field, alerttxt) {
	with (field) {
		if (validate_required(field, alerttxt) == '') {
			apos = value.indexOf("@");
			dotpos = value.lastIndexOf(".");
			if (apos < 1 || dotpos - apos < 2 || dotpos >= value.length - 2) {
				return alerttxt;
			} else return '';
		} else return alerttxt;
	}
}

function validate(thisform) {
    var why = "";
	if (thisform.id == 'mailinglist_form') {
    	why += validate_required(thisform.email, "Please enter a valid email address.\n");
    } else {
		why += validate_required(thisform.name, "Please tell us your name.\n");
		why += validate_email(thisform.email_digital, "Please enter a valid email address.\n");
		why += validate_required(thisform.file_submission, "You should probably include a file with your submission.\n");
	}    
	if (why != "") {
       alert(why);
       if (thisform.id == 'mailinglist_form') thisform.email.focus();
       return false;
    }
	return true;
}

function slideSwitch() {
    var $active = $('#submissions-slideshow img.active');

    if ( $active.length == 0 ) $active = $('#submissions-slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#submissions-slideshow img:first');

    $active.addClass('last-active');

    $next.addClass('active')
    $active.removeClass('active last-active');
}

function $() {
    setInterval( "slideSwitch()", 2000 );
}

$();

function changeBackground() {
	$('#bg-'+bgNum).fadeOut(2000);
	bgNum++;
	if(bgNum == 3) bgNum = 0;
	$('#bg-'+bgNum).fadeIn(2000);
}

