JW = {};
JW.app = function()
{
    function getStamp()
    {
        var stamp = new Date();
        return stamp.getTime();
    }
    
    return {
        product_counter : 0,
        init: function()
        {
            JW.app.loadModules();
        },
        
        loadModules: function()
        {
            var dependency_check = $('#jw-pepper').length > 0;
            if (dependency_check) {
                $('head').append('<link type="text/css" href="/jw-pepper/css/custom-theme/jquery-ui-1.7.1.custom.css" rel="stylesheet" />');
                $('head').append('<link type="text/css" href="/jw-pepper/css/jw-pepper.css" rel="stylesheet" />');
                $.getScript("/js/jquery-ui-1.7.1.custom.min.js", function()
                {
                    JW.app.initInfoBox();
                    JW.app.initResultsBox();
                    JW.app.initAjaxFeedback();
                    JW.app.bind();
                });
            }
        },
        
        initAjaxFeedback : function()
        {
            $(".jw-pepper-loading, #jw-pepper-results-dialog-overlay").ajaxStart(function(){
               $(this).show();
            }).ajaxStop(function(){
               $(this).hide();
            });
        },
        
        bind: function()
        {
            $('a.ui-corner-all').hover(function()
            {
                $(this).addClass('ui-state-hover');
            }, 
            function()
            {
                $(this).removeClass('ui-state-hover');
            });
            
            $('#jw-pepper-dialog-link').bind('click', function(e)
            {
                $("#jw-pepper-search").slideToggle("slow");
                $('span', this).toggleClass('ui-icon-plusthick').toggleClass('ui-icon-minusthick');
                return false;
            });
            
            $('.jw-pepper-app-info').live('click', function(e)
            {
                var info = $('#jw-pepper-info-dialog').load("/jw-pepper/jw-pepper-app-info.html?stamp=" + getStamp() + " div#jw-pepper-about", function(){
                    $('#jw-pepper-info-dialog').dialog('open');
                });
                return false;
            });
            
            $('.jw-pepper-results-paginate').live('click', function(e)
            {
                var page = parseInt(this.id, 10).toString();
                $('#jw-pepper-page').val(page);
                JW.app.submitSearch();
                return false;
            });
            
            $('.jw-pepper-search-submit').bind('click', function(e)
            {
                JW.app.submitSearch();
                return false;
            });
            
            $('button.jw-pepper-product-add').live('click', function(e)
            {
                JW.app.product_counter++;
                                
                var product_id = $(this).attr('id');
                var product_name = $(this).attr('title');
                var meta = '<div style="display: none;" id="jw-pepper-product-meta" class="' + product_id + '_added">' + 
                '<span class="product-id">' + product_id + '</span>' +
                '<span class="product-name">' + '' + '</span>' +
                '</div>';
                var input = '<input type="hidden" value="' + product_id + 
                                                '" name="data[Lesson][JwPepperProduct][' + JW.app.product_counter + '][product_id]" class="' + 
                                                product_id + '_added" />';
                input += '<input type="hidden" value="' + product_name + 
                                                '" name="data[Lesson][JwPepperProduct][' + JW.app.product_counter + '][description]" class="' + 
                                                product_id + '_added" />';
                                                
                var link = '<li class="' + product_id + '_added"><span>Sheet music requirement - <a href="http://www.jwpepper.com/' + 
                    product_id + '.item" target="_blank">' + product_name + '</a> [<a href="#" class="jw-pepper-remove-from-list" title="' + product_id + '">X</a>]</span></li>';
                    
                $('#jw-pepper-products-added-hidden').append(input);
                $('#jw-pepper-products-added').append(link);
                $(this).parent().hide().siblings('.added').show();
                return false;
            });
            
            $('.jw-pepper-product-options .added a').live('click', function(e)
            {
                var id = $(this).attr('class');
                $('.' + id + '_added').remove();
                $(this).parent().hide();
                $(this).parent().siblings('.options').show();
                return false;
            });
            
            $('#jw-pepper-products-added a.jw-pepper-remove-from-list').live('click', function(e)
            {
                var id = $(this).attr('title');
                $('.' + id + '_added').remove();
                return false;
            });
        },
        
        submitSearch : function() {
            var search = $('.jw-pepper-search-box').val();
            search = $.trim(search);
            if(search == '') {
                alert('You must enter a search term');
                return false;
            }
            
            $.ajax({
                type: "POST",
                url: "/jw-pepper/lib/jw-pepper-interface.php",
                data: $("#LessonAddForm").serialize(),
                success: function(msg){
                    $('#jw-pepper-results-dialog-content').empty();
                    $('#jw-pepper-results-dialog-content').append(msg);                         
                    if(!$('#jw-pepper-results-dialog').dialog('isOpen')) {
                        $('#jw-pepper-results-dialog').dialog('open');
                    }                             
                    $("#jw-pepper-results-table tbody tr:even").addClass("jw-pepper-result-highlight");
                    $('#jw-pepper-results-table button').each(function(){
                        var id = $(this).attr('id');
                        if($('input[value="' + id + '"]').length > 0) {
                            $(this).parent().hide().siblings('.added').show();
                        }
                    });
                },
                error: function(xhr, textStatus, errorThrown){
                    status = xhr['status'];
                    statusText = xhr['statusText']
                    alert(statusText);
                }
            });
        },
        
        initInfoBox: function()
        {
            // Dialog           
            $('#jw-pepper-info-dialog').dialog({
                autoOpen: false,
                width: 680,
                height: 580,
                buttons: {
                    "Close": function()
                    {
                        $(this).dialog("close");
                    }
                },
                open: function(event, ui)
                {
                    $('#jw-pepper-info-dialog').dialog('moveToTop');
                    if($('#jw-pepper-results-dialog').dialog('isOpen')) {
                        $('#jw-pepper-results-dialog').dialog('disable');
                    }  
                },
                close: function(event, ui)
                {
                    $('#jw-pepper-info-dialog').empty();
                    if($('#jw-pepper-results-dialog').dialog('isOpen')) {
                        $('#jw-pepper-results-dialog').dialog('enable')
                                                      .dialog('moveToTop');
                    }  
                }
            });
        },
        
        initResultsBox: function()
        {
            // Dialog           
            $('#jw-pepper-results-dialog').dialog({
                autoOpen: false,
                width: 750,
                height: 500,
                modal: true,
                overlay: {
                    opacity: 0.5,
                    background: "black"
                },
                buttons: {
                    "Close": function()
                    {
                        $(this).dialog("close");
                    }
                },
                close: function(event, ui)
                {
                    $('#jw-pepper-results-dialog-content').empty();
                    $('#jw-pepper-page').val(1);
                }
            });
        }
    };
    
}();

$(document).ready(function()
{
    JW.app.init();
});
