$(document).ready(function() {
    InitSite();
    InitUtils();

    browserTest();
    
    $(window).resize( function() {
        browserTest();
        InitUtils()
    });
});

//GENERIC SITE----------------------------------------------------------------------------->
//test to see if not ie6
function browserTest(){
    if ( $.browser.msie ){
        if($.browser.version != 6.0){
            checkSize();
        }
    }else{
        checkSize();
    }
}

//hide elements depending on width of screen
function checkSize(){
    if($(window).width() < 1000){
        $("#right-col").fadeOut();
    }
    if($(window).width() > 1000){
        $("#right-col").fadeIn();
    }
}

function InitSite(){
    //check to see if theme cookie exists
    var curTheme = $.cookie("cur_theme");
	if (curTheme){
	    $("link#theme").attr("href",curTheme);
	};

    //show social networking links
    $('#social').css('visibility', 'visible');
    $('#superfresh').css('visibility', 'visible');
    $('#sharebar A').css('visibility', 'visible');

    //initialise navigation
    InitQuickNav();
    InitThemeNav();
}


function InitQuickNav(){
    
var timeout    = 500;
    var closetimer = 0;
    var ddmenuitem = 0;
    var currentLink = null;
    var newMenu = null;
    var navState = null;
    
    $('#qn').bind('mouseover',  jsddm_open)
    $('#qn').bind('mouseout',  jsddm_timer)

    function jsddm_open()
    {   
       //cancel timer and close
       jsddm_canceltimer();
       jsddm_close();
       
       $("#quick-nav").css('visibility', 'visible');
       
       
       //bind the mouseover and mouseout events to the sub nav elements
       $('#quick-nav').bind('mouseover',  jsddm_open)
       $('#quick-nav').bind('mouseout',  jsddm_timer)
       
    }
    
    //control mouse over on sub nav
    function jsddm_openSub()
    {   
        //cancel timer and close
       jsddm_canceltimer();
       jsddm_close();
       
       //need to add the selected class to the main nav item again
       $("#quick-nav").css('visibility', 'visible');
    }

    function jsddm_close()
    {  
        $("#quick-nav").css('visibility', 'hidden');
    
    }

    function jsddm_timer()
    {  closetimer = window.setTimeout(jsddm_close, timeout);}

    function jsddm_canceltimer()
    {  if(closetimer)
       {  window.clearTimeout(closetimer);
          closetimer = null;}}

    $(document).ready(function()
    {  $('.levelone > li > A').bind('mouseover', jsddm_open)
       $('.levelone > li > A').bind('mouseout',  jsddm_timer)
    });

    document.onclick = jsddm_close;
    
}

function InitThemeNav(){
    
var timeout    = 500;
    var closetimer = 0;
    var ddmenuitem = 0;
    var currentLink = null;
    var newMenu = null;
    var navState = null;
    
    $('#ct').bind('mouseover',  jsddm_open)
    $('#ct').bind('mouseout',  jsddm_timer)

    function jsddm_open()
    {   
       //cancel timer and close
       jsddm_canceltimer();
       jsddm_close();
       
       $("#theme-nav").css('visibility', 'visible');
       
       
       //bind the mouseover and mouseout events to the sub nav elements
       $('#theme-nav').bind('mouseover',  jsddm_open)
       $('#theme-nav').bind('mouseout',  jsddm_timer)
       
    }
    
    //control mouse over on sub nav
    function jsddm_openSub()
    {   
        //cancel timer and close
       jsddm_canceltimer();
       jsddm_close();
       
       //need to add the selected class to the main nav item again
       $("#theme-nav").css('visibility', 'visible');
    }

    function jsddm_close()
    {  
        $("#theme-nav").css('visibility', 'hidden');
    
    }

    function jsddm_timer()
    {  closetimer = window.setTimeout(jsddm_close, timeout);}

    function jsddm_canceltimer()
    {  if(closetimer)
       {  window.clearTimeout(closetimer);
          closetimer = null;}}

    $(document).ready(function()
    {  $('.levelone > li > A').bind('mouseover', jsddm_open)
       $('.levelone > li > A').bind('mouseout',  jsddm_timer)
    });

    document.onclick = jsddm_close;
    
    //handle theme changes
    $('#theme-nav li A').click(function(evt) {
        evt.preventDefault();
        $("link#theme").attr("href",$(this).attr('rel'));
        //set cookie
        $.cookie("cur_theme", null);
        $.cookie("cur_theme", $(this).attr('rel'), {expires: 365, path: '/'});
    });
    
}

function InitUtils(){

    $('#social').css('top', $(window).height() - 90); 
    
    $('#superfresh').css('top', $(window).height() - 110); 

    $('#superfresh A').css('opacity', .5); 
    
    $('#superfresh A').mouseenter(function() {
            $('#superfresh A').animate({ opacity: 1}, 500 );

    });
    $('#superfresh A').mouseleave(function() {
            $('#superfresh A').animate({ opacity: 0.5}, 500 );
    });
}
function InitScroll(){
    $(".scroll-move").jCarouselLite({
        visible: 3,
        easing: 'easeInOutCubic',
        speed: 900,
        btnNext: ".scrollNext",
        btnPrev: ".scrollPrev",
        circular: false

    });

}

function InitIssueScroll(){
    $(".issue-scroll-move").jCarouselLite({
        visible: 1,
        easing: 'easeInOutCubic',
        speed: 900,
        btnNext: ".issueScrollNext",
        btnPrev: ".issueScrollPrev",
        circular: false

    });

}

function InitSponsors(){
    $(".sponsor-scroll-move").jCarouselLite({
        visible: 4,
        //auto: 2500,
        easing: 'easeInOutCubic',
        speed: 900,
        auto: 2500
    });
}

function InitList(){
    //change main image when thumbnails are clicked
    $("UL.main-list LI").mouseenter(function (evt) {
        evt.preventDefault();
        $(this).css("background-color","#ffffff");
    });
    $("UL.main-list LI").mouseleave(function (evt) {
        evt.preventDefault();
        $(this).css("background-color","Transparent");
    });
}
function InitScrollList(){
    //change main image when thumbnails are clicked
    $(".scroll-move UL LI").mouseenter(function (evt) {
        evt.preventDefault();
        $(this).css("background-color","#ffffff");
    });
    
    $(".scroll-move UL LI").mouseleave(function (evt) {
        evt.preventDefault();
        $(this).css("background-color","Transparent");
    });
}

function InitMainArticle(){
    //change main image when thumbnails are clicked
    $("A.main-article").mouseenter(function (evt) {
        evt.preventDefault();
        $("IMG.arrow").fadeIn("med");
    });
    
    $("div#main-article-box H3 A").mouseenter(function (evt) {
        evt.preventDefault();
        $("IMG.arrow").fadeIn("med");
    });
    
    $("A.main-article").mouseleave(function (evt) {
        evt.preventDefault();
        $("IMG.arrow").fadeOut("med");
    });
    
    $("div#main-article-box H3 A").mouseleave(function (evt) {
        evt.preventDefault();
        $("IMG.arrow").fadeOut("med");
    });
}

function InitShare(){
    curTitle = $("#fb").attr("href");
    //change main image when thumbnails are clicked
    $("A#shareLink").click(function (evt) {
        evt.preventDefault();
        $("#share").slideToggle();
    });
    $("#fb").attr("href", "http://www.facebook.com/sharer.php?u=" + window.location + "&t=" + curTitle);
    $("#tw").attr("href", "http://twitter.com/home?status=" + window.location);
    $("#ms").attr("href", "http://www.myspace.com/Modules/PostTo/Pages/?u=" + window.location + "&t=" + curTitle);
    $("#di").attr("href", "http://digg.com/submit?phase=2&url=" + window.location + "&title=" + curTitle);
}

function InitGallery(){
    //change main image when thumbnails are clicked
    $("UL.gallery li a").lightBox();
}

//cufon font replacement
Cufon.replace(['h1', 'h2.line', 'h2.black', '.side-box h3'], {fontFamily: 'Foundry Demi'});
//Cufon.replace(['#main-nav UL LI'], {fontFamily: 'Foundry Light'});

