
/* ++++++++++++++++++++++++++++++++
	++	colours.js
	++ extracted from					++
	++	jquery.pallette-0.1.11.js	++
	++										++
	++	Dependencies:					++
	++	- jQuery 1.2.6 or higher	++
	++++++++++++++++++++++++++++++++ */
	
	
(function( $ ) {
	$.fn.extend( {
	/* ================================
		==  Extending jQuery methods  ==
		================================ */
	
		palette: "0.1.11",
		colours:	"0.1",
	
		/*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.7
			//	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() { 						// needed for Webkit to pass focus to input
								$(this).forRef().each( function() {	// .focus() does not seem to work on every matched element, therefore .each() 
									this.focus();
								} );
							} );
					} );
				} );
		},
		
		clickable: function( cpTtl ) { // v0.1.9
			//	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 | optional]	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)
			//				.jsClickableFocus (on this, when guiding link gets focus)
			//				.jsGuide (on guiding link)
			return this
				.each( function() {
					var jClickElem = $(this);
					var jGuideLink = $("a[href]:not([href='#']):not([href^='javascript:'])", jClickElem).eq(0);
					if ( !jGuideLink ) return true;	// continue
					
					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
						.focus( function() { $(this).parents(".jsClickable").eq(0).addClass("jsClickableFocus"); })
						.blur( function() { $(this).parents(".jsClickable").eq(0).removeClass("jsClickableFocus"); })
						.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);
				} );
		},
		
		IEinlineIcons: function() { // v0.1
			// Appends a span with a non-breaking space to the matched elements (in IE) as a placeholder for an icon
			// CSS:		.jsIEfix (on matched elements)
			//				.jsInlineIcon (on appended span)
			return !$.browser.msie ? this : this.append('<span class="jsInlineIcon">&nbsp;</span>').addClass("jsIEfix");
		}*/
		
	} );
	
	$.extend( {
	/* ========================
		==  Custom functions  ==
		======================== */

		skipLinks: function() {
			$("#skipLinks a")
				.focus( function() {
					$(this)
						.addClass("jsFocus")
						.parents().eq(2).addClass("jsFocus");
				})
				.blur( function() {
					$(this)
						.removeClass("jsFocus")
						.parents().eq(2).removeClass("jsFocus");
				});
		},
		
		flashReplacements: function() {
			// check for jQuery Flash plugin, abort if not loaded
			if ( typeof $.fn.flash !== "function" && typeof $.fn.flash.hasFlash !== "function" ) return;
			
			// topVisual is default
			var topVisual = {
				elem:					"#headerContent",
				width:				960,
				height:				480,
				version:				7,
				wmode:				"transparent",
				expressInstall:	false,
				update:				false
			};
			
			var klanten = {
				elem:					"#coloursKlanten",
				width:				272,
				height:				72,
				version:				7,
				wmode:				"transparent",
				expressInstall:	false,
				update:				false
			};
						
			// replace content with Flash
			var flashContent = $.contentObj.flash;
			if ( flashContent && flashContent.constructor !== Object ) return;
			for ( var item in flashContent ) {
				var flashItem = flashContent[item];
				if ( !flashItem.src ) continue;
				
				// default to topVisual properties
				for ( var prop in topVisual ) {
					if ( !flashItem[prop] ) flashItem[prop] = topVisual[prop];
				};
				
				// insert Flash Content
				insertFlash( flashItem );
				
				// remove general alternative content
				$("#headerContent div.about").remove();
			};
			
			function insertFlash( f ) {
				$(f.elem).flash(
					{ src: f.src, width: f.width, height: f.height, wmode: f.wmode, flashvars: f.flashvars },
					{ version: f.version, expressInstall: f.expressInstall, update: f.update },
					f.callback
				);
			};

		},
		
		skipOffscreenContent: function( elem ) {
			// remove offScreen elements for sIFR
			$(".offScreen", elem).remove();
		},
		
		sidebarTabs: function() {
			var selected = "(geselecteerd)";		// string added to selected item
			var tab = {};
			tab.img = {};
			tab.img.path = "/images/ColoursWebsite/layout/";		// pathName tab image
			tab.img.ext = "gif";						// extension tab image
			tab.img.prefix = "tab_sidebar_";		// prefix filename image
		
			var tabsWrapper = $("#sidebar .tabs");
			if ( tabsWrapper.length == 0 ) return;
			
			var tabHeaders = $(".tab h2", tabsWrapper);
			if ( tabHeaders.length < 2 ) return;
			
			var items = "";
			tabHeaders.each( function( i ) {
				var jThis = $(this);
				tab.txt = jThis.text();
				tab.hash = jThis.parents("div.tab").eq(0).attr("id");
				tab.name = tab.hash.replace(/sb/, "");
				tab.id = "tab" + tab.name;
				tab.img.src = tab.img.path + tab.img.prefix + tab.name.substring(0,1).toLowerCase() + tab.name.substring(1) + "." + tab.img.ext;
				
				items += '<li id="' + tab.id + '">' +
							'<a href="#' + tab.hash + '">' +
							'<img src="' + tab.img.src + '" alt="' + tab.txt + '" />' +
							'</a>' +
							'</li>';
							
				var tabsArr = tabsWrapper.data("tabs") || [];
				tabsArr.push( tab.id );
				tabsWrapper.data("tabs", tabsArr);
				
				if ( i == 0 ) tabsWrapper.addClass(tab.id);
				jThis.addClass("offScreen");
			});
			if ( items ) tabsWrapper.prepend('<ul class="nav">' + items + '</ul>');
			
			$("ul.nav li", tabsWrapper)
				.each( function() {
					var jThis = $(this);
					var alt = $("img", jThis).attr("alt");
					jThis.data("alt", alt);
					jThis.data("altSel", alt + " " + selected);
				})
				.click( function(e) {
					var jThis = $(this);
					var tabs = tabsWrapper.data("tabs");
					for ( var t = 0; t < tabs.length; t++ ) {
						var tabID = tabs[t];
						if ( jThis.attr("id") == tabID ) {
							tabsWrapper.addClass(tabID);
							setAlt(jThis, "altSel");
						} else {
							tabsWrapper.removeClass(tabID);
							setAlt($("#" + tabID), "alt");
						};
					};
					e.preventDefault();
				});
				
			function setAlt( tab, altType ) {
				$("img", tab).eq(0).attr("alt", tab.data(altType));
			};
		},
		
		iconOnLastElement: function() {
			// Set icon on last element
			// CSS:		.jsIcon (on last element)
			//				.jsIconOnLastElement (on parent)
			var lastChilds = $("#siteContent .jsClickable > :last-child");
			lastChilds
				.each( function() {
					var jThis = $(this);
					if ( jThis.hasClass("more") ) jThis = jThis.prev();
					jThis
						.append('<span class="jsInlineIcon">&nbsp;</span>')
						.addClass("jsIcon")
						.parent().addClass("jsIconOnLastElement");
				});
		},
		
		initSearchSuggest: function() {
			var fallback = "searchAllSubmit";
			var code = {};
			code.start = '<div id="searchSuggest"><p><em>Zoekresultaten</em></p>';
			code.end = '<input type="submit" class="button" id="' + ($.contentObj.suggestButton.id || fallback) + '" name="' + ($.contentObj.suggestButton.name || fallback) + '" value="Toon alle resultaten" /></div>';
			$("#siteSearch div").eq(0).data("suggCode", code);
		},
		
		buildSuggest: function( sg ) {
			var insert = "";
			for ( var c = 0; c < sg.length; c++ ) {
				var cat = sg[c];
				var results = cat["results"];
				var catStart = '<li><p>' + cat["category"] + '</p><ul>';
				var items = "";
				for ( var r = 0; r < results.length; r++ ) {
					var result = results[r];
					var item = '<li><a href="' + result["href"] + '"><em>' + result["title"] + '</em><span>' + result["content"] + '</span></a></li>';
					items += item;
				};
				var category = catStart + items + '</ul></li>';
				insert += category;
			};
			return ( insert ) ? '<ul class="nav">' + insert + '</ul>' : '';
		},
		
		removeSuggest: function() {
			$("#searchSuggest").remove();
		},
		
		addSuggest: function( suggestions ) {
			if ( suggestions ) {
				if ( suggestions.constructor == String ) insert = suggestions;
				else if ( suggestions.constructor == Array ) insert = $.buildSuggest( suggestions );
			};
			
			// remove previous results:
			$.removeSuggest();
			
			// insert updated results if there are any:
			if ( insert ) {
				var sParent = $("#siteSearch div").eq(0);
				var code = sParent.data("suggCode");
				sParent.append(code.start + insert + code.end);
			}
		},
		
		newsRotator: function() {
			var slideDuration = "5000";
		
			var rotator = $("#newsRotator").eq(0);
			if ( rotator.length < 1 ) return;
			
			var current = $("li.show", rotator).eq(0);
			if ( current.length < 1 ) {
				current = $("li:first", rotator).addClass("show");
				if ( current.length < 1 ) return;
			};
			
			var interval = setInterval( rotate, slideDuration );
			
			function rotate() {
				current.fadeOut("normal", function() {
					var jThis = $(this)
					jThis.removeClass("show");
					current = jThis.next();
					if ( current.length < 1 ) current = current = $("li:first", rotator);
					current.fadeIn("normal", function() {
						$(this).addClass("show");
					});
				});
			};
		},
		
		bodyClick: function() {
			$(document.body)
				.click( function(e) {
					var clicked = $(e.target).parents().andSelf();
					
					// hide #searchSuggest
					//if ( !clicked.is("#searchSuggest") ) $.removeSuggest();
					// 01/03/2009 V.Kaliutsich 
					//DON'T DELETE!!!!
					if(ResultsBlock) ResultsBlock.Show(e);
					
				});
		},
		
		initServicesOverview: function() {
			var jItems = $("#mainContent .servicesOverview h2");
			var first = jItems.parent().eq(0);
			var jOverview = first.parent().eq(0);
			jItems
				.click( function(e) {
					var jThis = $(this).parent().eq(0);
					var current = jOverview.data("current");
					if ( !jThis.hasClass("jsShow") ) $(".servicesOverviewContent", jThis).slideDown("fast", function(){ $(this).parent().eq(0).addClass("jsShow"); });
					if ( current ) $(".servicesOverviewContent", current).slideUp("fast", function(){ $(this).parent().eq(0).removeClass("jsShow"); });
					jOverview.data("current", jThis);
					e.preventDefault();
				});
			first.addClass("jsShow");
			jOverview
				.addClass("jsSlider")
				.data("current", first);
		}
		
	} );
} )(jQuery);

/* ======================
	==  Function calls  ==
	====================== */

jQuery( function( $ ) {
		
	$.contentObj = window.contentObj || {};

	// overLabel
	$("#siteSearch label").overLabel();	

	// enhance skipLinks keyboard navigation
	$.skipLinks();
	
	// create sidebar tabs
	$.sidebarTabs();
	
	// init serviceOverview collapse/expand
	$.initServicesOverview();
	
	// make entire items clickable
	$("#siteContent .clickable").clickable();
	$.iconOnLastElement();
	
	// init newsRotator
	$.newsRotator();
	
	// init search suggestions
	$.initSearchSuggest();
	
	// add event handlers for clicking body
	$.bodyClick();
	
	var sugStr = '<ul class="nav"><li><p>Klanten_Str</p><ul><li><a href="#"><em>Sitecore</em><span>Sitecore implementatie...</span></a></li><li><a href="#"><em>Smartsite</em><span>Smartsite implementatie...<br />Implementatie CMS Smartsite<br />Showcase project smartsite</span></a></li><li><a href="#"><em>GX</em><span>GX implementatie...</span></a></li></ul></li><li><p>Diensten</p><ul><li><a href="#"><em>Campina</em>Sitecore implementatie...</span></a></li><li><a href="#"><em>Campina</em><span>Smartsite implementatie...</span></a></li></ul></li></ul>';
	var sugArr = [{"category":"Klanten_Arr","results":[{"title":"Sitecore","href":"#","content":"Sitecore implementatie..."},{"title":"Smartsite","href":"#","content":"Smartsite implementatie...<br />Implementatie CMS Smartsite<br />Showcase project smartsite"},{"title":"GX","href":"#","content":"GX implementatie..."}]},{"category":"Diensten","results":[{"title":"Campina","href":"#","content":"Sitecore implementatie..."},{"title":"Campina","href":"#","content":"Smartsite implementatie..."}]}];
	//$.addSuggest(sugStr);
} );