// JavaScript Document

//#####################################################################     
//###############     CopyRight Granville Design      #################
//###############       http://www.granville.nl       #################
//#####################################################################
//# @GD 2009 														  #
//# core.js															  #
//#####################################################################

// jQuery
$(document).ready(function() {
	
	// FOCUS: focus on the first element on the page with class "focus"
	$('.jFocus:first').focus();
	
	
	// HISTORYBACK: go one step back in page history
	$('.jHistoryBack').click(function(event) {
		event.preventDefault();
		window.history.go(-1);
	});
	
	
	// HISTORYNEXT: go one step front in page history
	$('.jHistoryNext').click(function(event) {
		event.preventDefault();
		history.go(+1);
	});
	
	
	// AUTOTAB: jumps to next input field
	$(".jAutoTab").keyup(function() {
		var size = $(this).val().length;
		var maxSize = $(this).attr('maxlength'); 
		if (size >= maxSize) {
			$(this).next().focus();
		}
	});
	
	
	// PRINT: prints the current window on click
	$(".jPrint").click(function(event) {
		event.preventDefault();
		window.print();
	});	


     // BOOKMARK: adds bookmark functionality to anchors <a>add a "rel=sidebar" attrib if Opera 7+  
	$(".jBookmark").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link
		var url = this.href;  
		var title = this.title;
		
		if (document.all) {
			window.external.AddFavorite(url, title);
		} else if (window.sidebar) {
			window.sidebar.addPanel(title, url, "");
		} else if(window.opera) {  // Opera 7+  
			return false;  // do nothing - the rel="sidebar" should do the trick  
		} else {  // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			alert('Unfortunately, this browser does not support the requested action,'+ ' please bookmark this page manually.');
		}
	
	});


	// HOMEPAGE: sets this page as homepage for IE users  
	$(".jHomepage").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   
		var url = this.href;  
		
		if (document.all) {
			document.body.style.behavior='url(#default#homepage)';
			document.body.setHomePage(url); 
		} else {  // for all other browsers who do not support this script
			alert('Unfortunately, this browser does not support the requested action,'+ ' please set this your homepage manually.');
		}
	
	});
	

	// CONFIRM: asked to confirm before leaving to href url
	$(".jConfirm").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   
		var url = this.href; 
		var title = this.title;
		var yousure = confirm(title);
		
		if (yousure == true) {
			document.location = url;
		} else {
			// Do nothing
		}		
	
	});
	

	// POPUP: launches url in a new resizale popup window 
	$(".jPopup").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   

		var url 	= this.href;
		var title 	= this.title;
		var regex 	= /\|[0-9]+\|[0-9]+\|$/;
		var day 	= new Date();
		var id 		= day.getTime();

		if (title.match(regex)) {
			// get dimensions
			var dimensions = String(regex.exec(title));
			var dimensionsArr = dimensions.split("|");
			
			// use user defined dimensions, not resizable
			eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + dimensionsArr[1] + ",height=" + dimensionsArr[2] + ",left = 100,top = 100');");
		} else {
			// use standard dimensions, keep resizable
			eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=800,height=600,left = 100,top =100');");
		}	
	
	});	

	
	// HIGHLIGHT: adds class highlight on mouse over and removes it om mouse out
	$('.jHighlight').mouseover(function(event) {
		$(this).addClass('highlight');
	}).mouseout(function(event) {
		$(this).removeClass('highlight');
	});
	
	
	// CLOSEANDREFRESH: close child windo and refresh parent
	$('.jCloseAndRefresh').click(function(event) {
		opener.location.reload(true);
		window.close();
	});
	
	
	// IMAGEINPUTSUBMIT: handles the submission of a input text field + image button, image anchor is base url
	$('a.jImageInputSubmit').click(function(event) {
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   

		var url 	= $(this).attr('href');
		var input 	= $(this).next('input:text').val();
		var newUrl	= url + '_search_' + escape(input);
		document.location = newUrl;
		
	});
	$('input.jImageInputSubmit').keypress(function(event) {
		   
		if (event.keyCode == 13) { // RETURN pressed
			event.preventDefault();  // prevent submission
			var url 	= $(this).prev('a').attr('href');
			var input 	= $(this).val();
			var newUrl	= url + '_search_' + escape(input);
			document.location = newUrl;	
		}
		
	});

});