
var cookiename = "textboxdata";
var usercard_cookiename = "usercardtext";
var ttl = 1;
function updateTextBoxData (str) {

	storeTextBoxData(str);
}

function log (str) {
	if (console) {
		console.log(str);
	}
}

function storeTextBoxData(value) {
	var expiry=new Date();
	expiry.setDate(expiry.getDate()+ttl);
	document.cookie=cookiename+"=" +value+";expires="+expiry.toGMTString()+";path=/";
}

function extractTextBoxData(){
    thisMovie("cardgadget").exportTextBoxData();
}

function preview(skipFlash) {
	$("#sendStage").attr("src","/images/stage-create.gif");
	if (!skipFlash) {
		thisMovie("cardgadget").preview();
	}
}
function edit(skipFlash) {
	$("#sendStage").attr("src","/images/stage-create.gif");
	if (!skipFlash) {
		thisMovie("cardgadget").edit();
	}
}

function retrieveTextBoxData() {
	
	if (document.cookie.length>0) {
	  
	  c_start=document.cookie.indexOf(cookiename + "=");
	  
	  if (c_start!=-1) { 
	    
	    c_start=c_start + cookiename.length+1; 
   		c_end=document.cookie.indexOf(";",c_start);
	    
		if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	     
	}
	return "";
}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function checkURLForEdit() {
    if (window.location.href) {
        var url = window.location.href;
        if (url.indexOf("#edit") != -1) {
            edit();
        }
    }
     
}

function advanceCardList(obj) {
    
    obj.children(".cardList").children("li").show();
    obj.children(".cardList").children("li:first").appendTo(obj.children(".cardList").get(0));
    obj.children(".cardList").children("li").each(function (index) {
        if (index > 3) {
            $(this).hide();
        } else {
            $(this).show();
        }
    });
    
    return false;

}

function rewindCardList(obj) {
    obj.children(".cardList").children("li:last").prependTo(obj.children(".cardList").get(0));
    obj.children(".cardList").children("li").each(function (index) {
        if (index > 3) {
            $(this).hide();
        } else {
            $(this).show();
        }
    });
    return false;

}

/**
 * Create/show the in-line login form
 */
function doInlineLogin () {
	
	if (!$.browser.msie) {
		$("#inlineLoginMessage").slideUp('fast', function(){
			showLoginFrame();
			$("#inlineLoginMessage").slideDown('slow');
		});
	} else {
		showLoginFrame();
	}
	return false;	
}

function showLoginFrame(){
	$("#inlineLoginMessage").empty();
	$("#inlineLoginMessage").html('<iframe id="il" scrolling="no" allowtransparency="yes" frameborder="no" src="/login/inline"></iframe>');
}


function addCommentForm () {
    var uuid = $('#uuid').val();
	var src = "http://www.omgecards.com/comments/"+uuid+"/"; 
    
	$('#commentForm').html('<iframe id="ic" scrolling="no" allowtransparency="yes" frameborder="no" src="'+src+'"></iframe>');
		
}


function storeUserCardText(value) {

	var expiry=new Date();
	expiry.setDate(expiry.getDate()+ttl);
	document.cookie=usercard_cookiename+"=" +escape(value)+";expires="+expiry.toGMTString()+";path=/";
}

$(document).ready(function () {

	$("#previewStage").click(function () {
		preview();
		return false;
	});
	$("#editStage").click(function () {
		edit();
		return false;
	});
    $("#sendAddressStage").click(function () {
        extractTextBoxData();
    });
    

    checkURLForEdit();
    
    $("#recip_name").focus();
    
    $("skip").css("display","inherit");
    $("#skip-confirm-link").click(function () {
       $("#skip-confirmation").attr("value","1");
       $("#send-card").trigger('submit'); 
       return false;
    });
    
    
    $(".cardListContainer").each(function () {
    
        var container = $(this);
        
        container.children(".cardList").children("li").each(function (index) {
            if (index > 3) {
                $(this).hide();
            } else {
                $(this).show();
            }
        });
    
    
    
        $(this).children(".lo").each(function () {
            
            $(this).children("img.next").wrap('<a href="#"></a>').click(function () { return advanceCardList(container); });
            $(this).children("img.prev").wrap('<a href="#"></a>').click(function () { return rewindCardList(container); });
        
        });
    
    });
    
    $("#previous_recipients").change(function () {
        var t = $(this);
        var extract = /([^\ <]{2,60}) <([^>]*)>/;
        var vals = [];
        vals = t.val().match(extract);
        
        if (vals.length) {
            $("#recip_name").val(vals[1]);
            $("#recip_address").val(vals[2]);
        }
    });
    
    $("#inlineLoginMessage").css("display","block");
	$("#inlineLoginLink")
		.wrap('<a href="#"></a>')
		.click(function(){
			return doInlineLogin();
		});
		
	$("#email_address").focus(function () {
		if ($(this).val() == "email address" || $(this).val() == 'Your email address') {
			$(this).val('');
		}
	});
    $("#password").focus(function () {
        if ($(this).val() == "password") {
            $(this).val('');
        }
    });
	$("#sender_name").focus(function () {
		if ($(this).val() == 'Your name') {
			$(this).val('');
		}
	});
	
	if ($('#commentForm').length && $('#uuid').length) {

		$('#commentForm').append('<a id="addComment" href="#commentForm"><img src="/images/add-a-comment.gif" alt="add a comment" /></a>');
		
		$('#addComment').click(function () {
			addCommentForm();
		});
	 }
	 
	 $('.privateSetting').click(function () {
	 
	 	var uuid = $(this).val();
	 	var checked = ($(this).attr('checked')) ? 0 : 1;
	 	var elem_id = $(this).attr('id');
	 	
	 	$.get("/myaccount/cardstate/"+uuid+"/"+checked, function (data) {
			if (data != 0) {
				
				var result = data.split(";");
				
				if (result[1] == 0) {
					$("#"+elem_id.replace(/checkbox_/, 'item_')).addClass("public");
				} else {
					$("#"+elem_id.replace(/checkbox_/, 'item_')).removeClass("public");
				}
			}
		});
	 
	 });
    
});


