// JavaScript Document

var browserName=navigator.appName; 
var browserVer=parseInt(navigator.appVersion); 
function checkLens(){
	
	if(browserName=="Microsoft Internet Explorer" && browserVer<7){
		avril = document.getElementById('page').scrollHeight;
		document.getElementById('container').style.height = (avril)+"px";
	}
	else{
		if(document.scrollHeight < document.getElementById('page').scrollHeight){
			document.getElementById('page').scrollHeight = window.scrollHeight;
		}
		avril = document.getElementById('page').scrollHeight;
		document.getElementById('container').style.height = (avril)+"px";
	}
	//alert(avril);
}

function urlEncode( s ){
return encodeURIComponent( s ).replace(  /' '/g, '%20' ).replace( /!/g, '%21' ).replace( /\'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /\~/g, '%7E' ).replace(/\-/g, '%2D').replace(/\./g, '%2E');
   }
   
function urlDecode( s ){
return decodeURIComponent( s.replace( /\%20/g, ' ' ).replace( /\%21/g, '!' ).replace( /\%27/g, "'" ).replace( /\%28/g, '(' ).replace( /\%29/g, ')' ).replace( /\%2A/g, '*' ).replace( /\%7E/g, '~' ).replace(/\%2D/,'-').replace(/\%2E/g, '.') );
   }

function okChars(s){
return urlEncode(s).replace(/\W/g,'_');
}

/****************************/
/*check for third party host*/
/****************************/

var hosty = window.location.hostname;
		
if (hosty.indexOf('shopridley') == -1) 
	var tp = true;
else
	var tp = false;


/****************************/
/* STRIP OUT URL FORMATTING */
/****************************/

function myunescape (str){
	str = "" + str;
	while (true){
		var i = str . indexOf ('+');
		if (i < 0)
			break;
		str = str . substring (0, i) + '%20' + str . substring (i + 1, str . length);
	}
	return decodeURIComponent(str);
}


/***********************/
/* SHOPPING LIST ITEMS */
/***********************/

var shoppingCount = 0;

function jaxSL(what){
	
	if(what){
		$('#dialog').html('<center><br/><br/><img src="/images/spinner.gif" border="0" /></center>');
		overlay();
	}
	
	$.ajax({
		url: 'http://shopridleys.com/_shoppingListXML.php',
		type: 'POST',
		dataType: 'xml',
		timeout: 5000,
		error:function (xhr, ajaxOptions, thrownError){
			/*alert(xhr.status);
			alert(ajaxOptions);
			alert(thrownError);*/
		},
		success: function(xml){
			var tOut;
			clearTimeout(tOut);
			$('#rolls #added').show(0).html('Added');
			tOut = setTimeout(function(){$('#rolls #added').fadeOut(500)},2000);
			shoppingCount = 0;
			qty = $(xml).find('qty').text();
			var adMsg = '';

			if(qty == 0){
				adMsg = '<div style="font-size:14px; line-height:20px; padding:10px;">You have not added any items to your shopping list yet. Add recipe ingredients or enter your own items below.';
			}else{
				adMsg += '<div id="shoppingListContent">';
				if(!what)
					adMsg += '<div align="right" style="font-size:14px"><a href="javascript:winOpen(\'http://shopridleys.com/wc/print.php\')"><img src="/images/print.png" border="0" align="absmiddle" /> Print Shopping List</a> | <a href="javascript:delAllSL()"><img src="/images/drop.png" border="0" align="absmiddle" /> Empty Shopping List</a></div>';

				if(!what){	
					first = true;	
					$(xml).find('rec').each(function(){
						if(first){
							adMsg += '<h2>Recipe Ingredients</h2>';
							first = false;
						}
						adMsg += '<h3>' + myunescape($(this).find('title').text())+ "</h3>\n";
						var recid = $(this).find('recid').text();
						$(this).find('recitem').each(function(){
								adMsg += '<div class="anItem" id="_r_' + recid + '_' + $(this).find('id').text() + '_r_"><span style="float:right"><a href="javascript:delSL(\'_r_' + recid + '_' + $(this).find('id').text() + '_r_\');" class="delete" style="margin:0px 0px 5px 10px" title="Delete item"><img src="/images/drop.png" border="0" width="14" height="14" /></a></span><b>' + myunescape($(this).find('description').text()) + "</b><br/>\n";
								adMsg += "</div>\n";	
								shoppingCount++;
						});
					});	
				
					first = true;	
					$(xml).find('useritem').each(function(){
						if($(this)){
							if(first){
								adMsg += '<h2>My Items</h2>';
								first = false;
							}
							adMsg += '<div class="anItem" id="_u_' + $(this).find('id').text() + '_u_"><span style="float:right"><a href="javascript:delSL(\'_u_' + $(this).find('id').text() + '_u_\');" class="delete" style="margin:0px 0px 5px 10px" title="Delete item"><img src="/images/drop.png" border="0" width="14" height="14" /></a></span><b>' + myunescape($(this).find('description').text()) + "</b><br/>\n";
							adMsg += "</div>\n";	
							shoppingCount++;
						}
					});
				}
			}

			// UPDATE QUANTITY COUNT
			var qty = $(xml).find('qty').text();
			$('#listQty').html(qty + (qty == 1 ? ' item' : ' items'));
			$.cookie('listQty',qty,{ path:'/', expires: 7 });
			//$('#overlay').hide();
			$('#dialog').html('<div id="shoppingList"><div style="padding:0px 10px 6px; font-size:16px; font-weight:900; text-align:center; background-color:#c4dfe0;"><span style="float:right; cursor:pointer;" onclick="$(\'#overlay\').fadeOut(100); jaxSL(null)">X</span>' + (what ? 'Add to ' + what + '\'s Menu' : 'My Shopping List') + '</div>' + adMsg + '<br/>' + (!what ? '<form name="addListSL" onsubmit="return addItemSL()"><br/><input type="text" size="50" name="description" value="Add item..."  onfocus="if(this.value == \'Add item...\') this.value = \'\';" onblur="if(this.value == \'\') this.value = \'Add item...\';"/> <input type="submit" value="Add to List" /></form><br/>' : '') + '</div></div>');
		}
	});
}
function addItemSL(){
	if(document.addListSL.description.value != 'Add item...'){
		$.ajax({
			url: 'http://shopridleys.com/_addItem.php',
			type: 'POST',
			data: 'description=' + escape(document.addListSL.description.value),
			dataType: 'xml',
			timeout: 2000,
			error: function(){
				//alert('Error loading XML document');
			},
			success: function(xml){
				code = $(xml).find('code').text();
				if(code == '101'){
					//alert("h "+$(xml).find('id').text());
					addSL($(xml).find('id').text(),'u');
					document.addListSL.description.value = 'Add item...';
				}
			}	
		});
	}
	return false;
}

function addSL(what,type){
	if(type == 'w')
		makeCookieSL(idArray[what],type);
	else
		makeCookieSL(what,type);
	jaxSL(null);
}

function delSL(what){
	var daCookie = $.cookie("shoppinglist").replace(what + ",",'');
	$.cookie("shoppinglist", daCookie, { path:'/', expires: 7 });
	$('#'+what).remove();

	if(what.indexOf("_r_") >= 0)
		$.post("http://shopridleys.com/_delItem.php", { id: what } );

	shoppingCount--;
	$('#listQty').html(shoppingCount + (shoppingCount == 1 ? ' item' : ' items'));
	$.cookie('listQty',shoppingCount,{ path:'/', expires: 7 });
}

function delAllSL(){
	if(confirm('Empty Shopping List?')){
		$.cookie("shoppinglist", null, { path:'/', expires: 7 });
		$.cookie("recItems", null, { path:'/', expires: 7 });
		jaxSL(null);
	}
}
	

function makeCookieSL(what,type){
	//alert(what);
	if(what != null){
		if($.cookie("shoppinglist")){
			if($.cookie("shoppinglist").indexOf("_"+type+"_" + what + "_"+type+"_,") < 0){
				var daCookie = $.cookie("shoppinglist") + "_"+type+"_" + what + "_"+type+"_,";
				$.cookie("shoppinglist", daCookie, { path:'/', expires: 7 });
			}
		}
		else{
			var daCookie = "_"+type+"_" + what + "_"+type+"_,";
			$.cookie("shoppinglist", daCookie , { path:'/', expires: 7 });
		}
	}
	//alert($.cookie("shoppinglist"));

}


function slLogin(what){
	$('#dialog').html('<div style="padding:0px 10px 6px; font-size:16px; font-weight:900; text-align:center; background-color:#c4dfe0;"><span style="float:right; cursor:pointer;" onclick="$(\'#overlay\').fadeOut(100); jaxSL(null)">X</span>My Shopping List</div><iframe src="http://shopridleys.com/company/contact.php" width="550" height="430" frameborder="0"></iframe>');	
	overlay();
}


function winOpen(url){
	window.open(url,'popup_window','width=550,height=400,status=0,location=0,menubar=0,toolbar=0,scrollbars=1');	
}

function openWin(url){
	window.open(url,'popup_window','width=550,height=400,status=0,location=0,menubar=0,toolbar=0,scrollbars=1');	
}

function getUrlVars(){
    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;
}


$(window).load(function() {
	if(tp){
		//
	}else{
		jaxSL(null);
	}
	$('#overlay').hide();
});
