﻿if (self != top) { top.location = self.location; }
var $D = YAHOO.util.Dom;

YAHOO.widget.Effect = function(el) {
	this.oEl = $D.get(el);
	this.height = parseInt($D.getStyle(this.oEl,'height'));
	this.width = parseInt($D.getStyle(this.oEl,'width'));
};
YAHOO.widget.Effect.prototype.BlindUp = function(iTimer, onComplete) {
	var timer = iTimer || 1;
	this.oEl.style.overflow = 'hidden';
	var blind = new YAHOO.util.Anim(this.oEl, { height: { to:0}, opacity: {to:0} }, timer, YAHOO.util.Easing.easeOut);
	if ( onComplete ) {
		blind.onComplete.subscribe(onComplete);
	}
	blind.animate();
};
YAHOO.widget.Effect.prototype.BlindDown = function(iTimer, onComplete) {
	this.oEl.style.visibility = 'hidden';
	this.oEl.style.overflow = 'hidden';
	this.oEl.style.height = '';
	var height = parseInt($D.getStyle(this.oEl,'height'));
	this.oEl.style.height = '0';
	this.oEl.style.visibility = 'visible';
	var timer = iTimer || 1;
 	var blind = new YAHOO.util.Anim(this.oEl,{height: {to:height, from:0}, opacity: {to:1.0, from:0.0} }, timer, YAHOO.util.Easing.easeOut);
	if ( onComplete ) {
		blind.onComplete.subscribe(onComplete);
	}
	blind.animate();
};
YAHOO.example = function() {
	return {
		init : function() {
			YAHOO.util.Event.on('menu-email','click',this.blinddown,$D.get('send-block'),true);
            YAHOO.util.Event.on('send-block-close', 'click', this.close, $D.get('send-block'),true);			
		},			
		blinddown : function(e) {
		    var block = $D.get('send-block');
			var z = new YAHOO.widget.Effect(block);
			block.style.display = "";
			z.BlindDown();
			$D.get("send-block-send").disabled = false;
			YAHOO.util.Event.stopEvent(e);
		},
		close  : function(e) {
		    var block = $D.get('send-block');
		    var z = new YAHOO.widget.Effect(block);
			z.BlindUp();
			YAHOO.util.Event.stopEvent(e);
			block.style.display = "none";
		}
	};
}();

var send_callback = 
{
    success : function(o) {
        var el_sendblock = $D.get("send-block");
        if (o.responseText == "SUCCESS") {
            $D.get("send-status").innerHTML = "Email sent successfully";
            $D.get("send-block-close").disabled = false;
        }
        else
            alert(o.responseText);
    },
    failure : function(o) {
        $D.get("send-status").innerHTML = "Email is not available right now. Please try later";
        $D.get("send-block-send").disabled = false;
        $D.get("send-block-close").disabled = false;        
    }
}
        
function send_start(o) {
    // sanity test on the form
    var filter = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/i;
    var elem = $D.get("send_toEmail");
    if (!filter.test(elem.value)) {
        $D.setStyle("send_toEmail", "border", "1px solid red" );
        $D.get("send-status").innerHTML = "Recipient mail does not look right";
        return false;
    }
    elem = $D.get("send_fromEmail");
    if (!filter.test(elem.value)) {
        $D.setStyle("send_fromEmail", "border", "1px solid red" );
        $D.get("send-status").innerHTML = "Your mail does not look right";
        return false;
    }
    // hide buttons
    $D.get("send-block-send").disabled = true;
    $D.get("send-block-close").disabled = true;
    $D.get("send-status").innerHTML = "Sending email...";
    YAHOO.util.Connect.setForm($D.get("send-form"));
    var xhrsend = YAHOO.util.Connect.asyncRequest('POST', "xhr-send.aspx", send_callback );
    
}

YAHOO.util.Event.on('send-block-send', 'click', send_start, $D.get('send-block'),true);

function showitem(id) {
    window.open("/reference/details.aspx?id="+id,"item");
}


/* vote on recipe */
var vote_success = function(o) {
    var el_votecount = $D.get("vote-count") ;
    var votecount = (el_votecount.innerText) ? el_votecount.innerText : el_votecount.textContent ;
    el_votecount.innerHTML = 1 + (votecount*1) ;
    
    var el_voteaction = $D.get("vote-action") ;
    el_voteaction.innerHTML = "<span>Voted</span>" ;
}
var vote_complete = 
{ 
    success: vote_success, 
    failure: function(o) {alert(o.statusText); }
}
function login() {
    document.location = "/users/k_signin.aspx?returnurl=" + encodeURI(document.location);
}
function vote(rid, uid) {
    if (uid == 0) { login(); return; }
    var post = "rid=" + rid ;
    var xhrvote = YAHOO.util.Connect.asyncRequest('POST', "xhr-vote.aspx", vote_complete, post); 
}
var favorite_success = function(o) {
    var rsp = o.responseText.replace(/^\s+|\s+$/g,""); 
    if (rsp == "1")
        $D.get("recipe-favorite").innerHTML = "<img src='/inc/icons/btn/fav.png' />" ;
    else if (rsp == "-1")
        $D.get("recipe-favorite").innerHTML = "<img src='/inc/icons/btn/fav-add.png' />" ;
    else if (rsp == "-2")
        alert("You must login to add this recipe to your favorites");
}
var favorite_complete =
{
    success: favorite_success,
    failure: function(o) {alert(o.statusText); }
}
// TODO: uid=0 case should be handled at XHR
function favorite_toggle(rid, uid) {
    if (uid == 0) { login(); return; }
    var post = "rid=" + rid ;
    var xhrfavorite = YAHOO.util.Connect.asyncRequest('POST', "xhr-favorite.aspx", favorite_complete, post);
}

YAHOO.util.Event.onAvailable('doc2', YAHOO.example.init, YAHOO.example, true);
YAHOO.util.Event.onDOMReady(Shadowbox.init);
