var ListingsPage = function(options){
	var _this = this;
	
	_this.largeIconPath = options.largeIconPath;
	_this.smallIconPath = options.smallIconPath;
	
}

ListingsPage.MAX_LOCATIONS_PER_CATEGORY = 1;

ListingsPage.prototype.initialize = function(categoryGroupID, categories, locations){

    var _this = this;

    $.extend(_this, new CoinksBase());		
    
    _this.startLoading();
    _this.categoryGroupID = categoryGroupID;
    _this.loadCategories(categories);
    _this.reload(locations);
    _this.selectedCategory = null;
    _this.initEvents();
    _this.endLoading();
    
}

ListingsPage.prototype.initEvents = function(){
    var _this = this;

    $('#id_location').click(function(){
        $(this).val("");
    });

    $('#search').click(function(){
        _this.search();
        return false;
    });

    $('#search_form').submit(function(){
        _this.search();
        return false;
    });
 
    $('.sidebar_location').live('click', function(){
        var location_id = $(this).pk();
        var category_id = $(this).parent().parent().pk();
        _this.selectCategory(category_id);
        _this.getLocation(location_id);
        return false;
    });

    $('.category_menu_item').live('click', function(){
       var category_id = $(this).pk();
       _this.selectCategory(category_id);
       return false;
    });

    $('.category_sidebar_item').live('click', function(){
       var category_id = $(this).pk();
       _this.selectCategory(category_id);
       return false;
    });

    $('.open_review').live('click', function(){
       var location_id = $(this).pk();
        $.pk('review', location_id).toggle();
       return false;
    });

    $('.open_tandc').live('click', function(){
        var location_id = $(this).pk();
        $.pk('tandc', location_id).toggle();
        return false;
    });
    
    $('.open_offer').live('click', function(){
        var offer_id = $(this).pk();
        _this.displayOffer(offer_id, function(response){
            $.pk('offer_loading', offer_id).hide();
            _this.renderOffer(offer_id, response);
        });
        return false;
    });
    
    $('.takeup_offer').live('click', function(){
        var offer_id = $(this).pk();
        var signature = $('#signature').val();
        var ns_coinks = $('#ns_coinks').val();
        var params = { signature : signature, ns_coinks : ns_coinks };
        $.pk('offer_loading', offer_id).show();
        $.pk('offer_detail', offer_id).hide();         
        _this.takeupOffer(offer_id, params, function(message, error){
            $.pk('offer_loading', offer_id).hide();
            _this.renderOffer(offer_id, message);
        });
    });
    
    $('.cancel_offer').live('click', function(){
        var offer_id = $(this).pk();
        _this.cancelOffer(offer_id);
    });
    
    
}

ListingsPage.prototype.loadLocations = function(locations){
    var _this = this;
    _this.locations = new Array;
    _this.locationsMap = new Object;
    $.each(locations, function(index, location){
        _this.locations.push(location);
        _this.locationsMap[location.id] = location;
    });
}

ListingsPage.prototype.getLocation = function(location_id){
    var _this = this;
    var location = _this.locationsMap[location_id];

    $('#locations').html(location.main_html).show();
    $('#offer').hide();
    document.location.href = '#locations';
}

ListingsPage.prototype.loadCategories = function(categories){
   var _this = this;
    _this.categoryMap = new Object;
    _this.categories = new Array;
    $.each(categories, function(index, category){
       var catlist = new CategoryListing(_this, category);
        _this.categoryMap[category.id] = catlist;
       _this.categories.push(catlist);
    });
}

ListingsPage.prototype.getCategory = function(categoryID){
    var _this = this;
    if(categoryID){
        return _this.categoryMap[categoryID];
    } else {
        if (_this.selectedCategory){
            return _this.selectedCategory;
        }
        var cat = null;
        $.each(_this.categories, function(index, category){
            if(category.getLocations().length) {
               cat = category;
               return false;
            }
        });
        return cat;
    }
    return _this.categories[0];
}

ListingsPage.prototype.search = function(){

    var _this = this;

    var location = $('#id_location').val();
    if(!location) return;
    var params = { location : location };
    
    var url = "/locations/group/ajax/nomap/" + _this.categoryGroupID + "/";

    _this.startLoading();
    $('#locations').html('');


    var callback = function(response){
        _this.reload(response.locations);
        _this.endLoading();
    }
    
    $.post(url, params, callback, "json");
}

ListingsPage.prototype.renderSidebar = function(){
    var _this = this;
    var html = new Array;
    $.each(_this.categories, function(index, category){
        var item = category.renderSidebarItem();
        if(item){
            html.push(item);
        }
    });
    html = html.join("");
    $('#categories').html(html);
}

ListingsPage.prototype.selectCategory = function(categoryID){
    var _this = this;
    var category = _this.getCategory(categoryID);
    if(category) category.renderLocations();
    _this.selectedCategory = category;
}

ListingsPage.prototype.startLoading = function(){
    $('#locations').hide();
    $('#reviews').hide();
    $('#loading').show();
    $('#offer').hide();
}

ListingsPage.prototype.endLoading = function(){
    $('#loading').hide();
    $('#locations').show();
}

ListingsPage.prototype.reload = function(locations){
    var _this = this;    
    _this.loadLocations(locations);               
    _this.renderSidebar();
    _this.selectCategory();
}

ListingsPage.prototype.cancelOffer = function(offer_id){

    var offer_detail = $.pk('offer_detail', offer_id);
    var bgcolor = "#FFFFFF";
    
    offer_detail.hide();

    var parent = offer_detail.parent();
    
    parent.children('.main_detail').show();
    parent.children('.prize_draw').show();
    
    parent = parent.parent();
    
    parent.children('.offer_button').show();
    parent.css('background-color', bgcolor);

}

ListingsPage.prototype.renderOffer = function(offer_id, response){

    var offer_detail = $.pk('offer_detail', offer_id);
    var bgcolor = "#D2EEFF";
    
    offer_detail.html(response).show();

    var parent = offer_detail.parent();
    
    parent.children('.main_detail').hide();
    parent.children('.prize_draw').hide();
    
    parent = parent.parent();
    
    parent.children('.offer_button').hide();
    parent.css('background-color', bgcolor);

}

var CategoryListing = function(page, data){
    var _this = this;
    $.extend(_this, data);
    _this.page = page;
    _this.locations = null;
    
    /*
    _this.largeIcon = "/media/showtime/images/categories/large/" + _this.slug + ".jpg";
    _this.smallIcon = "/media/showtime/images/categories/small/" + _this.slug + ".gif";
    */

    _this.largeIcon = page.largeIconPath + _this.slug + ".jpg";
    _this.smallIcon = page.smallIconPath + _this.slug + ".gif";
}

CategoryListing.prototype.getLocations = function(){
    var _this = this;

    _this._locations = new Array();
    
    $.each(_this.page.locations, function(index, location){
        if(location.category_id == _this.id){
           _this._locations.push(location);
        }
    });

    return _this._locations;
}

CategoryListing.prototype.renderSidebarItem = function(){
    var _this = this;
    var locations = _this.getLocations();
    
    var html = ['<h3><a href="#" class="category_sidebar_item" id="category_sidebar_item:']

    html.push(_this.id)
    html.push('">')
    html.push(_this.name); 
    html.push('</a></h3><ul id="category_list:');
    html.push(_this.id);
    html.push('">');

    if(!locations || !locations.length){
        html.push('<li>Sorry, there are no shows available for your chosen selection.</li>');
    }

    $.each(locations, function(index, location){
        if(index > (ListingsPage.MAX_LOCATIONS_PER_CATEGORY - 1)) return false;

        var element_id = "location:" + location.id;
        html.push('<li><a class="sidebar_location" href="#" id="');
        html.push(element_id);
        html.push('">');
        html.push(location.name);
        html.push("</a>");
        if(location.distance){
            html.push("&nbsp;" + location.distance + "m");
        }
        html.push("</li>");
    });
    
    
    locations = null;
    
    html.push("</ul>");
    html = html.join("");
    
    return html;
}

CategoryListing.prototype.removeEmptyReviews = function(){
    $.each($('.review'), function(){
        var review = $(this);
        if(review.html().replace(/^[\r\n\s]+|\r\n\s]+$/g,'').length == 0){
            review.parent().children('.review_button').hide();
        }
    });
}

CategoryListing.prototype.renderLocations = function(){
    var _this = this;  
    var icon = '<img src="' + _this.largeIcon + '">';
  
    $('#offer').hide();
    $('#no_locations').hide();
    $('#locations').html('');
    
    $('#selected_category').html(icon).show();

    $('.category_menu_item').show();
 
    var locations = _this.getLocations();
    if(!locations || locations.length == 0){
        $('#locations').hide();
        $('#no_locations').show();
        return;
    };

    var html = new Array();
    
    $.each(locations, function(index, location){
        html.push(location.main_html);
    });

    locations = null;
    html = html.join("");   
    $('#locations').html(html).show();

    _this.removeEmptyReviews();
    
}



