/**
 * moviespage extends page.
 *
 * @author geraldyeo
 */
var moviesPage = function(opts){
    var that = htmlPage(opts), super_init = that.init;
    
    function prepMovieDetails(){
        $("#moviemodal-content").find("a.media").each(function(i, item){
            var $item = $(item);
            var meta = $.metadata.get(item);
            var playerid = 'flvplayer'+i;
            var $flvplayer = $('<div id="'+playerid+'" />'); 
            $item.parent().append($flvplayer);
            $item.remove();
            
            // check for flv
            var vidUri = $item.attr('href');
            vidUri = vidUri.toLowerCase();
            if (vidUri.indexOf('flv') === -1) {
                alert("Media is not an FLV file.");
                return;
            }
            
            var flashvars = {
                flvPath: vidUri,
                autoPlay: meta.autoplay
            };
            var params = {
                base: ".",
                allowscriptaccess: "always"
            };
            var attributes = {
                id: playerid
            };
            
            if (swfobject) {
                swfobject.embedSWF(that.flvPlayerPath, playerid, meta.width, meta.height, "9.0.0", that.expressInstallPath, flashvars, params, attributes);
            }
        });
        
        $("#moviemodal-content").find("table.casts span.exclusive").prepend("<img src='/cineplexes/media/images/exclusive.gif' />");
        
        // booking link open new page
        $(".booking a").click(function(){
            this.target = "_blank";
        });
    }
    
    function loadMovieDetails(mid){
        var $mov = $("#movie-listing a[href$="+mid+"]");
        if ($mov.length === 1){
            $mov.click();
        }
    }
    
    function loadHandler(type){
        switch (type) {
            case "tabs-1":
            case "tabs-3":
			
                initScrollable("#"+type);
                break;
            case "tabs-2":
			case "tabs-4":
        }
        
        prepMovieThumbs();
        // add events
        $("a[class=moviemodal]").modal({
            name: "moviemodal-",
            target: "#main_content",
            height: "505px",
            width: "918px",
            pathToCloseBtn: "/cineplexes/media/images/close_ie6.png",
            onFadeIn: prepMovieDetails
        });
        // if there's a specified movie, open it
        var mid = (typeof that.parsedUrl.param === 'function') ? that.parsedUrl.param("mid") : undefined;
        if (mid) {
            that.parsedUrl = ""; // load once
            delegate.callLater(500, this, loadMovieDetails, mid);
        }
    }
    
    function initScrollable(tabId){
        $(tabId+" div.scrollable").scrollable({
            clickable: false,
            items: '#movie-listing',
            size: 5,
            speed: 800
        });
    }
    
    function setSneakPreviewFrame($item){
        var $parent = $item.parent();
        var bgsrc = $item.attr("src");
        var imgsrc = ($item.is(".exclusive")) ? "/cineplexes/media/images/sneak_exclusive.png" : "/cineplexes/media/images/sneak_preview.png";
        
        if ($parent.is("a")) {
            $item.css({
                margin: "0"
            });
            $parent.css({
                border: "none"
            });
        }
        
        $item.attr({
            src: imgsrc
        });
        
        $item.css({
            background: "url(" + bgsrc + ") no-repeat",
            border: "none"
        });
    }
    
    function prepMovieThumbs(){
        $("dl.movie-thumb").each(function(i, item){
            var $movie = $(item);
            // set sneak preview frame
            $sneak = $movie.find("img.sneak-preview");
            if ($sneak.length > 0) {
                setSneakPreviewFrame($sneak);
            }
            
            // set ellipsis to titles
            $movieTitle = $movie.find("dt.title");
            $movieTitle.text(ellipsis($.trim($movieTitle.text()), 35));
            
            // put star next to title which is exclusive
            if ($movie.find("img").is(".exclusive")) {
                $movieTitle.prepend("<img src='/cineplexes/media/images/exclusive.gif' />");
                $movieTitle.find("img").css({"margin-right":"5px"});
            }
            
            // booking link open new page
            $(".booking a").click(function(){
                this.target = "_blank";
            });
        });
    }
    
    function ellipsis(str, len){
        if (str && str.length > len) {
            return str.substring(0, len - 3) + "...";
        }
        return str;
    }
    
    function updateCalendar(){
        // replace the <a>
        var today = new Date();
        $date = $('<div id="month">'+that.shortmonth[today.getMonth()]+'</div><div id="date">'+today.getDate()+'</div>');
        var url = $("#calendar a").attr("href");
        $("#calendar").html($date).click(function(){
            //window.location = url;
            window.open(url);
        });
    }
    
    // members
    that.flvPlayerPath = '/cineplexes/media/swf/trailerplayer.swf';
    that.expressInstallPath = "/cineplexes/media/swf/expressInstall.swf";
    that.shortmonth = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    
    // override public init
    var init = function(){
        super_init.apply(that);
        var tabNumbers = {"now-showing":1, "top-five":2, "coming-soon":3, "marina-showing":4};
        
        $("#tabs").simpletabs({
            selected: tabNumbers[that.parsedUrl.param("type")] || "1",
            onLoaded: loadHandler
        });
        
		  $("#moviestrip").load('/cineplexes/content/movies-' + $("#moviestrip").attr('rel') + '.html', function(data){ loadHandler.apply(); initScrollable('#moviestrip'); } );	

        updateCalendar();
    };
    that.init = init;
    
    return that;
};


