defined = function( x ) {
    try {
        if( typeof x != "undefined" )
            return true;
    }
    catch( e ) {}
    return false;
}
if( !defined( window.XMLHttpRequest ) ) {
    window.XMLHttpRequest = function() {
        var types = [
            "Microsoft.XMLHTTP",
            "MSXML2.XMLHTTP.5.0",
            "MSXML2.XMLHTTP.4.0",
            "MSXML2.XMLHTTP.3.0",
            "MSXML2.XMLHTTP"
        ];

        for( var i = 0; i < types.length; i++ ) {
            try {
                return new ActiveXObject( types[ i ] );
            } catch( e ) {}
        }

        return undefined;
    }
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function searchBar(e, t) {
	if(e == 'focus') {
		t.style.width = '250px';
		if(t.value == 'search this site...') {
			t.value = ''; 
			t.setAttribute('class', 'search-on');
		}
	} else {
		t.style.width = '178px';
		t.setAttribute('class', 'search-off');
		if(t.value == '')
		 	t.value = 'search this site...';
	}
}

function toggleSubscribe(a) {
	var pos = findPos(a);
	var arrayPageSize = getPageSize();

	var subscribe = document.getElementById('nav-subscribe');
	
	if(!subscribe)
		return;
		
	subscribe.style.left = pos[0] - 250 + (arrayPageSize[0] * 0.03) + 'px';
	subscribe.style.top = pos[1] + 25 + 'px';
	
	if(subscribe.style.display == 'block' || subscribe.style.visibility == 'visible') {
		subscribe.style.visibility = 'hidden';	
		subscribe.style.display = 'none';
	} else {
		subscribe.style.visibility = 'visible';	
		subscribe.style.display = 'block';
	}	
}

function onClickHandler(evt) {
    // try to find our "window" element in dom hierarchy
    evt = evt || event;
    var element = evt.target || evt.srcElement;
    el_link = document.getElementById('navbar');
    el = document.getElementById('nav-subscribe');
    if (!el) return true;
    while( element ) {
        if( (element == el) || (element == el_link) )
            return true;
        element = element.parentNode;
    }

    el.style.display = 'none';
    el.style.visibility = 'hidden';
    return true;
}
if( document.addEventListener )
    document.addEventListener( "click", onClickHandler, true );
else if( document.attachEvent )
    document.attachEvent( "onclick", onClickHandler );

function livePreview() {
	var name = commenter_name || document.getElementById('comment-author').value || 'Anon';
	var url = document.getElementById('comment-url').value;
	var text = document.getElementById('comment-text').value;
	text = '<p>'+ text.replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';	
	document.getElementById('comment-livepreview-byline').innerHTML = '<a href="' + url + '">' + name + '</a> wrote:';
	document.getElementById('comment-livepreview-body').innerHTML = text;
}

var screenshots = [];
var currentScreenshot = 0;

function initScreenshots() {
	var widgetScreenshot = document.getElementById('widget-screenshots');
	
	if(!widgetScreenshot)
		return;
	
	var divs = widgetScreenshot.getElementsByTagName('div');
	
	for(var i = 0; i < divs.length; i++) {
		if(divs[i].id.match('screen_')) {
			screenshots[screenshots.length] = divs[i];
			if (screenshots.length > 1)
				divs[i].style.display = 'none';
		}
	}
}

function flipScreenshot(dir) {
	screenshots[currentScreenshot].style.display = 'none';
	
	if(dir == 'next') {
		currentScreenshot++;
		if(currentScreenshot >= screenshots.length )
			currentScreenshot = 0;
	} else {
		currentScreenshot--;
		if(currentScreenshot < 0)
			currentScreenshot = screenshots.length - 1;		
	}
	
	screenshots[currentScreenshot].style.display = 'block';
}

var plugin;

function startWizard(id) {
	Element.show('overlay');
	
	// If there's a screencast, hide it to prevent flashing
	var screencast = document.getElementById('plugin-screencast');
	if(screencast)
		screencast.style.visibility = 'hidden';

	// stretch overlay to fill page and fade in
	var arrayPageSize = getPageSize();
	Element.setHeight('overlay', arrayPageSize[1]);
	overlayEffect.custom(0,0.5);
	
	// Hide all the other wizards
	Element.hide('signup');
	Element.hide('download');
	Element.hide('purchase');
	
	// Element.setTop('signup-download', dWizardTop);
	Element.show('signup-download');	
	Element.show(id);
}

function showLicense(u, w) {
	if(!u)
		return;
		
	Element.hide(w + '-license-personal');
	Element.hide(w + '-license-commercial');
	Element.hide(w + '-license-enterprise');
	
	Element.show(w + '-license-' + u);
	Element.show(w + '-license');
}

function toggleSubmit(c) {
	var submitButton = document.getElementById('submit');
	
	if(!c)
		c = document.getElementById('license_agree');
		
	if(c.checked == true)
		submitButton.style.display = 'block';
	else
		submitButton.style.display = 'none';
}

function submitWizard(id) {
	var f = document.forms[id];
	var action = new Array();
	var breakEmpty;
	for (var i = 0; i < f.elements.length; i++) {
		var el = f.elements[i];
		if(!el.name)
			continue;
			
		if(!el.value) {
			el.style.borderColor = '#c00';
			breakEmpty = 1;
		}			
			
		action[action.length] = el.name + '=' + el.value;
	}
	
	if(breakEmpty) {
		alert('All fields are required to download the plugin, please ensure a value has been entered');
		return
	}
	
	var status = document.getElementById(id + '-status').getElementsByTagName('p')[0];
	status.innerHTML = '';
	
	var indicator = document.getElementById(id + '-indicator');
	indicator.style.display = 'block';
	
	var xmlhttp = new XMLHttpRequest();
	if(xmlhttp) {
		var requestFile = f.action + '?' + action.join('&');
		xmlhttp.open("POST", requestFile ,true);
	    xmlhttp.onreadystatechange = function () {
		    if (xmlhttp.readyState == 4) {
				var res = xmlhttp.responseText;
				if(!res.match('Success')) {
					status.innerHTML = 'An error occured: ' + res;
					status.style.color = '#c00';
				} else {
					status.innerHTML = res;
				}
				indicator.style.display = 'none';
		    }
		}
 		xmlhttp.send(action.join('&'));	
 	}
	return false;
}

function closeWizard() {
	Element.hide('signup-download');
	Element.hide('overlay');
	
	// If there's a screencast, it was hidden, show it
	var screencast = document.getElementById('plugin-screencast');
	if(screencast)
		screencast.style.visibility = 'visible';
}

function autoWizard() {
	if(document.location.hash.match('start:download'))
		startWizard('download');
		
	if(document.location.hash.match('start:purchase'))
		startWizard('purchase');
		
}

function startDownload(fid) {
	var f = document.forms[fid];
	
	window.location = f.action + f.elements['file_basename'].value + '_' + f.elements['version'].value + '.' + f.elements['format'].value;
	
	return false;
}