﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery */

//  
//  Author: Kevin Burkitt
//  Date:   2010-02-25
//  
//  CC Namespace Root Object
//
var cc = function() {
    return {
        activeUsername: '',
        activeUserAvatar: '',
        activeUserIsSubscriber: '',

        //  CC Logging Function
        log: function(message) {
            if (typeof console !== 'undefined') {
                if (typeof console.log === 'function') {
                    console.log(message);
                }
            }
        },

        //  
        //  Init Functions
        //
        // target: used to specify a target container - called after ajax load
        // so that init applied to container contents
        init: function(target) {

            if (typeof target === 'undefined') {
                target = 'body';
            }

            // Init on Page Load only:
            if (target === 'body') {

                // AJAX HELPERS
                jQuery.ajaxSetup({
                    error: function(xhr, ajaxOptions, thrownError) {
                        cc.log('ERROR: STATUS:' + xhr.status + ' - ERROR: ' + thrownError);
                        cc.ajax.displayAjaxError();
                    },
                    success: function(data, textStatus, XMLHttpRequest) {
                        if (typeof data !== 'undefined' && data.indexOf('##Redirect##') === 0) {
                            location.href = data.substr(12);
                        }
                    }
                });

                // SEARCH SUGGEST 
                cc.suggest.init();

                // FOOTER BUBBLES
                /*
                jQuery("a.cc-icon img").hover(function() {
                jQuery(this).next("em").stop(true, true).animate({ opacity: "show", top: "-105" }, "slow");
                }, function() {
                jQuery(this).next("em").animate({ opacity: "hide", top: "-115" }, 200);
                });
                */

                // HEADER LOGIN BOX
                cc.auth.login.init();


                // Header Menu: show overlay
                jQuery(".cc-menu").hoverIntent({
                    sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    

                    interval: 10, // number = milliseconds for onMouseOver polling interval    
                    over: function() {
                        jQuery(".cc-submenu", jQuery(this)).stop(true, true).animate({ opacity: "show", top: "-5px" }, 300);
                        return false;
                    },

                    timeout: 300, // number = milliseconds delay before onMouseOut    
                    out: function() {
                        jQuery(".cc-submenu", jQuery(this)).animate({ opacity: "hide", top: "5px" }, 200);
                    }
                });



                /*
                *  Watch Page
                */
                jQuery('#cc-wr-imageslink').click(function() {
                    jQuery('#cc-wr-imageslink').addClass('active');
                    jQuery('#cc-wr-videoslink').removeClass('active');
                    jQuery('#wr-images,#wr-videos').stop(true, true).animate({ 'margin-left': "0px" }, 300);
                });
                jQuery('#cc-wr-videoslink').click(function() {
                    jQuery('#cc-wr-videoslink').addClass('active');
                    jQuery('#cc-wr-imageslink').removeClass('active');
                    jQuery('#wr-videos,#wr-images').stop(true, true).animate({ 'margin-left': "-300px" }, 300);
                });


                /*
                *  User Profile Page
                */
                // Media Slider
                jQuery('#cc-up-imageslink').click(function() {
                    jQuery('#cc-up-imageslink').addClass('active');
                    jQuery('#cc-up-videoslink').removeClass('active');
                    jQuery('#up-images,#up-videos').stop(true, true).animate({ 'margin-left': "0px" }, 300);
                });
                jQuery('#cc-up-videoslink').click(function() {
                    jQuery('#cc-up-videoslink').addClass('active');
                    jQuery('#cc-up-imageslink').removeClass('active');
                    jQuery('#up-videos,#up-images').stop(true, true).animate({ 'margin-left': "-460px" }, 300);
                });



                /*
                *  Homepage Slider
                */
                jQuery("#cc-slider").easySlider({
                    auto: true,
                    continuous: true,
                    pause: 8000,
                    speed: 250,
                    controlsFade: true
                });


                /*
                * Error Message (displays message if querystring contains: ?emsg=My+Error+Message )
                */
                cc.helpers.queryStringErrorMessage();

                /*
                * Bad Browser Message (displays message if browser unsupported (IE6 or whatever)
                */
                cc.badbrowser.init();

                /* 
                *  Init page tracking Code
                */
                setTimeout(function() {
                    cc.tracking.init(target);
                }, 100);
            }


            //
            //  THE REMAINING FUNCTIONS ARE ALSO CALLED AFTER AJAX REQUESTS - BEWARE OF DOUBLE BINDING 
            //  EVENTS (e.g. so that jQuery.click handler would be called twice)
            //

            /*
            *  Ajax Load Links 
            */
            cc.ajax.init(target);

            // INIT HISTORY
            cc.history.init(target);

            // Capture return key for Form input fields
            jQuery('.cc-return').each(function() {
                jQuery(this).bind("keydown", function(e) {
                    if (e.keyCode === 13) {
                        jQuery(this).parents('form:first').submit();
                        return false; //prevent default behaviour
                    }
                });
            });

            /*
            * Init UI Helpers
            */
            cc.ui.init(target);

            // LOCAL SCROLL
            jQuery(target).localScroll({ axis: 'y', hash: true, duration: 300, offset: { top: -30, left: 0} });

            /*
            *  Comments (Paging)
            */
            cc.comments.init(target);

            /*
            *  Watch Page
            */
            cc.watch.init(target);


            /*
            *  My Profile Page Helpers
            */
            cc.myprofile.init(target);


            /*
            *  Mail Page Helpers
            */
            cc.mail.init(target);


            // FOCUS
            jQuery('.cc-focus', target).focus();

            // HINTS
            jQuery('label.pre', target).labelOver('over');

            // INIT AUTH
            cc.auth.init(target);

            // INIT 'Expose' Effect on FAQ page
            cc.ui.faqInitExpose();

            // Init Payment Links
            cc.payments.init(target);

            /*
            *  Stars Overlays (for Watch page)
            */
            cc.stars.init(target);

            /*
            *  FORMS 
            */
            cc.form.init(target);

            /*
            *  USER PROFILE PAGE HELPERS
            */
            cc.userprofile.init(target);


            /*
            *  Video Chat Controller
            */
            // Give page a few moments to settle
            cc.videochat.init(target);

            /*
            *  Init Alerts Updates
            */
            cc.alerts.init(target);
        }

    };
} ();


//  
//  Author: Kevin Burkitt
//  Date:   2010-02-25
//  
//  Global Init Function
//

jQuery(document).ready(function() {

    cc.init();
    
});


//
//  OuterHtml helper class for jQuery
//
//  See: http://brandonaaron.net/blog/2007/06/17/jquery-snippets-outerhtml/
//
jQuery.fn.outerHTML = function() {
    return jQuery('<div>').append(this.eq(0).clone()).html();
};

// Background image flickers on hover
// http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=1&postId=1104
//try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { }

