/*=========================
  CORE LEVEL
========================*/

$(document).ready(function() {	
	// Default AJAX Settings
	$.ajaxSetup({
		url: 'Ajax',
		type: 'POST',
		dataType: 'html'
	});
});

jQuery.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;}
});


jQuery.expr[':'].shadowbox = function(obj) {
	return typeof $(obj).attr('rel') != 'undefined' && $(obj).attr('rel').match(/^shadowbox/i);
}

String.prototype.ucwords = function() {
	var out = []; var words = this.replace(/[\_\-]+/i,' ').split(' ');
	for (var i in words) {
		if (typeof words[i] == 'string') out.push(words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase());
	}
	return out.join(' ');
}

function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]=a[i];
  }
  return o;
}

function showLoadingForeground() {
	$('<div />').css({
		'background-image':"url:('/images/loadingBkg.png')",
		'background-repeat':'repeat',
		'position':'fixed',
		'top':'0',
		'left':'0',
		'height':'100%',
		'width':'100%'
	}).attr('id','SystemIsLoading').append($('<img />').attr('src','/images/loading.gif').css({
		'position':'relative',
		'top':'45%',
		'left':'45%'
	})).prependTo('body');
}

function removeLoadingForeground() {
	$('#SystemIsLoading').remove();
}


var AYR = {};
AYR.system 	= {sitemap: 'sitemap', search: 'search', productlist: 'productlist', product: 'product', sitecontrol: 'sitecontrol', payment: 'payment'};
AYR.ajax 	= function(obj, data, callback) {
					async = (typeof callback == "boolean" && callback);
					if (typeof data.callBack == "undefined" || typeof data.pageName == "undefined") {
						
						if (AYR.isModule(AYR.system, obj)) data.callBack = obj;
						else data.pageName = obj;

						if (async) return jQuery.ajax({data: data, async: false}).responseText;
						else jQuery.ajax({data: data, success: callback});
					} else {
						alert("Ajax call for " + obj + " failed. Data.callBack and data.pageName are reserved properties and cannot be used");
					}
				};
AYR.isModule = function(obj, val) {
					for (var i in obj) {
						if (i == val) {
							return true;
						}
					}
					return false;
				};

function adjustIFrameSize (iframeWindow) {
	var iframeElement = false;
	var contentHeight = iframeWindow.document.getElementById('MainContent').clientHeight;
	if (parseInt(contentHeight)<=0 || !contentHeight) contentHeight = iframeWindow.document.getElementById('MainContent').offsetHeight;

	if (typeof $ != 'undefined') {
		iframeElement = $('#iframeID').get(0);
	} else {
		if (iframeWindow.innerHeight) {
			iframeElement = document.getElementById("iframeID");
		} else if (document.all) {
			iframeElement = document.all["iframeID"];
		}
	}

	if (iframeElement && contentHeight) {
		iframeElement.style.height = contentHeight + 5 + "px";
		iframeElement.scrolling = "no";
	}
}

function global_openWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function printSafeWindow(ItemId,RetailerId) {
    window.open('/catalogues/print.php?item_id='+ItemId+'&retid='+RetailerId,'printWindow','width=400,height=600');
}

