/* Js to have global header works */
/** Need to point to production URL **/
var lcc = window.document.URL;
var kd_url = "http://www.kitchendaily.com";

if(lcc.indexOf("kitchendailydev") >0 ){
	 kd_url = "http://kitchendailydev.web.aol.com";
}
if (lcc.indexOf("kitchendailyqa") >0) {
kd_url = "http://kitchendailyqa.web.aol.com";
}
if(lcc.indexOf("kitchendailylocal") >0) {
 kd_url = "http://kitchendailylocal.web.aol.com:8080";
}
if(lcc.indexOf("sandbox.kitchendaily") >0) {
 kd_url = "http://"+window.location.hostname;
}


function getrecipes() {
var lccu = window.location.href;
var callfunc = 0;
var gfeatur = "";
if(lccu.indexOf("my-recipes") >0 || lccu.indexOf("grocery-list") >0) {
    callfunc = 1;
}
if(lccu.indexOf("my-recipes") >0){gfeature = "recipe_box"; }
if(lccu.indexOf("grocery-list") >0){gfeature = "mylist"; }
if($("#my-tools").length >0 && callfunc =="1") {

var ttokentest = $("#token").val();
if(ttokentest.length <=0) {

var signi ="<a href='javascript:kd.kdauthpopup(\"rauthpopup\",\"\");' title='Signin' class='confirm-btn'>Sign In</a>";

if(gfeature == "recipe_box") { var rdefault='<h2 class="title">Sign in to access your Recipe Box</h2><div id="myrecipes-signin">'+signi+'<div id="cntval"></div><div id="openAuthIframeMiniComment"></div>';
$("#zipiframe").html('').hide(); $("#MR_default").html(rdefault).show();
}
if(gfeature == "mylist") { var rdefault='<h2 class="title">Sign in to access your Grocery List</h2><div id="myrecipes-signin">'+signi+'<div id="cntval"></div><div id="openAuthIframeMiniComment"></div>';
$("#zipiframe").hide(); $("#MR_default").addClass('gList').html(rdefault).show();
}
		if($("#my-grocery-list").length>0){
			$("#my-grocery-list").hide();
		}
}

if(ttokentest.length >0) {
var href1 = window.location.href;
$.ajax({
    url: "/kd-proxy/?feature="+gfeature+"&referer="+href1,
    type: "POST",
    data: $("#authform").serialize(),
    success: function(data){
     //var geind = data.indexOf("<img");
     //data = data.substr(0, geind);
     //if(data.length >0) { 
	    $("#MR_default").html('').hide(); $("#zipiframe").html('<iframe src ="'+data+'" width="100%" height="550px" SCROLLING="NO" frameborder="0"></iframe>').show(); 	 
	 //}        
   }
 });
 if($("#my-grocery-list").length>0){
	 $("#my-grocery-list").show();
 }
}
}
}
var kd = {

     kdauthpopup: function(divid,pparam) { var dividup = "#"+divid; $(document).ready(function(){
							$(dividup).multiAuth({tabs:['aol', 'aim', 'facebook', 'google', 'yahoo', 'twitter'],
							devId: 'as1x2n_aVsWSeQ5K',
							getTokenCallback: function(json){
									var $elem = $(this.authLink),
									response = json.response
							},
						  successUrl: kd_url+'/controller/authReceiver/authReceiver.html?source='+pparam
							});
							$(dividup).click();
   						});
   						
   				}
					   			
};


//Multiauth Binds

(function($) {
	$(document).ready(function(){
		var siteDevId = 'as1x2n_aVsWSeQ5K';
		var authTabs = ['aol', 'aim', 'facebook', 'google', 'yahoo', 'twitter'];

		$('#aol-header-login').multiAuth({
		    devId: siteDevId,
		    tabs: authTabs,
		    getTokenCallback: function(json){	
				var $elem = $(this.authLink),response = json.response;
				if (response.statusCode === 200) {
				
					userAttr = response.data.userData.attributes;
				    	$elem.html('<span id="helloUser">Signed in: </span><span id="username">' + userAttr.displayName + '  </span><a id="logoutLink" class="logoutlink" href="#">Sign out</a>').attr('title','Sign out');
					$("#token").val(response.data.token.a);
					$("#user_signin").val(userAttr.displayName);
					$("#udisplayname").val(userAttr.displayName);
				} else {
				    	$elem.html('Sign In / Register').attr('title','Sign In / Register');
					$("#token").val('');
					$("#user_signin").val('');
					$("#udisplayname").val('');
				}
                                var wincheck = window.location.href;
         			if(wincheck.indexOf("/my-tools")> 0) { getrecipes();}
			}
		});
	});
}(jQuery));
