/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	/**
	 * Cookie plugin
	 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
	 * Dual licensed under the MIT and GPL licenses:
	 * http://www.opensource.org/licenses/mit-license.php
	 * http://www.gnu.org/licenses/gpl.html
	 */
	jQuery.cookie = function(name, value, options) {
		if (typeof value != 'undefined') { // name and value given, set cookie
			options = options || {};
			if (value === null) {
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
				var date;
				if (typeof options.expires == 'number') {
					date = new Date();
					date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
				} else {
					date = options.expires;
				}
				expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
			}
			// CAUTION: Needed to parenthesize options.path and options.domain
			// in the following expressions, otherwise they evaluate to undefined
			// in the packed version for some reason...
			var path = options.path ? '; path=' + (options.path) : '';
			var domain = options.domain ? '; domain=' + (options.domain) : '';
			var secure = options.secure ? '; secure' : '';
			document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		} else { // only name given, get cookie
			var cookieValue = null;
			if (document.cookie && document.cookie != '') {
				var cookies = document.cookie.split(';');
				for (var i = 0; i < cookies.length; i++) {
					var cookie = jQuery.trim(cookies[i]);
					// Does this cookie string begin with the name we want?
					if (cookie.substring(0, name.length + 1) == (name + '=')) {
						cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
						break;
					}
				}
			}
			return cookieValue;
		}
	};
	
	$.extend({
		getUrlVars: function(){
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for(var i = 0; i < hashes.length; i++) {
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			return vars;
		},
		getUrlVar: function(name){
			return $.getUrlVars()[name];
		}
	});
		
	$.extend({URLEncode:function(c){var o='';
		var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
		while(x<c.length){
			var m=r.exec(c.substr(x));
			if(m!=null && m.length>1 && m[1]!=''){
				o+=m[1];x+=m[1].length;
			} else {
				if(c[x]==' ')o+='+';
				else{
					var d=c.charCodeAt(x);var h=d.toString(16);
					o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;
				}
			}return o;
		},
		URLDecode:function(s){
			var o=s;
			var binVal,t;
			var r=/(%[^%]{2})/;
			while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){
				b=parseInt(m[1].substr(1),16);
				t=String.fromCharCode(b);o=o.replace(m[1],t);
			}return o;
		}
	});
	
	//same as $(document).ready();
	$(function() {
		
		/* Lightbox init */
		$("a.lightbox").fancybox({
			'overlayShow': true,
			'overlayColor': '#ffffff',
			'overlayOpacity': 0.75,
			'autoScale': false,
			'titleShow': false,
			'zoomSpeedIn': 600,
			'zoomSpeedOut': 500
		});
		$("a.lightiframe").fancybox({
			'overlayShow': true,
			'overlayColor': '#ffffff',
			'titleShow': false,
			'overlayOpacity': 0.75,
			'zoomSpeedIn': 600,
			'zoomSpeedOut': 500,
			'frameWidth': 493,
			'frameHeight': 310,
			'hideOnContentClick': false
		});
		// externalLinks();
		$("a[rel=external]").each(function(i) {
			this.target = "_blank";
		});
		
		$("#box-news li a").bind("mouseenter",function(){
			$("#box-news li a").removeClass('active');
			$(this).addClass('active');
		});
		$("#box-projects li a").bind("mouseenter",function(){
			$("#box-projects li a").removeClass('active');
			$(this).addClass('active');
		});
		$("#box-events li a").bind("mouseenter",function(){
			$("#box-events li a").removeClass('active');
			$(this).addClass('active');
		});
		$('#slider-inner ul').anythingSlider({
			width               : 690,   // if resizeContent is false, this is the default width if panel size is not defined
			height              : 269,   // if resizeContent is false, this is the default height if panel size is not defined
			resizeContents      : true, // If true, solitary images/objects in the panel will expand to fit the viewport
			startStopped        : false,  // If autoPlay is on, this can force it to start stopped
			navigationFormatter : function(index, panel){ // Format navigation labels with text
				return ['1', '2', '3', '4'][index - 1];
			}
		});
		// hide/show default search text
		$("#sitesearch").click(function() { 
			var default_search_text = "Zoeken";
			  if (this.value == default_search_text) {
				this.value = "";
			  }
		}).blur(function() { 
			var default_search_text = "Zoeken";
			  if (this.value =="" ) {
				this.value = default_search_text;
			  }
		});
		// just to be sure (but mainly for ie7/8):
		 $("#content div.box:last-child").css({"margin-right":"0px"});
		// print this page
		$("#print_this_page").click(function() { 
			window.print();
		});
		
		$('div.faces.detail h1').after($('div.data.faces'));

	});
	
	function setOrientation() {
		if(window.orientation != undefined) {
			var orient = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait';  
			var inv_orient = orient == 'portrait' ?  'landscape' : 'portrait';
			$(document.body).addClass(orient);
			$(document.body).removeClass(inv_orient);
			//alert(window.orientation);
			//alert(document.body.className);
			if(orient == 'portrait') {
				$('p.logo a img').remove();
				$('p.logo a').append('<img src="/joepla/_/img/logo.png" alt="logo" title="home" />');
			} else {
				$('p.logo a img').remove();
			}
		}
	}
	

	$(window).bind("load", function() {
		setOrientation();

		if($('#pit_tinymenu')) {
			/* ui actions for the Pit Tiny Menu (PTM) */
			// store settings in array and init ptm
			var ptm_settings = new Array();
			ptm_settings['ptm_display'] = "none";
			$('#pit_tinymenu_items').hide();		
			// toggle the PTM based on cookie
			if ($.cookie('ptm_display')) {
				ptm_settings['ptm_display'] = $.cookie('ptm_display');
				if(ptm_settings['ptm_display'] == "none") {
					$('#pit_tinymenu_items').hide();
				} else {
					$('#pit_tinymenu_items').show();
				}
			} else {
				$.cookie('ptm_display', ptm_settings['ptm_display'], { path: '/' });	
			}
			// toggle the PTM (and store state in cookie)
			$('#pit_tinymenu_nav_btn_link').click(function() {
				$('#pit_tinymenu_items').slideToggle('fast', function() {
					ptm_settings['ptm_display'] = ( ptm_settings['ptm_display'] == "none" )? "block" : "none"											
					$.cookie('ptm_display', ptm_settings['ptm_display'], { path: '/' });
				});
			});
		}

		
	});
	$(window).bind("orientationchange", function() {
		setOrientation();
	});
	
})(jQuery);

// window.addEventListener('load', setOrientation, false); 
// window.addEventListener('orientationchange', setOrientation, false);

