﻿/* ++++++++++++++++++++++++++++++++
	++	ret.js							++
	++	extracted from					++
	++	jquery.pallette-0.1.7.js	++
	++										++
	++	Dependencies:					++
	++	- jQuery 1.2.6 or higher	++
	++++++++++++++++++++++++++++++++ */

	
(function( $ ) {
	$.fn.extend( {
	/* ================================
		==  Extending jQuery methods  ==
		================================ */
	
		ret: "0.1",
		
		contentObj: window.contentObj || {},
	
		hashRef: function() { // v0.1.5
			//	Finds corresponding element(s), based on href= attribute (hash) of first matched element. Works both ways.
			return this.is("[href*='#']") ? ( $(this[0].href.substring( this[0].href.lastIndexOf("#") )) ) : $("[href$='#" + this[0].id + "']");
		},
	
		forRef: function() { // v0.1.5
			//	Finds corresponding label(s), input, select or textarea, based on for= attribute of first matched element. Works both ways.
			return this.is("label[for]") ? $("#" + this[0].htmlFor) : $("label[for='" + this[0].id + "']");
		},
		
		overLabel: function() { // v0.1.6
			//	Makes 'overLabel' behavior possible. Works both on labels and form fields.
			//	Concept based on http://www.alistapart.com/articles/makingcompactformsmoreaccessible/
			//	CSS:		.jsOverLabel (on label)
			//				.jsOverLabelBlur (on parent of form field: blur)
			//	Deps.:	forRef() method (v0.1+)
			return this
				.each( function() {
					var jThis = $(this);
					var jRef = jThis.forRef();
					if ( jThis.is("label[for]") ) {
						var jFormControl = jRef;
						var jLabels = jThis;
					} else {
						var jFormControl = jThis;
						var jLabels = jRef;
					}
					if ( !( jFormControl.length > 0 && jLabels.length > 0 ) ) return true;	// continue
					
					jLabels.each( function() {
						$(this).addClass("jsOverLabel");
						if ( jFormControl.val() === "" ) jFormControl.parent().addClass("jsOverLabelBlur");
						jFormControl
							.focus( function() { $(this).parent().removeClass("jsOverLabelBlur"); } )
							.blur( function() { 
								var jFormControl = $(this);
								if ( jFormControl.val() === "" ) jFormControl.parent().addClass("jsOverLabelBlur");
							} )
							.click( function() { $(this).forRef().trigger("focus"); } );
					} );
				} );
		},
		
		clickable: function( cpTtl ) { // v0.1.8
			//	Makes elements clickable, linking to location specified by href= attribute of first descending link (where href is not "#" and does not start with "javascript:").
			// Args.:	cpTtl:	[Boolean]	Specifies whether or not to copy the title= attribute from the link to clickable element (default: true)
			//	CSS:		.jsClickable (on this)
			//				.jsClickableHover (on this:hover)
			//				.jsGuide (on guiding link)
			return this
				.each( function() {
					var jClickElem = $(this);
					var jGuideLink = $("a[href]:not([href='#']):not([href^='javascript:'])", jClickElem).eq(0);
					var href = jGuideLink.attr("href");
					if ( !href ) return true;	// continue
					
					$(this).data("href", href);
					if ( ( cpTtl || cpTtl == null ) && !this.title && jGuideLink[0].title ) this.title = jGuideLink[0].title;
					jClickElem
						.click( function() { window.location.href = $(this).data("href"); } )
						.hover(
							function() { $(this).addClass("jsClickableHover"); },
							function() { $(this).removeClass("jsClickableHover"); }
						)
						.addClass("jsClickable");
					jGuideLink.addClass("jsGuide");
				} );
		},
		
		selectNav: function() { // v0.1.7
			//	Adds auto submit behavior to select boxes, for navigational use. Works with keyboard too (in IE, FX and Op)
			//	CSS:		.jsSelectNav (on parent of select box)
			return this
				.filter("select")
				.each( function() {
					$(this)
						.focus( function() { $(this).data("origValue", this.value); } )
						.change( function() {
							if ( $(this).data("newValue") ) $(this).data("origValue", $(this).data("newValue"));
							$(this).data("newValue", this.value);
						} )
						.blur( navigate )
						.click( navigate )
						.parent().addClass("jsSelectNav");
					
					function navigate() {
						var newValue = $(this).data("newValue");
						if ( newValue && newValue != $(this).data("origValue") ) this.form.submit();
					};
				} );
		},
		
		popLink: function( props ) { // v0.1.8
			// Opens matching links in popup. Default properties can be overridden with props argument
			// Args.:	props:	[Object]		Key/value pairs of properties that differ from default
			//	CSS:		.jsPopup (on link, default class name can be overridden)
			
			// Default properties may be changed and can be overridden on function call:
			var defProps = {
				target: "_blank",			// [String]		Target name of popup
				blank: false,				// [Boolean]	Blank window: true > no properties are applied / false > properties are applied 
				width: 500,					// [Integer]	Width popup window
				height: 550,				// [Integer]	Height popup window
				absH: false,				// [Boolean]	Horizontal positioning: true > absolute / false > center
				absV: false,				// [Boolean]	Vertical positioning: true > absolute / false > center
				popH: 640,					// [Integer]	Horizontal position. If absHor is false, this value will be used as minimal screen width in case the available screen width is unknown
				popV: 480,					// [Integer]	vertical position. If absVert is false, this value will be used as minimal screen height in case the available screen height is unknown
				print: false,				// [Boolean]	Print version. If true, the toolbar will be shown in case the browser does not support window.print
				props: ["resizable"],	// [Array]		Array with, comma seperated, window propertie names [String] which must be 'on'. E.g. ['location','scrollbars']
				keepRef: false,			// [Boolean]	Keep reference to popup window. If true a reference will be kept in $.data("ref") stored on the link
				className: "jsPopup",	// [String]		Class name set on popup link
				replaceLoc: false			// [Boolean]	Replace popup location in browser's history or not
			};
			
			// Do not change code below:
			if ( !props ) props = {};
			for ( var p in defProps ) if ( !props[p] ) props[p] = defProps[p];
			
			var propString = "";
			if ( !props.blank ) {
				if ( props.print && !window.print && $.inArray("toolbar", props.props) == -1 ) props.props.push("toolbar");
				
				function calcPos( prop, dim ) {
					if (window.screen && window.screen.availWidth) prop = window.screen["avail" + dim];
					return Math.round((prop - props[dim.toLowerCase()]) / 2);
				};
				if ( !props.absH ) props.popH = calcPos( props.popH, "Width" );
				if ( !props.absV ) props.popV = calcPos( props.popV, "Height" );
				
				propString = "width=" + props.width + ",height=" + props.height + ",left=" + props.popH + ",top=" + props.popV;
				if ( props.props.length > 0 ) propString = propString + "," + props.props.join(",");
			};
			
			return this
				.filter("a[href], area[href]")
				.each( function() {
					$(this)
						.data("popup", {
							target:	props.target,
							props:	propString,
							repl:		props.replaceLoc,
							keepRef:	props.keepRef
						} )
						.click( function(e) {
							var popProps = $(this).data("popup");
							var popup = window.open(this.href, popProps.target, popProps.props, popProps.repl);
							if ( popProps.keepRef ) $(this).data("ref", popup);
							e.preventDefault();;
						} )
						.addClass(props.className);
				} );
		}
		
	} );

	$.extend( {
	/* ========================
		==  Custom functions  ==
		======================== */
	
		expandAskJo: function() {
			// == Animate Ask Jo ==
			var jAskJo = $("#sidebar .search");
			jAskJo
				.addClass("jsAnimSearch")
				.mousedown( function() { setActive(true); } )
				.click( function() {
					setActive(true);
					$("[type='text']", this).focus();
				} );
			
			$("[type='text']", jAskJo)
				.focus( function() {
					setActive(false);
					$("#sidebar .search").animate({ width: "340px" }, 100);
					$(this).animate({ width: "307px" }, 100);
				} )
				.blur( function() {
					if ( $("#sidebar .search").data("active") ) return;
					setActive(false);
					$(this).animate({ width: "137px" }, 100);
					$("#sidebar .search").animate({ width: "170px" }, 100);
				} );
				
			$("[type='submit'], [type='image']", jAskJo)
				.click( function(e) {
					e.stopPropagation();
					setActive(false);
				});
				
			function setActive(bool) {
				$("#sidebar .search").data("active", bool);
			};
		},
		
		locationForm: function() {
			// == Update location form ==
			/*var ajaxURL = window.locationFormURL;	// defined in HTML doc
			if ( !ajaxURL ) return;
			ajaxURL += ( ajaxURL.indexOf("?") == -1 ) ? "?" : "&amp;";
			
			var from = $("#fromLocation");
			from.data("cache", {});
			var to = $("#toLocation");
			to.data("cache", {});
			$("#fromType select, #toType select")
				.change( function() {
					var jSelect = $(this);
					var type = jSelect.parents().is("#fromType") ? "from" : "to";
					var loc = encodeURI(jSelect.val());
					var typeForm = $("#" + type + "Location");
					var locForm = typeForm.data("cache[" + loc + "]");
					if ( locForm ) {
						typeForm.html(locForm);
					} else {
						typeForm
							.empty()
							.load(ajaxURL + type + "=" + loc, function() {
							var html = $(this).html();
							if ( html != "" ) {
								typeForm.data("cache[" + loc + "]", html);
							} else {
								jSelect.get(0).form.submit();
							}
						});
					}
				})
				.parent().addClass("jsUpdateForm");;*/
				$(".planuwreis").addClass("jsUpdateForm");
		},
		
		selectTimetable: function() {
			// == Select timetable ==
			var ajaxURL = window.stopSelectURL;	// defined in HTML doc
			if ( !ajaxURL ) return;
			ajaxURL += ( ajaxURL.indexOf("?") == -1 ) ? "?" : "&;";
					
			var trackField = $("#trackField").after('<div id="stopField"></div>');
			var stopField = $("#stopField").hide();
			stopField.data("cache", {});
			var button = $("#travelplanner .dienstregeling .column button").hide();
			$("select", trackField)
				.eq(0)
				.change( function() {
					var track = encodeURI($(this).val());
					if ( !track ) {
						stopField.hide().empty();
						button.hide();
					} else {
						var stops = stopField.data("cache[" + track + "]");
						if (stops) {
							stopField
								.html(stops)
								.show();
						} else {
							stopField
								.empty()
								.load(ajaxURL + "track=" + track, function() {
								var jThis = $(this);
								var html = jThis.html();
								if ( html != "" ) {
									jThis.show();
									stopField.data("cache[" + track + "]", html);
								}
							});
						}
						button.show();
					}
				} );
		},
		
		overlay: function() {
			// == Initialize overlay ==
			var openOverlay = $("a.openOverlay");
			if ( openOverlay.length > 0 ) $("#content").append('<div id="overlay"><div id="overlayContent"></div><p id="closeOverlay"><a href="#"><img src="/include/images/ico_close.gif" width="21" height="22" alt="Sluiten" /></a></p><div class="shadow"><div class="shTop"></div><div class="shSides"></div><div class="shBottom"></div></div></div>');
			
			$("#closeOverlay")
				.click( function(e) {
					e.preventDefault();
					$("#overlay").fadeOut(150, function() {
						$("#content").removeClass("jsOverlayOpen")
						$("#overlayContent", this).empty();
					} );
				} );
			
			openOverlay
				.click( function(e) {
					e.preventDefault();
					var jThis = $(this);
					var content = jThis.data("cache");
					if ( content ) {
						$("#overlayContent").html(content);
						fadeInOverlay();
					} else {
						var href = encodeURI(jThis.attr("href"));

						$("#overlay").css("top", $().scrollTop() + 1);
						
						href += ( href.indexOf("?") == -1 ) ? "?" : "&";
						$("#overlayContent")
							.html("<p>Content wordt geladen...</p>")
							.load(href + "overlay=true", function() {
								var jContent = $(this).html();
								if ( jContent == "<p>Content wordt geladen...</p>" || jContent == "" ) {
									window.location.href = href;
								} else {
									fadeInOverlay();
									jThis.data("cache", jContent);
								}
							} );
					}
				} );
				
			function fadeInOverlay() {
				$("#content").addClass("jsOverlayOpen");
				$("#overlay").fadeIn(150);
			}
		},
		
		tabs: function() {
			// == Add tab behavior ==
			
			// Service en verkoop:
			var content = $("#serviceenverkoop");
			if ( content.get(0) ) {
				$(".tabs .navigation a", content)
					.each( function() {
						var jTabLink = $(this);
						var jTab = jTabLink.hashRef().eq(0);
						if ( jTab[0] && !jTabLink.parent().hasClass("current") ) jTab.hide();
						jTabLink.data("tab", jTab);
					} )
					.click( function(e) {
						e.preventDefault();
						$(".tabs .tab", content).hide();
						$(this).data("tab").show();
						$(".tabs .navigation li", content).removeClass("current");
						$(this).parent().addClass("current");
					} );
					
			}
		},
		
		addPrintLinks: function() {
			if ( !window.print ) return;
			var jReisadvies = $("#reisadvies");
			/*$("ul.options", jReisadvies).append('<li><a class="print" href="#"">Afdrukken</a></li>');
			jReisadvies.append('<p class="options"><a class="print" href="#">Afdrukken</a></p>');*/
			$("a.print", jReisadvies).click( function(e) {
				e.preventDefault();
				window.print();
			} );
		}
		
	} );
} )(jQuery);


/* ======================
	==  Function calls  ==
	====================== */

jQuery( function( $ ) {

	// Vacatures:
	$("#vacatures select, #zoekresultaten select, #dienstregeling .toonresultaten select").selectNav();

	// Travelplanner:
	$.locationForm();
	$.selectTimetable();
	
	// Print:
	$.addPrintLinks();

	//Search Results:
	$("#zoekresultaten .zoekresultaten .path").clickable(); 

	// Animate Ask Jo:
	$.expandAskJo();
	
	// overlay:
	$.overlay();
	
	// tabs:
	$.tabs();
	
	// popups
	$("a.popup, area.popup").popLink();
	
	
	//countdownOverlay

    $('#countdownOverlay a.closeCountdown').click(function() {
        $('#countdownOverlay').hide();
        $('#content #travelplanner .planuwreis select').show();
    });
   
    function showSelect(){
        $('#countdownOverlay').hide();
        $("#content #travelplanner .planuwreis select").css('display', 'block');
    }
    
    setTimeout(showSelect, 10000);
    	
} );