window.pemco = window.pemco || {};

/*
Creates a widget to view PEMCO commercials
*/

(function($) {

    var pub = pemco.bbq = {};

    var checkHashTime = 100;
    var currHash, lastHash;

    var initState = {};
    initState.userProfiles = {};

    // Manage Load Screen
    pub.loadComplete = (function() {
        // State Variables
        var loadShown = false;

        var currHash = window.location.hash.substr(1).split("/").shift() || 'profiles';

        var fadeOut = function() {
            $('#load_screen').fadeOut(600);
            loadShown = true;
            clearTimeout(timer);
        }

        var timer = setTimeout(fadeOut, 30 * 1000);

        // Hide the load screen
        return function(hash) {
            if (!loadShown && (hash === undefined || currHash == hash)) fadeOut();
        };

    })();

    pub.changeHash = function(newHash, profileName) {
        //		console.log('pub.changeHash(%s)', newHash);
        lastHash = window.location.hash;
        currHash = /#/.test(newHash) ? newHash : '#' + newHash;
        targetDefaultProfile = profileName;
        hashChange();
        window.location.hash = currHash;
        lastHash = false;
        //		console.log('window.location.hash = currHash');
    }

    // Watch for Hash Change
    $(function() {
        var timer, hash;
        (function() {
            clearTimeout(timer);
            hash = window.location.hash;
            if (currHash != hash && lastHash != hash) {
                currHash = hash;
                hashChange();
            }
            timer = setTimeout(arguments.callee, checkHashTime);
        })();
    });

    // Hash Change Event
    var hashChange = function() {
        //		console.log('hashChange()');

        var arrState = currHash.substr(1).split("/");
        var pageName = '';

        if (arrState.length > 0) {
            switch (arrState[0]) {
                /*            
                case 'twitter':
                pemco.tray.menu.selectTwitter();
                pemco.tray.raise();

                    pageName = 'Twitter';

                    break;

                case 'blog':
                pemco.tray.menu.selectBlog();

                    var strId = '';
                var strCurrent = '';
                var strBloggerId = '';
                var strAction = '';

                    pageName = 'Blog';

                    //split the hash
                if (arrState.length == 2) { //#blog/id
                strId = arrState[1];

                        pageName = 'Blog / ' + strId;
                }
                else if (arrState.length == 3) { //#blog/id/current
                strId = arrState[1];
                strCurrent = arrState[2];

                        pageName = 'Blog / ' + strId;
                }
                else if (arrState.length == 4) { //#blog/id/current/blogger_id
                strId = arrState[1];
                strCurrent = arrState[2];
                strBloggerId = arrState[3];

                        pageName = 'Blog / ' + strId;
                }
                else if (arrState.length >= 5) {
                strId = arrState[1];
                strCurrent = arrState[2];
                strBloggerId = arrState[3];
                strAction = arrState[4];

                        pageName = 'Blog / ' + strId;
                }
                    
                pemco.blog.setBbqUrl(strId, strCurrent, strBloggerId, strAction);
                pemco.blog.load();
                pemco.tray.raise();

                    break;

                case 'difference':
                pemco.tray.menu.selectDifference();
                pemco.tray.raise();

                    pageName = 'PEMCO Difference';

                    break;

                case 'polls':
                var strMode = 'polls';
                var strContestHandle = '';
                var strEntryId = '';
                if (arrState.length > 1) {
                strContestHandle = arrState[1];

                        if (arrState.length > 2) {
                strEntryId = arrState[2];
                }
                }

                    pemco.tray.menu.selectContests();
                pemco.contests.setBbqUrl(strMode, strContestHandle, strEntryId);
                pemco.contests.load();
                pemco.tray.raise();
                pageName = 'Contests';

                    break;

                case 'contests':
                var strMode = 'contests';
                var strContestHandle = '';
                var strEntryId = '';
                if (arrState.length > 1) {
                strContestHandle = arrState[1];

                        if (arrState.length > 2) {
                strEntryId = arrState[2];
                }
                }

                    pemco.tray.menu.selectContests();
                pemco.contests.setBbqUrl(strMode, strContestHandle, strEntryId);
                pemco.contests.load();
                pemco.tray.raise();
                pageName = 'Contests';

                    break;
                */ 
                //                case 'january':                      
                //                case 'february':                      
                //                case 'march':                      
                //                case 'april':                      
                //                case 'may':                      
                //                case 'june':                      
                //                case 'july':                      
                //                case 'august':                      
                //                case 'september':                      
                //                case 'october':                      
                //                case 'november':                      
                //                case 'december':                      
                //                    //pemco.tray.menu.selectCalendar();                      

                //                    if (arrState.length > 0) {                      

                //                        switch (arrState[0]) {                      
                //                            case 'january':                      
                //                            case 'february':                      
                //                            case 'march':                      
                //                            case 'april':                      
                //                            case 'may':                      
                //                            case 'june':                      
                //                            case 'july':                      
                //                            case 'august':                      
                //                            case 'september':                      
                //                            case 'october':                      
                //                            case 'november':                      
                //                            case 'december':                      
                //                                // #calendar/month/eid                      
                //                                if (arrState.length > 1) {                      
                //                                    pemco.calendar.loadMonth(arrState[0], arrState[1]);                      
                //                                }                      
                //                                // #calendar/month                      
                //                                else {                      
                //                                    pemco.calendar.loadMonth(arrState[0]);                      
                //                                }                      
                //                                break;                      

                //                            default:                      
                //                                // #calendar/codename                      
                //                                pemco.calendar.setProfile(arrState[0]);                      

                //                                // #calendar/codename/month                      
                //                                if (arrState.length > 3) {                      
                //                                    pemco.calendar.loadMonth(arrState[1], arrState[2]);                      

                //                                }                      
                //                                else if (arrState.length > 1) {                      
                //                                    pemco.calendar.loadMonth(arrState[1]);                      
                //                                }                      
                //                                else {                      
                //                                    pemco.calendar.load();                      
                //                                }                      

                //                                break;                      
                //                        }                      
                //                    }                      

                //                    //pemco.tray.raise();                      

                //                    pageName = 'Calendar';                      

                //                    break;                      

                case 'my_account':
                    pemco.home.myProfilesMode();
                    //pemco.tray.lower();

                    pageName = 'My Account';

                    break;
                case 'userprofiles':
                    var upID = arrState[1] || initState.userProfiles.id;
                    /*
                    if (upID) {
                    pemco.user_carousel.showProfile(upID);
                    }
                    else {
                    pemco.user_carousel.load(function() {
                    initState.userProfiles.id = pemco.user_carousel.getCurrentProfileID();
                    });
                    }
                    */
                    pemco.home.userCarouselMode();

                    if ($('#ProfileCarouselUser').length == 0) {

                        pemco.home.resetCarouselDiv();
                        pemco.home.createUserCarousel();
                    }

                    if (upID != null && upID != "undefined")
                        pageName = 'User Profiles/' + pID;
                    else
                        pageName = 'User Profiles';
                        
                    break;

                case 'createprofile':
                    //Switch to create profile mode

                    var upID = arrState[2];
                    var nw_type_id = 0;

                    if (upID) {
                        try {
                            nw_type_id = parseInt(upID);
                        }
                        catch (err) {
                            nw_type_id = 0;
                        }

                        if (nw_type_id > 0) {
                            pemco.create_profile.editUserProfile('ajax/default.aspx', nw_type_id);
                        }
                    }
                    else {
                        pemco.home.createProfileMode();
                    }
                    //pemco.tray.lower();
                    pageName = 'Create Profile';

                    break;

                case 'profilespotted':
                    if (arrState[2]) {
                        pemco.profile_spotted.setProfile(arrState[2] || false);
                        pemco.profile_spotted.setShow(arrState[1] || false);
                    }
                    else {
                        pemco.profile_spotted.setProfile(arrState[1] || false);
                        pemco.profile_spotted.setShow(false);
                    }
                    pemco.home.profileSpottedMode();
                    //pemco.tray.lower();
                    pageName = 'Profile Spotted';

                    break;

                case 'profiles':
                    pemco.home.carouselMode();
                    var pID = arrState[1];
                    defaultProfile = pID;
                    window.location.hash = currHash; //IE Need to pick up now
                    //if ($("#ProfileCarousel").length == 0 || arrState.length > 1) {
                    pemco.home.resetCarouselDiv("our"); //reset html content

                    if ((defaultProfile == null || defaultProfile == "undefined") && (targetDefaultProfile != null && targetDefaultProfile != "undefined")) {

                        pemco.home.createProfileCarousel();

                        //                        var isIE = navigator.appName.indexOf("Microsoft") != -1;

                        //                        var intTimer = 1000;

                        //                        if (isIE) intTimer = 4000;

                        //                        setTimeout("getFlashMovie('ProfileCarousel').setDefaultProfile(targetDefaultProfile)", intTimer);
                    }
                    else {
                        pemco.home.createProfileCarousel();
                    }
                    //}

                    if (pID != null && pID != "undefined")
                        pageName = 'NW Profiles/' + pID;
                    else
                        pageName = 'NW Profiles';
                        
                    break;

                case 'commercials':

                    pemco.home.commercialsMode();

                    pemco.ajax.getPemcoProfileCommercial({
                        codename: arrState[1] || 'goatrenterguy'
                    }, function(res) {
                        if (!(res && res.data && res.data.ProfileCommercial)) {
                            pemco.ajax.flash('Invalid Data returned for commercial.', 'Error');
                        }
                        else {
                            blnAutoPlay = false;
                            if (arrState[1] !== undefined) {
                                blnAutoPlay = true;
                            }
                            var comm = res.data.ProfileCommercial[0];
                            pemco.commercials.loadmedia(comm.ads_id, comm.file_type, comm.filename, comm.image_filename, comm.profile_no, comm.title, comm.event_count, comm.code_name, '', blnAutoPlay, comm.image_title);
                        }
                    });

                    //pemco.tray.lower();
                    pageName = 'Commercials';

                    //Show Hide Top Nav
                    $('#top_profiles').hide();
                    $('#top_content').show();

                    break;

                default:
                    if (pemco.home) {
                        pemco.home.carouselMode();
                        //pemco.carousel.construct();
                        //pemco.tray.lower();
                    }
                    pageName = 'NW Profiles';

                    break;
            }
        }

        var strLocation = window.location.toString();

        //Update Tracking for calendar
        if (
        strLocation.indexOf("contests_selection.aspx") > 0 ||
        strLocation.indexOf("contests_audio_layout.aspx") > 0 ||
        strLocation.indexOf("contests_enter_chant.aspx") > 0 ||
        strLocation.indexOf("contests_enter_photo.aspx") > 0 ||
        strLocation.indexOf("contests_enter_send_friend.aspx") > 0 ||
        strLocation.indexOf("contests_enter_video.aspx") > 0 ||
        strLocation.indexOf("contests_image_layout.aspx") > 0 ||
        strLocation.indexOf("contests_polls_handler.aspx") > 0 ||
        strLocation.indexOf("contests_text_layout.aspx") > 0 ||
        strLocation.indexOf("contests_video_layout.aspx") > 0 ||
        strLocation.indexOf("poll_selection.aspx") > 0 ||
        strLocation.indexOf("poll_layout.aspx") > 0
       ) {
            pageName = window.location.toString();
        }

        pemco.stats.track(pageName);

        if (pemco.home) {
            //if ((pemco.carousel.isVisible() || arrState[0] == 'profiles' || arrState[0] == 'twitter' || arrState[0] == 'calendar' || !arrState[0]) && !pemco.carousel.isSpotlighted()) pemco.home.showBackground();
            if ((arrState[0] == 'profiles' || arrState[0] == 'userprofiles' || !arrState[0])) pemco.home.showBackground();
            else pemco.home.hideBackground();

            pemco.home.makeSureSomethingIsLoaded();
        }
    }

    // Speed up hash links
    $('a[href^=#]').live('click', function(e) {
        pub.changeHash(/\#(.*)/.exec(this.href)[1]);
        //		alert(this.tagName+(this.id?'#'+this.id:'')+(this.className?'.'+this.className:''));
        e.preventDefault();
    });

})(jQuery);

