/**
 * @title meredith rewards
 * @author kasey mccurdy / pyroblue multimedia, inc. / kc[/-\T]pyroblue.com
 */

/**********************************************************
 * Fixes, hacks, and stuff to make the world a better place
**********************************************************/
/* lets me use console.log like it's going' out of style */
try{console.log('init console... done'); } catch(e) { console = { log: function(){}}};

/**********************************************************
 * SHELL JAVASCRIPT
**********************************************************/
var show_kitchen_layer = function(user){
    sendLinkEvent('','Community: Rewards: Kitchen: '+user);  
	$('#view_kitchen_layer_wrap').slideDown("slow"); 
	console.log("show_kitchen_layer");
	$('#mdp_r').createAppend(
	    'div', { id: 'blackout' }
	);
	$("#blackout").css({opacity:.4},{height: $(window).height()});
	$("#blackout").click(function(){hide_kitchen_layer()});
}

var hide_kitchen_layer = function(){
	$('#view_kitchen_layer_wrap').slideUp("slow");
	$("#blackout").remove();	
}

var show_my_kitchen_layer = function(){
	$('#mdp_r').createAppend(
	    'div', { id: 'blackout' }
	);
	$("#blackout").css({opacity:.4},{height: $(window).height()});
	$("#blackout").click(function(){hide_my_kitchen_layer()});
	$('#my_kitchen_layer_wrap').slideDown("slow"); 
	console.log("show_my_kitchen_layer")
}
var hide_my_kitchen_layer = function(){
	$('#my_kitchen_layer_wrap').slideUp("slow"); 
	image_pinger($("#my_kitchen_image"),10,2000);
	$("#blackout").remove();	
}

var show_my_kitchen_store = function(){
    sendLinkEvent('','Community:Rewards:Kitchen Store');  
	$('#mdp_r').createAppend(
	    'div', { id: 'blackout' }
	);
	$("#blackout").css({opacity:.4},{height: $(window).height()});
	$("#blackout").click(function(){hide_my_kitchen_store()});
	$('#my_kitchen_store_wrap').slideDown("slow"); 
}
var hide_my_kitchen_store = function(){
	$('#my_kitchen_store_wrap').slideUp("slow"); 
	image_pinger($("#my_kitchen_image"),10,2000);
	$("#blackout").remove();	
}

var show_my_avatar_layer = function(){
	$('#mdp_r').createAppend(
	    'div', { id: 'blackout' }
	);
	$("#blackout").css({opacity:.4},{height: $(window).height()});
	$('#my_avatar_layer_wrap').slideDown("slow");
}
var hide_my_avatar_layer = function(){
	$('#my_avatar_layer_wrap').slideUp("slow");
    image_pinger($("#profile_avatar"),5,1000)
	$("#blackout").remove();	
}


var image_pinger = function(el, max_tries, speed){
	$('#kitchen_image_loading').remove();
	var tries = 0;
	var image_ping =  setInterval(function(){
		if(tries == max_tries){ clearInterval(image_ping); $("#kitchen_image_loading").hide("slow")}
		el.attr("src",el.attr("src")+("&pingcachebuster="+(Math.random()*100000)));
		tries++;
		console.log("trying");
	}, speed);
	el.after('<p id="kitchen_image_loading"><img src="/img/shell/indicator.gif"  />&nbsp;Image Loading</p>');
}

var email_profile = function(profile_id){
		$('#mdp_r').createAppend(
		    'div', { id: 'email_profile_layer', style: 'border: 2px solid #333; position:absolute;top:45px;left:145px;background-color:#FFF;padding:10px;' }, []
		);
	 $('#email_profile_layer').load("/views/shared/email_your_profile.php?u="+profile_id, function(){ }); 
}

var show_kitchen_upgrade_notice = function(lvl){
	//$.cookie("mdpr_hide_kitchen_upgrade_notice", null, { path: '/' });
	var notice_shown = $.cookie("mdpr_hide_kitchen_upgrade_notice_shown"); 
	var show_notice = $.cookie("mdpr_hide_kitchen_upgrade_notice");
	
	if (show_notice == null || show_notice < lvl) {
		$('#account_module #earnings').createAppend('div', {
			id: "kitchen_upgrade_notice"
		}, ['h4', {}, 'Upgrade Your Kitchen!', 'p', {}, 'You\'ve earned an upgrade to your kitchen! <a href="/kitchen_upgrade.php">Click here to see your options.</a></p>', 'p', {
			id: "hide_kitchen_upgrade"
		}, '<a href="javascript:void(0)" onclick="hide_kitchen_upgrade_notice(' + lvl + ')">[ hide this ]</a></p>', ]);

		if (notice_shown == "true") {
			$("#kitchen_upgrade_notice").show();
		}else{
			$("#kitchen_upgrade_notice").slideDown("slow");
		}
	}
 	$.cookie("mdpr_hide_kitchen_upgrade_notice_shown", "true", { path: '/', expires: 90 });
}

var view_kitchen_upgrade_options = function(){
		
}

var hide_kitchen_upgrade_notice = function(lvl){
	$("#kitchen_upgrade_notice").slideUp("fast", function(){
	});
	$.cookie("mdpr_hide_kitchen_upgrade_notice", lvl, {
		path: '/',
		expires: 90
	});
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function randomLetter()
{
   return String.fromCharCode(97 + Math.round(Math.random() * 25));
}
