/**
 * promotionspage extends page.
 *
 * @author geraldyeo
 */
var promotionsPage = function(opts){
    var that = htmlPage(opts), super_init = that.init;
    
    /*function updateTips(t){
        $("#validateTips").text(t);
    }*/
    
    /*function checkEmpty(o, n){
        var trimmed = $.trim(o.val());
        if (trimmed.length < 1 || trimmed === o.context.defaultValue) {
            o.addClass('ui-state-error');
            updateTips(n);
            return true;
        }
        return false;
    }*/
    
    /*function checkLength(o, n, min, max){
        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n.toUpperCase() + " must be between " + min + " and " + max + ".");
            return false;
        }
        return true;
    }*/
    
    /*function checkRegexp(o, regexp, n){
        if (!(regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        }
        return true;
    }*/
    
    /*function validate($input){
        var name = $input.attr('name');
        var rule = that.rules[name];
        var msg = that.messages;
        var label = $.trim($input.parent().text());
        //
        if (!rule.required) {
            return true;
        }
        // check empty
        if (rule.required && checkEmpty($input, label.replace(/:/, msg.required))) {
            return false;
        }
        // check against rule
        if (rule.regexp) {
            return checkRegexp($input, rule.regexp, msg[name]);
        }
        else 
            if (rule.rangeLength) {
                return checkLength($input, label.replace(/:/, ""), rule.rangeLength[0], rule.rangeLength[1]);
            }
    }*/
    
    /*function checkCookie(target, fadeType){
        if (fadeType === 'in') {
            if ($.cookie('cathay_promoreg_cookie') === 'registered') {
                $.cookie('cathay_promoreg_cookie', null); // testing only.
                return;
            }
            // exclusive promo, needs email reg
            if ($(target).hasClass("register")) {
                var $content = $("#promomodal-content");
                var contentHtml = $content.html();
                $content.load("/content/dialog-emailreg.html", function(data){
                    prepForm($content, contentHtml);
                });
            }
        }
    }*/
    
    /*function prepDetails($content, content){
        $("#email-dialog").remove();
        $content.html(content);
    }*/
    
    /*function prepForm($content, prevContent){
        $content.find("#loader").css({
            position: "absolute",
            right: "0"
        }).hide();
        
        // init text
        $content.find("input:text").each(function(i, input){
            var $input = $(input);
            $input.focus(function(evt){
                $input.removeClass('ui-state-error');
                if (this.value === this.defaultValue) {
                    this.value = '';
                }
                else {
                    this.select();
                }
            }).blur(function(evt){
                if ($.trim(this.value) === '') {
                    this.value = this.defaultValue || '';
                }
            });
        });
        // submit 
        $content.find("input:button").click(function(evt){
            evt.preventDefault();
            //
            var valid = true;
            $content.find("input:text").each(function(i, input){
                var $input = $(input);
                valid = valid && validate($input);
                return valid;
            });
            //
            if (valid) {
                 //$content.find("#loader").show();
                 //var emailStr = $content.find("input#email").val();
                 //var dataString = 'email=' + emailStr;
                 //ajax submit
                 //$.ajax({
                 //type: "POST",
                 //url: "bin/process.php",
                 //data: dataString,
                 //error: delegate.create(this, onSubmitError, $content),
                 //success: delegate.create(this, onSubmitSuccess, $content)
                 //});
                //// test
                onSubmitSuccess($content, prevContent);
            }
        });
    }*/
    
    /*function onSubmitError($content){
        $content.find("#loader").hide();
        updateTips(that.messages.submitError);
    }*/
    
    /*function onSubmitSuccess($content, contentHtml){
        $content.find("#loader").hide();
        // cookie expires in 2 days
        $.cookie('cathay_promoreg_cookie', 'registered', {
            expires: 2
        });
        // fade out form and show content
        $content.fadeOut(200, function(){
            $content.find("div.dialog").remove().end().html(contentHtml).show();
        });
    }*/
    
    function setExtLink(target, type) {
        $("#promomodal-content").find("a[rel$=external]").click(function(){
            this.target = "_blank";
        });
    }
    
    function loadPromoDetails(pid){
        var $promo = $("#promo-listing dd.keyart a[href$=" + pid + "]");
        
        if ($promo.length === 1) {
            $promo.click();
        }
    }
    
    function loadHandler(type){
        $("#" + type + " div.scrollable").scrollable({
            clickable: false,
            items: '#promo-listing',
            size: 5,
            speed: 800
        });
        
        // add events
        $("a[class*=promomodal]").modal({
            name: "promomodal-",
            target: "#main_content",
            height: "505px",
            width: "918px",
            pathToCloseBtn: "/cineplexes/media/images/close_ie6.png",
            onFadeStart: setExtLink
        });
        
        // if there's a specified promo, open it
        var pid = (typeof that.parsedUrl.param === 'function') ? that.parsedUrl.param("pid") : undefined;
        if (pid) {
            that.parsedUrl = ""; // load once
            delegate.callLater(500, this, loadPromoDetails, pid);
        }
    }
    
    /*that.rules = {
        email: {
            required: true,
            regexp: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
        }
    }*/
    
    /*that.messages = {
        required: " is required",
        email: "Please check your email.",
        submit: "Submitting...",
        submitSuccess: "Success!",
        submitError: "Error submitting form. Please try again later."
    };*/
    
    // override public init
    var init = function(){
        super_init.apply(that);
        var tabNumbers = {
            "box-office": 1,
            "fnb": 2
        };
        
        $("#tabs").simpletabs({
            selected: tabNumbers[that.parsedUrl.param("type")] || "1",
            onLoaded: loadHandler
        });
    };
    that.init = init;
    
    return that;
};
