var $j = jQuery.noConflict();

/*
<?
  //ini_set('display_errors', E_ALL);
  require_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-blog-header.php');
  header('HTTP/1.1 200 OK');
  global $current_user;
?>
*/
function initGTranslate() { PikeResearch.GoogleTranslate.ElementInit(); }

var PikeResearch = {

    init : function() {
        PikeResearch.EventTracking.init($);
        PikeResearch.GoogleTranslate.init($);
        PikeResearch.Billboards.init($);
        PikeResearch.CareersContactForm.init($);
    },

    CareersContactForm : {
        init : function() {
            if($j('.your-message textarea').length > 0 && typeof($j.fn.tinymce)!=='undefined') {
                PikeResearch.tinyMCE_interval = setInterval(function(){
                    if(typeof($j.fn.tinymce)!=='undefined') {
                        clearInterval(PikeResearch.tinyMCE_interval)
                        $j('.your-message textarea').tinymce({
                            script_url : '/wordpress/wp-content/themes/pike-research/javascript/tiny_mce/tiny_mce.js',
                            theme : 'simple'
                        });
                    }
                }, 500);
            }
        }
    },


    GoogleTranslate : {
        init : function() {
            if($j('#google_translate_element').length > 0) {
                $j.getScript('http://translate.google.com/translate_a/element.js?cb=initGTranslate');
            }
        },

        ElementInit : function() {
            new google.translate.TranslateElement({
                pageLanguage: 'en'
            }, 'google_translate_element');
            $j('#google_translate_element .skiptranslate')
            .html( $j('#google_translate_element .skiptranslate div') )
            .prepend('Translate:<br />');
        }
    },

    EventTracking : {

        /**
        *	Report brochure downloads
        *	Executive Summary downloads
        *	White Paper downloads
        *	Advisory Service brochure downloads
        *	Company Brochure downloads
        *	My Pike Report downloads
        *
        * For each event, capture (1) user name, (2) company name, (3) title, (4)
        * email address, (5) phone number, (6) state, (7) country.
        */

        init : function() {
            if( ! $j('.download-tracking').length > 0) { return; }
            $j('.download-tracking').click(function(){
                /*
                <? $userdata = get_userdata($current_user->ID); ?>
                var user_info = <?= (is_user_logged_in())?'"Name: '.$userdata->first_name .' '. $userdata->last_name.',\
                Username: ' . $userdata -> user_nicename . ',\
                Company: '. $userdata -> organization . ',\
                Title: '. $userdata -> title .',\
                Email: '.$userdata -> user_email.',\
                Phone: '.$userdata -> phone.',\
                State: '.$userdata -> stateprovince.',\
                Country: '.$userdata -> country.'";':'"Guest User";'; ?>
                var download_action = $(this).attr('title');

                if( typeof(pageTracker) != "undefined" ) {
                    pageTracker._trackPageview( $(this).attr('href') );
                    pageTracker._trackEvent('Downloads', download_action, user_info + ',\
                    Requested Download: ' + $(this).attr('href'));
                }
                return true;
                */
            });
        }

    },

    Billboards : {

        animating : false,
        currentCycle : 1,
        clicked : false,
        cycleTime : 10000,
        fadeTime : 1200,

        init : function() {
            $j('#home_carousel li').each(function() {
                $j(this).click(function() {
                    var href = $j("a", this).attr('href');
                    var target = href.charAt(href.length-1);
                    PikeResearch.Billboards.change( target, true );
                    return false;
                });
            });
            
            setTimeout("PikeResearch.Billboards.cycle()", PikeResearch.Billboards.cycleTime);
        },

        change : function(number, click) {
            if(click) PikeResearch.Billboards.clicked = true;
            if(PikeResearch.Billboards.animating) return;
            if($j("div#home_carousel div:visible:first").attr('class') == 'page_' + number) {
                return;
            };
            
            PikeResearch.Billboards.animating = true;
            
            $j('.currentbb').removeClass('currentbb');
            $j('#bb_' + number).addClass('currentbb');
            
            $j("div#home_carousel div:visible:first div").fadeOut(PikeResearch.Billboards.fadeTime / 2, function() {
                $j(this).parent().delay('100').fadeOut('100');
                var prev = this;
                $j('.page_' + number).fadeIn(PikeResearch.Billboards.fadeTime / 2, function() {
                    $j(prev).show();
                    PikeResearch.Billboards.animating = false;
                });
                
            });
        },

        cycle : function() {
            try {
                if(PikeResearch.Billboards.clicked) return;
                PikeResearch.Billboards.currentCycle++;

                if(PikeResearch.Billboards.currentCycle > 5) {
                    PikeResearch.Billboards.currentCycle = 1;
                }
                PikeResearch.Billboards.change(PikeResearch.Billboards.currentCycle,false);

                setTimeout("PikeResearch.Billboards.cycle()", PikeResearch.Billboards.cycleTime);
            } catch(e) {
                if(typeof(window.console!='undefined')) { window.console.log(e); }
            }
        }
    }
}

var researchTimeout, aboutTimeout, newsTimeout;

function hideDrop(name) {
    $j("#" + name + "_drop").hide();
    $j("#" + name + "_nav").removeClass(name + '_drop');
    clearTimeout(name + 'Timeout');
}
function hideBreadcrumb(i) {
    $j("a.selected_cat", i).removeClass("selected");
    $j("ul", i).removeClass("selected");
}

$j().ready(function() {
    $j.preloadCssImages();
    PikeResearch.init();
    
    $j('li#breadcrumb_dropdown').each(function() {
        $j(this).click(function() {
            $j("a.selected_cat", this).toggleClass("selected");
            $j("ul", this).toggleClass("selected");
            $j(this).mouseleave(function() {
                breadcrumbTimer = setTimeout(hideBreadcrumb, 200, this);
            });
        });
    }); 
	
	// Research Dropdown
    $j("#research_nav > a").mouseover(function() {
        $j(this).parent().addClass('research_drop');
        $j("#research_drop").show();
        clearTimeout(researchTimeout);
    });
    $j("#research_nav, #research_drop").mouseover(function() {
        clearTimeout(researchTimeout);
    });
    $j("#research_nav, #research_drop").mouseleave(function() {
        researchTimeout = setTimeout("hideDrop('research')", 100);
    });
	
	// About Dropdown
    $j("#about_nav > a").mouseover(function() {
        $j(this).parent().addClass('about_drop');
        $j("#about_drop").show();
        clearTimeout(aboutTimeout);
    });
    $j("#about_nav, #about_drop").mouseenter(function() {
        clearTimeout(aboutTimeout);
    });
    $j("#about_nav, #about_drop").mouseleave(function() {
        aboutTimeout = setTimeout("hideDrop('about')",100);
    });
	
	// News Dropdown
    $j("#news_nav > a").mouseover(function() {
        $j(this).parent().addClass('news_drop');
        $j("#news_drop").show();
        clearTimeout(newsTimeout);
    });
    $j("#news_nav, #news_drop").mouseenter(function() {
        clearTimeout(newsTimeout);
    });
    $j("#news_nav, #news_drop").mouseleave(function() {
        newsTimeout = setTimeout("hideDrop('news')",100);
    });
	
	// Events Dropdown
    $j("#events_nav > a").mouseover(function() {
        $j(this).parent().addClass('events_drop');
        $j("#events_drop").show();
        clearTimeout(eventsTimeout);
    });
    $j("#events_nav, #events_drop").mouseenter(function() {
        clearTimeout(eventsTimeout);
    });
    $j("#events_nav, #events_drop").mouseleave(function() {
        eventsTimeout = setTimeout("hideDrop('events')",100);
    });	
	
	// View All
    $j("#view_all").click(function() {
        var href = $j(this).attr('href');
        $j(this).parent().parent().fadeOut(100,function() {
           
            $j(href).fadeIn(100); 
        });
    });
    $j("a.mypike_view_all").each(function() {
        $j(this).click(function() {
            var parent = $j(this).parents('.my_pike_cat');
            $j('.display', parent).toggle();
            $j('.display-all', parent).toggle();
            $j('#extra', parent).toggle();
            $j(this).toggleClass('collapse');
            
        });
    });
    $j("#backToTop").click(function() { window.scrollTo(0, $j("#content").offset().top); return false; });

    (function() {
        var setBackToTopLocation = function() {
            if($j('#backtotop').length > 0) {
                var offset = $j("#footer").offset().top;

                var backToTopWidth = $j('#backtotop').width();

                var bodyLeftOffset = $j('#backtotop').parent().parent().offset().left;

                if ($j(window).scrollTop() + $j(window).height() < $j('#backtotop').parent().offset().top + 50) {
                    $j("#backtotop").stop().css({'top': '','left': ''});
                    $j("#backtotop").removeClass('fixed');
                    $j("#backtotop").removeClass('bottom');
                    $j("#backtotop").addClass('top');
                } else if ($j(window).scrollTop() + $j(window).height() < offset) {

                     var top = $j(window).height() - 40;
                     var left = (bodyLeftOffset);
                     $j("#backtotop").removeClass('top');
                     $j("#backtotop").removeClass('bottom');
                     $j("#backtotop").addClass('fixed');
                     $j("#backtotop").stop().css({'top': top + 'px','left': left + 'px'});
                } else {

                    $j("#backtotop").stop().css({'top': '','left': ''});
                    $j("#backtotop").removeClass('fixed');
                    $j("#backtotop").removeClass('top');
                    $j("#backtotop").addClass('bottom');
                };
            }
            
            
        }

        setBackToTopLocation();

        $j(window).scroll(setBackToTopLocation);
        $j(document).bind('filterComplete', setBackToTopLocation);
        } ());
});

