/*
 * object holding all states of index page
 */
function VIET_pq_ui_Index() {
    this.arTabs = {
        Home: {
            bInited: false, 
            sUrl: 'home.php',
            iIndiPos_vn: 30, // position of menu indication
            iIndiPos_en: 15
        },
        Search: {
            bInited: false, 
            sUrl: 'list.php?filter_type=search',
            sLastKeyword: '',
            iLastPage: 1,
            iIndiPos_vn: 565,
            iIndiPos_en: 565
        },
        Category: {
            bInited: false, 
            sUrl: 'cat.php',
            iIndiPos_vn: 120,
            iIndiPos_en: 90
        },
        All: {
            bInited: false,
            sUrl: 'list.php',
            iIndiPos_vn: 210,
            iIndiPos_en: 180
        }
    };
    this.sCurrTab = 'Home';
}

VIET_pq_ui_Index.prototype = {

    init: function() {

        if( (typeof startWith == 'undefined') || (startWith!='player' && this.arTabs[startWith]==null) )
            startWith = 'Home'; // default

        oPlayer.init();

        if( startWith=='player' && $_COOKIES('curm')!=null ) {
            this.show(startWith);
            oPlayer.PlayVideo(parseInt($_COOKIES('curm'), 10), parseInt($_COOKIES('curc'), 10)); // play last video
        }
        else
            this.show(startWith);

        /*if( typeof startWith != 'undefined' && this.arTabs[startWith]!=null ) {
            
            this.arTabs[startWith].bInited = true;
            this.sCurrTab = startWith;
        }*/

        /*if( BrowserDetect.browser=='Explorer' ) {
            $('browserDetect').innerHTML = 'Some movies might not work due to security settings in Internet Explorer (we are fixing it). Please download and use <a class="footerlk" href="http://www.mozilla.com/en-US/firefox/landing/better/?utm_content=0705features&utm_source=google&utm_medium=ppc&utm_campaign=0507lndgpgtest&gclid=COG0wZu3_5ECFQwcawod4EvF-A">Fire Fox</a>.';
        }*/

        this.onWindowResize();
    },
    
    onWindowResize: function() {
        if( typeof document.documentElement != 'undefined' )
            $('footer').style.top = Math.max(document.documentElement.clientHeight-19, 540) + 'px';
    },

    show: function(what) {
        this.hideTabs();
        if( what=='player' ) {
            oPlayer.show();
        }
        else if (this.arTabs[what]!=null) {

            oPlayer.hide();

            if( what=='Search' ) {
                $('fr' + what).src = this.arTabs[what].sUrl + '&filter_value=' + $('searchField').value;
            }
            else if( what=='Category' ) {
                // alway reload
                $('fr' + what).src = this.arTabs[what].sUrl;
            }
            else if( !this.arTabs[what].bInited ) {
            
                //$('loading' + sTabName).style.display = 'block';
                $('fr' + what).src = this.arTabs[what].sUrl;

            }

            $('fr' + what).style.top = '64px';
            $('fr' + what).style.visibility = 'visible';

            this.arTabs[what].bInited = true;
            this.sCurrTab = what;
        }

        this.adjustMenuIndicator(what);
    },
    
    hideTabs: function() {
        for( var s in this.arTabs ) {
            $('fr' + s).style.top = '-1000px';
            $('fr' + s).style.visibility = 'hidden';
        }
    },

    adjustMenuIndicator: function(activeMenu) {
        if( activeMenu==null )
            activeMenu = '';

        $('menu_indicator_img').style.left = '-50px';
        for( var m in this.arTabs ) {

            if( m==activeMenu ) {
                $('menu_indicator_img').style.left = this.arTabs[activeMenu]['iIndiPos_' + lang] + 'px';
            }

            if( $('imgMenu' + m)!=null )
                $('imgMenu' + m).src = '/images/menu_' + m + '_' + lang + (m==activeMenu ? '_A' : '') + '.gif';
        }
    },    
    
    //
    // search interface event handling
    //
    searchField_onfocus: function(field) {
        if( field.value == "enter keyword")
            field.value = "";
    },
    
    searchField_onblur: function(field) {
        if(field.value == "")
            field.value = "enter keyword";
    },
    
    searchButton_onclick: function() {
        if( $('searchField').value=="enter keyword" )
            $('searchField').value = '';
        return true;
    },
    
    switchLanguage: function() {
        lang = (lang=='vn' ? 'en' : 'vn');
        setCookie('lang', lang, 30);
        document.location.href = '/';
    }
}
var oIndex = new VIET_pq_ui_Index();


//--------------------------------------------------------------------------------------
// for communication with the iframes in index page
//--------------------------------------------------------------------------------------

function onPlayRequest(iVideoId) {
    oIndex.show('player');
    oPlayer.PlayVideo(iVideoId);
}

function showTab(what) {
    oIndex.show(what);
}
