/*
	--------------------------------
	cms common javascript
	----------------
*/
/* Main Jquery Document Ready Event */
$(document).ready(function() {
	// put all your jQuery goodness in here.
	if($('#feature-box').length != 0) { $('#feature-box').equalHeights(); }
	$('.top-most-search-element').click( function() {
		$(document).removeData("center-focus");								
	});
	$('.catalogue-search-keyword-input-text').click( function() {
		$(document).data("center-focus", "1");								
	});
	$('.catalogue-search-keyword-go-button').click( function() {
		$(document).data("center-focus", "1");								
	});
	$('input').keypress(function (event) {
		if( event.keyCode == 13 ) {
			if($(document).data("center-focus") == "1") {
				if(window.__catalogue_PostGo) { __catalogue_PostGo($('#catalogue-state-path').val()); }
			} else {
				if(window.generalSearchSubmit) { generalSearchSubmit(); }
			}
			return false;
		} else {
			return true;	
		}
	});
});
// help window
function showHelp(section) {
	var $url = '/cms/docs/__frame.php?section='+section+'';
	Boxy.load($url);
}
var $ajax = null;
function __ajaxCreate() {
	/* ajax object create */
	try { $ajax = new XMLHttpRequest(); }
	catch (e) {
		try { $ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { $ajax = new ActiveXObject("Microsoft.XMLHTTP"); }	
			catch (e) { $ajax = false; }
		}
	}
	if(!$ajax) {
		alert('You browser does not support AJAX, which is required to use this search feature');	
		return false
	} else {
		return true;
	}
}

// prototypes
Array.prototype.compare = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) { 
            if (!this[i].compare(testArr[i])) return false;
        }
        if (this[i] !== testArr[i]) return false;
    }
    return true;
}
// swap product image click
var $currentProductThumbnail = 1;
function clickProductThumbnail(index) {
	if(index != $currentProductThumbnail) {
		var $tmb = '#product-thumbnail-'+index;
		var $pic = '#product-image-li-'+index;
		var $prepic = '#product-image-li-'+$currentProductThumbnail;
		var $pretmb = '#product-thumbnail-'+$currentProductThumbnail;
		$($prepic).hide();
		$($pic).show();
		$($tmb).css("border","1px #333 solid");
		$($pretmb).css("border","1px #CCC solid");
		$currentProductThumbnail = index;
	}
}
// urlencode
function validEmailAddress(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if (str.indexOf(" ")!=-1){
		return false;
	}
	return true;
}
// urlencode
function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

// tinymce image window override
function mceInsertImageClick(tinymce_element) {
	openFileManagerPopup('IMAGES', '', tinymce_element.id, tinymce_element);
}

// common events
function windowResize() {
	if($(document).data("fileManagerActive") == 255) {
		resizeFileManagerPopup();
	}
}

// elements disable, enable
function disableFormElement(form, name, bool) {
	for (var i=0; i<form.elements.length; i++) {
		if (form.elements[i].name == name) {
			form.elements[i].disabled = bool;
		}
	}
}

// format currency
function currencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

 // Rollover Script add 

$(document).ready( function()
{
   PEPS.rollover.init();
});

PEPS = {};

PEPS.rollover =
{
   init: function()
   {
      this.preload();
     
      $(".ro").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_o\./, '.');
   }
};




// cookie things
function createCookie(name,value,days) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
// array things
function inArray(arr, value) {
	rtn = false;
	for(var i=0; i<arr.length; i++) {
		if(arr[i] == value) {
			rtn = true;	
		}
	}
	return rtn;
}
function arrayInArray(arr, ray) {
	rtn = false;
	for(var i=0; i<arr.length; i++) {
		if(arr[i].compare(ray)) {
			rtn = true;	
		}
	}
	return rtn;	
}
function arrayIndexOf(arr, value) {
	rtn = -1;
	for(var i=0; i<arr.length; i++) {
		if(arr[i] == value) {
			rtn = i;	
		}
	}
	return rtn;
}
function arrayIndexOfArray(arr, ray) {
	rtn = -1;
	for(var i=0; i<arr.length; i++) {
		if(arr[i].compare(ray)) {
			rtn = i;	
		}
	}
	return rtn;
}
function arrayJoin(arr, char) {
	var rtn = "";
	for(var i=0; i<arr.length; i++) {
		if((arr[i] == '' || arr[i] == null) || arr[i] == "undefined") {
		} else {
			if(i == 0) {
				rtn += arr[i];
			} else {
				rtn += (char + arr[i]);
			}
		}
	}
	return rtn;
}
function arrayDeleteIndex(arr, index) {
	var rtn = new Array();
	for(var i=0; i<arr.length; i++) {
		if(i != index) {
			rtn[rtn.length] = arr[i];
		}
	}
	return rtn;
}
function arrayDelete(arr, value) {
	var rtn = new Array();
	for(var i=0; i<arr.length; i++) {
		if(arr[i] != value) {
			rtn[rtn.length] = arr[i];
		}
	}
	return rtn;
}
/* 
gets the value at the index of array 2 that the value is at in array 1
arr1 = ['dog','cat','mouse','gerbil']
arr2 = ['loyal','devious','cute','wierd']
arrayAssociatedValueAt(arr1, arr2, 'mouse') returns 'cute'
*/
function arrayAssociatedValueAt(arr1, arr2, valu) {
	rtn = false;
	for(var i=0; i<arr1.length; i++) {
		if(arr1[i] == valu) {
			rtn = arr2[i];	
		}
	}
	return rtn;
}
/* confirm remove */
function removeListItem(rpage, id) {
	if(rpage == '') {
		rpage = '?void';	
	}
	if(confirm('Are you sure that you want to remove this item?')) {
		document.location = rpage+'&remove='+id+'';	
	}
}
/* get offsets for all dhtml popups */
function getPopupOffsets(ofsX, ofsY) {
	var cenx= $(window).width(); var ceny = $(window).height();
	var lofs = $(window).scrollLeft() + (cenx - ofsX) / 2;
	var tofs = $(window).scrollTop() + (ceny - ofsY) / 2;
	return new Array(tofs, lofs, $(window).scrollLeft(), cenx, $(window).scrollTop(), ceny);
}
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// 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;
	}
 
}
function showPassword(element, stri) {
	var sdiv = document.getElementById(element);
	if(sdiv) {
		sdiv.innerHTML = '<p>' + Base64.decode(stri) + '</p>';
	}
}
