/**
 * base page class. depends on jquery.
 *
 * @author geraldyeo
 */
var htmlPage = function(opts){
    var that = {}, defaults = {
        nav: (opts.nav || "#nav"),
        prefix: (opts.prefix || "current-")
    };
    
    // private methods
    var generateNavSprites = function(){
        // remove css applied nav background to <a>, except
        // the current one.
        $(defaults.nav).children("li").each(function(){
            var liId = $(this).attr("id");
            var current = defaults.prefix + liId;
            var navClass = $(defaults.nav).attr("class");
            attachNavEvents(liId, navClass);
            
            if (navClass !== current) {
                $(this).children("a").css({
                    backgroundImage: "none"
                });
            }
        });
    };
    
    var attachNavEvents = function(name, navClass){
        // don't attach events for current page
        if (navClass === defaults.prefix + name) {
            return;
        }
        
        // fade effects for ff/safari
        $(defaults.nav + " #" + name).hover(function(e){
            // mouse over: create pseudo-link
            $(this).find("a").prepend('<div class="nav-' + name + '"></div>');
            $("div.nav-" + name).css({
                opacity: 0
            }).stop().animate({
                opacity: 1
            }, 400);
        }, function(e){
            // mouse out
            $("div.nav-" + name).stop().animate({
                opacity: 0
            }, 400, function(){
                // remove pseudo-link
                $(this).remove();
            });
        });
    };
    
    var loadWidgets = function(){
        $.getJSON(that.widgetsJsonPath, function(data){
            $("div.widget").each(function(i, item){
                if (data.widgets[i].type === 'swf') {
                    var flashvars = {
                        jsonPath: data.widgets[i].config
                    };
                    var params = {
                        base: ".",
                        allowscriptaccess: "always"
                    };
                    var attributes = {
                        id: data.widgets[i].id
                    };
                    
                    if (swfobject) {
                        swfobject.embedSWF(data.widgets[i].content, data.widgets[i].id, "298", "253", "9.0.0", that.expressInstallPath, flashvars, params, attributes);
                    }
                }
                else {
                    $(item).html(data.widgets[i].content);
                }
            });
        });
    };
    
    // public methods
    that.parsedUrl = "";
    that.widgetsJsonPath = "/cineplexes/content/json/widgets.json";
    that.expressInstallPath = "/cineplexes/media/swf/expressInstall.swf";
    
    var init = function(){
        if ($.url) {
            that.parsedUrl = $.url;
        }
        
        if ($.support.opacity) {
            generateNavSprites();
        }
        
        // set external link target to "_blank"
        $("a[rel$=external]").click(function(){
            this.target = "_blank";
        });
        
        // disable right click
        $(document).bind("contextmenu", function(evt){
            //evt.preventDefault();
        });
        
        $.ajaxSetup({
            // Disable caching of AJAX responses */
            cache: false
        });
        
        loadWidgets();
    };
    that.init = init;
    
    var load = function(path){
        alert('override this function');
    };
    that.load = load;
    
    // return ref
    return that;
};
