
Util.namespace('Holiday2011.View.Home', function ()
{
    var init = function () {
		$("#instagramSignIn").click(function(){
            var client_id = "3d27065c7f4a404aa057dfb8889f6861";
            var inst = InstagramClient(client_id);
            inst.authenticate('/Card/Instagram/', function(){
			});
		});
	    var app_id = "188551017890681";
        var perms = "user_photos";
        var fb = FacebookClient(app_id);
        fb.init();
		fb.checkLoginStatus(
			function(user_id){ // logged in
				$("#fbSignIn").click(function(){
					location.href = getDomain() + '/Card/FB';
				});
			},
			function() { // not logged in
				$("#fbSignIn").click(function(){
					fb.login('/Card/FB', perms, function(userId){
					});
				});
			}
		);
    };
    return {
        'init': init
    };
});

Util.namespace('Holiday2011.View.FacebookCard', function ()
{
	var cardBuilder;
    var init = function () {
		cardBuilder = $("#card-builder");
		Holiday2011.Components.FacebookCardBuilder.init(cardBuilder, {});
    };
    return {
        'init': init
    };
});

Util.namespace('Holiday2011.View.InstagramCard', function()
{
	var cardBuilder;
	var init = function() {
		cardBuilder = $("#card-builder");
		Holiday2011.Components.InstagramCardBuilder.init(cardBuilder, {});
	};
	return {
		'init': init
	};
});

Util.namespace('Holiday2011.View.Submit', function()
{
	var init = function() {
		$("#submit-button").click(function(){
			submitForm();
		});
		// submit form by pressing enter in the verification code box
		$("#code").keydown(function(){
			if (event.keyCode == 13) { // if enter was pressed
				submitForm();
			}
		});
		// submit form by pressing enter on the submit button
		$("#submit-button").keydown(function(){
			if (event.keyCode == 13) { // if enter was pressed
				submitForm();
			}
		});
		// set input focus to the name field by default
		$("#name").focus();
		// initialize the share link
		var share = $("#share");
		var shareImagePath = $("#share-image-path");
		// console.log("Parsing " + shareImagePath.attr("href"));
		var paths = shareImagePath.attr("href").split("/");
		var gid = paths[paths.length-2]; // second to last contains the gid folder
		// console.log(paths);
		// console.log("Got " + gid);
		var showUrl = getDomain() + "/Card/Show/" + gid;
		share.attr("href", "http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(showUrl));
		share.click(function () {
		    window.open($(this).attr("href"), "fbsharewindow", "status = 1, height = 360, width = 500, resizable = 0");
		    return false;
		});
	};
	
	function submitForm(){
		var input = getPrintRequest();
		// post the data via JSON
		$.post("/Card/Print", input, function(r){
			// get the result
			var response = $.parseJSON(r);
			// console.log("Got POST result");
			// console.log(response);
			if(response.success) {
				// advance to thanks page
				window.location = getDomain() + "/Card/Thanks/" + response.data.id;
			}
			else {
				// display error messages
				if(!response.codeValid) {
					$("#code-error").text(response.message);
					$("#address-error").text("");
				}
				else {
					$("#code-error").text("");
					$("#address-error").text(response.message);
				}
			}
		});
	}
	
	function getPrintRequest(){
		var name = $("#name").val();
		var street1 = $("#street1").val();
		var street2 = $("#street2").val();
		var city = $("#city").val();
		var state = $("#state").val();
		var zip = $("#zip").val();
		var code = $("#code").val();
		var path = $("#path").val();
		return { 
			Name : name,
			Street: street1,
			Street2: street2,
			City: city,
			State: state,
			Zip: zip,
			Code: code,
			Path: path
		};
	}
	
	return {
		'init': init
	};
});

Util.namespace('Holiday2011.View.Thanks', function () {
    var init = function () {
        // initialize the share link
        var share = $("#share");
        var shareImagePath = $("#share-image-path");
        // console.log("Parsing " + shareImagePath.attr("href"));
        var paths = shareImagePath.attr("href").split("/");
        var gid = paths[paths.length - 2]; // second to last contains the gid folder
        // console.log(paths);
        // console.log("Got " + gid);
        var showUrl = getDomain() + "/Card/Show/" + gid;
        share.attr("href", "http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(showUrl));
        share.click(function () {
            window.open($(this).attr("href"), "fbsharewindow", "status = 1, height = 360, width = 500, resizable = 0");
            return false;
        });
    };
    return {
        'init': init
    };
});

Util.namespace('Holiday2011.View.Show', function () {
    var init = function () {
        // initialize the share link
        var share = $("#share");
        var shareImagePath = $("#share-image-path");
        // console.log("Parsing " + shareImagePath.attr("href"));
        var paths = shareImagePath.attr("href").split("/");
        var gid = paths[paths.length - 2]; // second to last contains the gid folder
        // console.log(paths);
        // console.log("Got " + gid);
        var showUrl = getDomain() + "/Card/Show/" + gid;
        share.attr("href", "http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(showUrl));
        share.click(function () {
            window.open($(this).attr("href"), "fbsharewindow", "status = 1, height = 360, width = 500, resizable = 0");
            return false;
        });
    };
    return {
        'init': init
    };
});

