$(document).ready(function() {
    Rebind();
});
function Rebind(){
    preLoadLoader();
    logout();
    createCoffeeBox();
    closeCoffeeBox();
    BoxtoBox();
    $('div.msgbox').draggable();
}
function preLoadLoader(){
    $.ajax({
        type: "GET",
        url: '/images/RR.gif',
        async: false
    });
    $.ajax({
        type: "GET",
        url: '/images/deleteLoading.gif',
        async: false
    });
}

function closeCoffeeBox(){
    $("a.close").live("click", function(){
        $("#coffeebox").fadeOut('fast').$(".msgboxcontent").html("");
       
        return false;
    });

//    $("#coffeebox").live("click", function(){
//        $("#coffeebox").fadeOut('fast').$(".msgboxcontent").html("");
//        return false;
//    });

//    $('.msgbox').live("click",function(){
//        return false;
//    });

    $(document).keyup(function(e){
        if(e.keyCode == 27){
            $("#coffeebox").fadeOut('fast').$(".msgboxcontent").html("");
            return false;
        }
        
    });
}
function createCoffeeBox(){
    $("a.coffee").live("click", function(){
        var url = this.href;
        $(".msgboxcontent").empty();
        $(".contentloading").html(" <div class=\"loading\"><img src=\"/images/RR.gif\" alt=\"loading\" /> </div>").fadeIn();
        $('#coffeebox').css("height", $(document).height());        
        $("#coffeebox").fadeIn();
        centerCoffeeBox();
        loadContent(url);
        $('div.msgboxcontent').css("height","inherit");
        $('div.msgboxcontent').css("width","inherit");
        $("#coffeebox").focus();
        return false;
    });

    $('a[rel*=quickCuppa]').live("click", function(){
        var html  = $(this.name).html();
        $('#coffeebox').css("height", $(document).height());
        $('div.msgboxcontent').html(html);
        $("#coffeebox").fadeIn();
        $('div.msgboxcontent').fadeIn();
        $('div.msgboxcontent').css("height",$(this.name).height());
        $('div.msgboxcontent').css("width",$(this.name).width());
        centerCoffeeBox();
        return false;
    });

}
function showMessage(div){
    var html  = $(div).html();
    $('#coffeebox').css("height", $(document).height());
    $('div.msgboxcontent').html(html);
    $("#coffeebox").fadeIn();
    $('div.msgboxcontent').fadeIn();
    $('div.msgboxcontent').css("height",$(div).height());
    $('div.msgboxcontent').css("width",$(div).width());

    centerCoffeeBox();
}
function loadContent(url) {
    $.ajax({
        type: "GET",
        url: url,

        success: function(data){
            $('div.msgboxcontent').hide().html(data);
            $('.msgboxcontent').find('img').each(function() {
                $.ajax({
                    type: "GET",
                    url: $(this).attr('src'),
                    async: false
                });
            });                
        },

        error: function(){
            $(".msgbox").load("/ajaxErrorPage.html");
        },

        complete: function(){
            $('div.msgboxcontent').fadeIn();
            $(".contentloading").hide();
            centerCoffeeBox();
        //Rebind();
        }

    });
}

function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}

function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowHeight = document.body.clientHeight;
    }
    return windowHeight
}

function centerCoffeeBox(){
    $('.msgbox').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	($(window).width()/2)-(($(".msgbox").width())/2)+160
    });
}

function centerCoffeeBoxLoading(){
    $('.msgbox').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	($(window).width()/2)-90
    }).show();
}

function logout(){
    
    $('a.logMeOut').live("click", function(){
        $(this).html("<img src=\"/images/deleteLoading.gif\" alt=\"delete\" width=15 height=15 />");
        $.ajax({
            type: "GET",
            url: "/pages/user/logoutUser.php",
            dataType: 'html',

            success: function(html){
                $('div#header-login').html(html);
            },

            error: function(){
                $('div#header-login').load("/ajaxErrorPage.html");
            },

            complete: function(){
            }
        });
        return false;
    });
}

function UpdateCornerCart(){
    $('.cartnum').html("<img src=\"/images/deleteLoading.gif\" alt=\"delete\" width=24 height=24 />");
    $.ajax({
        type: "GET",
        url: '/pages/user/ajaxuserMenu.php',
        dataType: 'html',

        success: function(html){
            $('div#header-login').html(html);
        },

        error: function(){
            $('div#header-login').load("/ajaxErrorPage.html");
        },

        complete: function(){
        //Rebind();
        }
    });
    return false;
}

function BoxtoBox(){
    $('a.box-link').live("click", function(){
        $(".msgboxcontent").html(" <div class=\"loading\"><img src=\"/images/RR.gif\" alt=\"loading\" /> </div>").fadeIn();
        centerCoffeeBox();
        var url = this.href;
        loadContent(url);
        return false;
    });
}
