
/** Small Search Form Javascript **/

var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}


/**
 * This function takes the query for a search page and adds it onto the URL for SEO purposes.
 *
 * @param string formID This is the ID of the form to get the search term from and to submit. ID should not have a '#'.
 */
var originalSmallSearchFormAction = '';
function CheckSmallSearchForm(formID){
	var search = $('#' + formID + ' .smallSearch_Query').val();
	search = search.replace(/ /g, '+');
	search = escape(search);

	$('#' + formID).attr('action',  originalSmallSearchFormAction + '/' + search);
}

/**
 * This function opens a popup window to display the search tips
 */

function launchSearchTips() {
	var urlToOpen = iwp_global_siteurl + "/searchtips.html";
	var day = new Date();
	var id = day.getTime();
	window.open(urlToOpen, "'" + id + "'", 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=600,height=500');
}

$(document).ready(function() {
	var url = '';
	url = document.location.href;
	if(url.substr(-1, 1) == '/'){
		url = url.substr(0, (url.length - 1));
	}

	// @TODO: Add in a fix for finding the current URL for links that are using /site/path instead of http://www.example.com/site/path

	$('.tplIsCurrentItem:has(a[href='+url+'])').each(function(){
		$(this).addClass('currentItem');
	});

	$('.tplIsCurrentItem:has(a[href='+url+'/])').each(function(){
		$(this).addClass('currentItem');
	});
	if (url.indexOf('http://wipmedia.se') != -1) {
		new_url = url.replace('http://wipmedia.se','');
	} else {
		new_url = url.replace('http://www.wipmedia.se','');
	}
	new_url = Url.decode(new_url);
	$('.tplIsCurrentItem:has(a[href='+new_url+'])').each(function(){
		$(this).addClass('currentItem');
	});
	
	$('.smallSearchForm').each(function(){
		var formID = $(this).attr('id');
		originalSmallSearchFormAction = $(this).attr('action');

		$(this).submit(function (){
			CheckSmallSearchForm(formID);
		});
	});
});

