﻿/// <reference path="jquery-1.4.1.js" />

/// <reference path="../Styles/PIE.js" />

/**
 * Oatau Web
 */
$o = new Object();

$o.loadPIE = function () {
    $('.t-tabstrip').each(function () {
        PIE.attach(this);
    });
    $('.rounded_corners').each(function () {
        PIE.attach(this);
    });
    $('.orangeBar').each(function () {
        PIE.attach(this);
    });
    $('body').each(function () {
        PIE.attach(this);
        });
};

$(document).ready(loadLinks);

$(function () {
    $(".vertMenu").buildMenu({

});
});

deleteFromCart = function (id) {
    $.post('/Cart/Remove', { id: id }, function (data) {
        if (data.Result == true) {
            $('tr#cart_' + id).fadeOut('normal', function () {
                $(this).remove();
                cartItemsCount--;

                if (cartItemsCount < 1) {
                    $("#cartContainer").fadeOut();
                }
            });
        }
    }, "json");
}

emptyCart = function () {
    $.post('/Cart/Empty').success(function (data) {
        if (data.Result == true) {
            $("#cartContainer").fadeOut('normal', function () {
                $("#cartTable").empty();
            });
        }
    });
};
 

function loadLinks () {
    $("div.productSummary")
        .each(function (index, element) {
            var link = $(element).children("div.summary").children("span").children("a").attr("href");
            $(element).click(function () {
                window.location = link;
            });
        })
        .hover();

        $(".o-menu-link")
            .each(function (index, element) {
                $(element).click(function ()
                { window.location = $(element).children("a").attr("href"); });
            });
        };

        $(document).ready(loadLinks);

    /**
    * Created by JetBrains PhpStorm.
    * User: Michael
    * Date: 11/12/10
    * Time: 4:40 PM
    * To change this template use File | Settings | File Templates.
    */
    

    $o.isSet = function (v) {
        return (typeof v !== "undefined" && v);
    };

    $o.Cache = function (url, cacheKey) {

        var params = new Object();
        var cache = Array();

        this.setParam = function (key, value) {
            params[key] = value;
        }

        this.setParams = function (p) {
            params = p;
        }

        this.setCacheKey = function (k) {
            params[cacheKey] = k;
        }

        this.clearParam = function () {
            params = Array();
        }

        this.get = function (callback) {
            if ($o.isSet(cache[params[cacheKey]])) {
                callback(cache[params[cacheKey]], null, null);
            } else {
                $.ajax({
                    url: url,
                    dataType: 'json',
                    data: params,
                    success: function (d) {
                        cache[params[cacheKey]] = d;
                        callback(d);
                    }
                });
            }
        }
    };

    $o.Cart = function (c) {

        var cartMenuButton = $('div.o-toolbar li#Cart');

        var cartMenu = $('<div/>', {
            'class': 'cartMenu'
        }).appendTo(cartMenuButton);

        var cartMenuContents = $('<div/>', {
            style: 'padding:15px'
        }).appendTo(cartMenu);

        var cartTable = $('<table></table>', {
            'class': 'cartTable'
        });

        $.each(c, function (v, k) {
            var row = $('<tr/>').attr('id', k.id);
            $('<td/>', { text: k.name }).appendTo(row);
            $('<td/>', { text: k.count }).appendTo(row);
            cartTable.append(row);
        });

        cartMenuContents.append(cartTable);

        cartMenuButton.hover(function () {
            $(this).animate({
                backgroundColor: "#DDD"
            });
            cartMenu.css('opacity', '0');
            cartMenu.css('display', 'block');
            cartMenu.position({
                of: cartMenuButton,
                my: 'right top',
                at: 'right bottom',
                offset: '0 0'
            });

            cartMenu.animate({
                opacity: 1
            }, 100);

        }, function () {
            $(this).animate({
                backgroundColor: "#DADADA"
            });

            cartMenu.animate({
                opacity: 0
            }, 100, function () {
                cartMenu.css('display', 'none');
            });
        });
    };

    $o.Overlay = function (productId) {
        $.getJSON('/Product/SingleJson',
            { productId: productId },
            function (json) {

                var overlay = $('<div/>',
            { 'class': 'o-overlay' })
            .appendTo('body');
                var prodOv = $('<div/>',
            { 'class': 'o-product-left' })
            .appendTo('body').text("Hello World");
                var prodOv = $('<div/>',
            { 'class': 'o-product-left-bg ' })
            .appendTo('body');
                var prodOv = $('<div/>',
            { 'class': 'o-product-right-bg ' })
            .appendTo('body');
                var prodOv = $('<div/>',
            { 'class': 'o-product-right' })
            .appendTo('body').text("Hello World");

            });
    }

    $o.Products = function (b) {
        //Loadup items in the bootstrap
        var listPanel = $('.o-product-list div.o-list-panel');
        for (var i = 0; i < b.Items.length; i++) {
            var obj = b.Items[i];
            var li = jQuery('<li>', {
                text: obj.Name
            }).appendTo(listPanel);
            li.attr('elementId', obj.Id);
        }

        var list = $('.o-product-list');
        var listItems = $('.o-product-list div.o-list-panel li');
        var mainPanel = $('.o-product-list div.main');
        var loader = $('div#loader');

        var active = null;
        var cache = new $o.Cache('/Product/ListJson', 'productId');

        $.getJSON('/Cart/ListJson', null, function () {
            this.cartMenu = new $o.Cart(b);
        });

        loaderHtml = loader.html();

        listItems.hover(function (event) {
            var elementId = $(this).attr('elementId');

            if (active == this) return;

            if (active != null)
                onSwitch.call(active);

            active = this;

            $(this).animate({
                "margin-left": "40px"
            });
            //Load Category
            var loader = jQuery('<img/>',
            {
                'class': 'loading',
                src: '/Content/Images/ajax-loader.gif',
                alt: 'loading'
            });

            mainPanel.append(loader);

            loader.position({
                of: mainPanel,
                my: 'center center',
                at: 'center center',
                offset: '0 0'
            });

            loader.animate({
                opacity: 1
            }, 1000);

            cache.setCacheKey(elementId);

            cache.get(function (d) {
                mainPanel.empty();

                for (var i = 0; i < d.Items.length; i++) {
                    var obj = d.Items[i];
                    var panel = jQuery('<div/>', {
                        'class': 'product',
                        productId : d.Items[i].Id
                    });

                    panel.append(jQuery(
                            '<img/>', {
                                src: '/Content/Images/imageloader.ashx?id=' + obj.ImageId,
                                alt: '<img>'
                            }
                        ));

                    panel.append(jQuery('<div/>', {
                        text: obj.Name
                    }));

                    panel.appendTo(mainPanel);
                }
                $("div.product").click(function () {
                    new $o.Overlay($(this).attr('productId'));
                });
            }
                );
            //Make a request ... on callback...


        }, function () {
        });

        var onSwitch = function () {
            $(this).animate({
                "margin-left": "0px"
            });
        }
    };

