function init_zeta()
{
    bg_img_attr = {
        natural_width: 1600,
		natural_height: 1080
    };
    
	$(window).resize(bg_crop);
	bg_crop();
	
	$('.vAlign').VerticalAlign();
	$(".subNavi>div>ul>li:not(.active)>a").each(function(){
	        $(this).blend();
	    });
	$(".subNavi>div>ul>li.active>ul>li>a").each(function(){
	        $(this).blend();
	    });
    $(".subNavi>div>ul>li.active>ul>li>ul>li>a").each(function(){
            $(this).blend();
        });
	$(".tabNavi>ul>li:not(.active)>a").each(function(){
	    $(this).blend();
	});
	$(".topNavi>ul>li:not(.active)>a").each(function(){
	    $(this).blend();
	});
	$("#controller_hide").each(function(){
	    $(this).toggle(function(){ $(".hide").show(); }, function() { $(".hide").hide(); });
	});
	
	$(".printLink").each(function(){
	    $(this).click(function(){
	        window.print();
	        return false;
	    });
	});
	$("table.register #change_password").click(function(){
	    if ($(this).attr("checked"))
	    {
	        $("table.register tr.hide").each(function(){
	            $(this).show();
	        });
	    }
	    else
	    {
	        $("table.register tr.hide").each(function(){
	            $(this).hide();
	        });
	    }
	});

	$(".recipePush").each(function() {
	    $(this).hover(
	    	    function() {
	    	        $(this).find("img.delete").fadeIn("fast");
	    	    },
	    	    function() {
	    	        $(this).find("img.delete").fadeOut("fast");
	    	    }
	    	    );
	});
	$(".productPush").each(function() {
	    $(this).hover(
	    	    function() {
	    	        $(this).find("img.delete").fadeIn("fast");
	    	    },
	    	    function() {
	    	        $(this).find("img.delete").fadeOut("fast");
	    	    }
	    	    );
	});
	
}

function bg_crop()
{
    var img = $(".bgPageImg");
    if (img)
    {
        var window_width = $(window).width();
        var window_height = $(window).height();
        ratio_window = window_height / window_width;
        ratio_img = bg_img_attr.natural_height / bg_img_attr.natural_width;

        if (ratio_window > ratio_img)
        {
            new_width = window_height / ratio_img;
            img.css("height", window_height + "px");
            img.css("width", "auto");
            img.css("left", "-" + parseInt((new_width - window_width)/2) + "px");
        }
        else
        {
            img.css("width", "100%");
            img.css("height", "auto");
            img.css("left", "0");
        }
    }
}

// Calls the Zeta web service.
// method (string) - The name of the method to call.
// args (object) - An object with the arguments to the method. An empty object if there are no arguments.
// success (function) - The function to call on success.
// error (function) - The function to call on failure.
function call(method, args, success, error) {
	$.ajax({
		type: 'POST',
		contentType: 'application/json; charset=utf-8',
		url: '/Service.asmx/' + method,
		data: JSON.stringify(args),
		dataType: 'json',
		error: error,
		success: function(data) { success(data.d); }
	});
}

function addrecipe(userid, recipeid)
{
    var $this = $(this)
    $.get( location.protocol + '//' + location.hostname + ':' + location.port + "/Recipe.ashx",
    {
        userid: userid,
        recipeid: recipeid
    },
    function(data){
        var width = $(document).width();
        var height = $(document).height();
        var infocontainer = $("#infoContainer");
        var msg = "";
        if (data == "OK")
            msg = "Receptet har lagts till i din samling."
        if (data == "DENIED")
            msg = "Ett okänt fel har uppstått. Vänligen kontakta <a href='mailto:admin@zeta.nu'>admin@zeta.nu</a>.";
        if (data == "ALREADY_EXISTS")
            msg = "Detta receptet finns redan i din samling.";
        infocontainer.html(msg);
        infocontainer.show();
        
        infocontainer.css( { left: (width / 2 - infocontainer.width() / 2) + "px", top: (height / 2 - infocontainer.height() / 2) + "px" } );
        infocontainer.fadeOut(4000);
    });
    return false;
}

function addproduct(userid, productid)
{
    $.get( location.protocol + '//' + location.hostname + ':' + location.port + "/Product.ashx",
    {
        userid: userid,
        productid: productid
    },
    function(data){
        var width = $(document).width();
        var height = $(document).height();
        var infocontainer = $("#infoContainer");
        var msg = "";
        if (data == "OK")
            msg = "Produkten har lagts till i din samling."
        if (data == "DENIED")
            msg = "Ett okänt fel har uppstått. Vänligen kontakta <a href='mailto:admin@zeta.nu'>admin@zeta.nu</a>.";
        if (data == "ALREADY_EXISTS")
            msg = "Denna produkt finns redan i din samling.";
        infocontainer.html(msg);
        infocontainer.show();
        
        infocontainer.css( { left: (width / 2 - infocontainer.width() / 2) + "px", top: (height / 2 - infocontainer.height() / 2) + "px" } );
        infocontainer.fadeOut(4000);
    });
    return false;
}

function deleterecipe(userid, recipeid) {
    if (confirm("Vill du verkligen ta bort detta recept?")) {
        $.get(location.protocol + '//' + location.hostname + ':' + location.port + '/Recipe.ashx',
        {
            userid: userid,
            recipeid: recipeid,
            'delete': true
        },
        function(data){
            if (data == 'OK')
            {
                window.location.reload();
            }
        },
        'text');
    }
}
function deleteproduct(userid, productid) {
    if (confirm("Vill du verkligen ta bort denna produkt?")) {
        $.get(location.protocol + '//' + location.hostname + ':' + location.port + '/Product.ashx',
        {
            userid: userid,
            productid: productid,
            'delete': true
        },
        function(data){
            if (data == 'OK')
            {
                window.location.reload();
            }
        },
        'text');
    }
}

var addthis_config = {
     ui_cobrand: "ZETA",
     ui_offset_top: 15,
     ui_offset_left: -60,
     ui_delay: 200,
     //services_compact: "",
     ui_language: "sv",
     services_exclude: "print"
}

$(function () {
    init_zeta();
});


