// JavaScript Document

//#####################################################################     
//###############     CopyRight Granville Design      #################
//###############       http://www.granville.nl       #################
//#####################################################################
//# @GD 2009 														  #
//# core.js															  #
//#####################################################################
	
// jQuery extension: jData
/*(function($){
    $.fn.extend({
        jData: function() {
			var arrayOfClasses = $(this).attr('class').split(' ');
			for (var key in arrayOfClasses) {
				if (arrayOfClasses[key].indexOf('jData:') >= 0) {
					// Get data
					var data = arrayOfClasses[key].replace('jData:','');
					return data;
				}
			}            
        }
    });
})(jQuery);*/


// jQuery class selectors
$(document).ready(function() {
	
	// FOCUS: focus on the first element on the page with class "jFocus"
	$('.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();
	});	
	
	
	// CLICKCLEAR: clears input of value
	$(".jClickClear").click(function(event) {
		$(this).val('');
	});	

    // 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
		}		
	
	});
	

	// ALERT: shows alert message on click
	$(".jAlert").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link    
		var title = this.title;
		alert(title);
	});
	

	// SUBMIT: submits the form defined in rel
	$(".jSubmit").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link    
		var form = this.rel;
		$(form).submit();
	});
	
	
	// SUBMITONUPDATE: submits the form defined in rel when the field is updated
	$('.jSubmitOnUpdate[class*="jData:"]').change( function(event) {
		var arrayOfClasses = $(this).attr('class').split(' ');
		for (var key in arrayOfClasses) {
			if (arrayOfClasses[key].indexOf('jData:') >= 0) {
				// Get data
				var data = arrayOfClasses[key].replace('jData:','');
				break;
			}

		}
		//alert(data);
		$(data).submit();
	});

/*	$('.jSubmitOnUpdate[class*="jData:"]').change( function(event) {
		var data = $(this).jData();
		$(data).submit();
	});*/


	// 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;	
		}
		
	});
	
	
	// OPTIONSELECT: handles selection and input in optional_select_fields
	$('.jOptionSelect').change( function() {
		var value = $(this).val();
		if (value != '[[OTHER]]' && value != '') {
			// check input ($(this).val()) for validity here
			$(this).next().val('');
			$(this).focus();
		}
	});
	// OPTIONINPUT: jumps to next input field
	$('.jOptionInput').keyup(function() {
									  
		var size = $(this).val().length;
		if (size >= 1) {
			$(this).prev().val('[[OTHER]]');
			$(this).focus();
		}
	});


	// TOOLTIP: shows a tooltip on hover, style #tooltip for tooltip layout 
	/* CONFIG */		
		xOffset = 50;  
		yOffset = 10;  		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$('.jTooltip').hover(function(e) {											  
		this.t = this.title;
		this.title = "";									  
		$('body').append('<p id="tooltip">'+ this.t +'</p>');
		$('#tooltip')
			.css('top',(e.pageY - xOffset) + 'px')
			.css('left',(e.pageX + yOffset) + 'px')
			.fadeIn('fast');		
    },
	function() {
		this.title = this.t;		
		$('#tooltip').remove();
    });	
	$('.jTooltip').mousemove(function(e){
		$('#tooltip')
			.css('top',(e.pageY - xOffset) + 'px')
			.css('left',(e.pageX + yOffset) + 'px');
	});
	

	// LINKIMAGESWAP: swaps the href of the link with the src of the image id in rel
	$(".jLinkImageSwap").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   
		var url = this.href; 
		var id = this.rel;
		
		// Swap target src
		$("#"+id).attr('src', url);		
	
	});	
	
	// SCROLL: scrolls to the first element on the page with class "jScroll" (needs scrollto plugin)
	//$.scrollTo('.jScroll:first', {axis:'y', duration:500});
	//$.scrollTo('.jScroll:first');	


});

