// Setup JS events when the DOM is ready
$(document).ready(function(){
	$(".search_box").focus(clearbox);
	$(".search_box").blur(clearbox);
   $("#print_page").click(printpage);
	/*$('.styleswitch').click(function() {
		switchStylestyle(this.getAttribute("rel"));
		return false;
		});
	var c = readCookie('style');
	if (c) switchStylestyle(c);*/
	
	// Popup links
	$("a.popup").click("this.target='_blank'");
	
	// Fix background image caching problem
    if (jQuery.browser.msie) {
        try { document.execCommand("BackgroundImageCache", false, true);
        } catch(err) {}
    } 
    
	// Jump Menu
	$("#sites_field").change(function() {
	   var url = this.options[this.selectedIndex].value;
         if (url != "")
         {
         window.open(url);
         }
	   }
	)
	
	
	// Homepage banner animation
	if ($("#picture").length > 0) $.slideshow('picture', 4000);
});



// jQuery fader
// Based on: http://portfolio.gizone.co.uk/applications/slideshow/
$.slideshow = function (containerId, timeout) {
	var current = 0;
	var id = '#' + containerId;
	$(id).css({position:'relative'});
	var slides = $(id).children().get();
	for ( var i = 0; i < slides.length; i++ ) {
		$(slides[i]).css({zIndex:(slides.length - i), position:'absolute', top:'0', left:'0'});
	}
	setTimeout((function(){$.slideshow.next(slides, timeout, current);}), timeout);
}
$.slideshow.next = function (slides, timeout, current) {
	for (var i = 0; i < slides.length; i++) {
		var slide = slides[(current + i) % slides.length];
		$(slide).css({zIndex:(slides.length - i)});
	}
	// IE doesn't seem to support .show() after it has been faded out, so we use .fadeIn("fast")
	$(slides[current]).fadeOut('slow', 
			function(){$(slide).css({zIndex:'0', opacity:1}).fadeIn("fast");}
			);
	
	current = (current + 1) % slides.length;
	setTimeout((function(){$.slideshow.next(slides, timeout, current);}), timeout);
}


// Reset default text in input box
function clearbox()
{
    if (!this.default_value) this.default_value = this.value;
    
    if (this.value == '') {
        this.value = this.default_value;
    } else if (this.value == this.default_value) {
        this.value = '';
    }
}

// Jump to url
function jump_to_url(url)
{
	if (url == "") return false;
	location.href = "/" + url;
}

// Show admin popup window
function admin_popup(url)
{
	var win = window.open(url,'_adminedit','height=650,width=675,resizable=yes,dependent,scrollbars=yes');
	win.focus();
}

// JQuery Style Switcher
function switchStylestyle(styleName)
{
	$('link[@rel*=style]').each(function(i) {
	    this.disabled = true;
	    if (this.getAttribute('title') == styleName) this.disabled = false;
		});
	createCookie('style', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// Print Function
	
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1); 

function printpage() {
  if (pr) // NS4, IE5, Safari
    window.print()
  else // other browsers
    alert("Sorry, your browser doesn't support this feature.\n Please use the Print option from the menu of your browser.");
  return false;
}	