function popupVideo( path ) {
    if ( $("#news_roll") ) $('#news_roll').val('false');
    if ( $("#live_scores_roll") ) $('#live_scores_roll').val('false');
    show_popup("<div id=\"playerContainer\"></div>", "Video", false, false, false);
    var fo = new SWFObject( "/includes/flowplayer/FlowPlayerLight.swf", "FlowPlayer", "720", "405", "9", "#fff", true);
            fo.addVariable("config", "{videoFile: '"+path+"', loop: true, bufferLength: 10, showPlayList: false, showStopButton: true, controlsOverVideo: 'locked', showFullScreenButton: false, showMenu: false, usePlayOverlay: true, autoPlay: true }");
            fo.write("playerContainer");
}


function show_popup( content, title, callback, ht, wd ) {
    if ( !ht ) ht = 550;
    if ( !wd ) wd = 670;
    if( $('#popup_content') ) { 
        $('#dialog').dialog('destroy');
        $('#dialog').remove();
    }
//  
    $("<div>").attr('id','dialog').appendTo($('body'));
    $("<div>").attr('id','popup_content').appendTo( $('#dialog') ).html( content );
    
    var btns;
    if (callback != 'NONE') { 
        if (callback != 'NULL') { 
            btns = { 
                "Ok": function() { 
                    var ret = eval( callback ); 
                    if ( ret && ret != NULL ) {
                        $(this).dialog('destroy'); 
                        $('#dialog').remove(); 
                    }
                },  
                "Cancel" : function() { 
                    $(this).dialog('destroy'); 
                    $('#dialog').remove(); 
                } 
            };
        } else {
            btns = { 
                "Cancel" : function() { 
                    $(this).dialog('destroy'); 
                    $('#dialog').remove(); 
                } 
            };
        }
    }
    $("#dialog").dialog({
        bgiframe: true,
        width: wd,
        height: ht,
        modal: true,
        title: title,
        buttons: btns,
        close: function() {
            $(this).dialog('destroy');
            $('#dialog').remove();
        }
    });
}
    
    
function demoMap( location ){
    
    
    var locs = new Array();
    locs['1'] = new Array();
    locs['1']['lat'] = 52.131572;
    locs['1']['long'] = -106.628451;
    locs['1']['name'] = "Rutherford Rink";
/*//\n69 Campus Dr\nS7N 5B1
*/  locs['2'] = new Array();
    locs['2']['lat'] = 52.13499;
    locs['2']['long'] = -106.637206;
    locs['2']['name'] = "Education Gym";
/*//28 Campus Dr    S7N 0X1
*/  locs['3'] = new Array();
    locs['3']['lat'] = 52.125545;
    locs['3']['long'] = -106.630318;
    locs['3']['name'] = "Field 7";

    locs['4'] = new Array();
    locs['4']['lat'] = 52.12706;
    locs['4']['long'] = -106.629846;
    locs['4']['name'] = "Griffiths Stadium in PotashCorp Park";
/*  //  5 Stadium Crescent
*/
    locs['5'] = new Array();
    locs['5']['lat'] = 52.129951;
    locs['5']['long'] = -106.631316;
    locs['5']['name'] = "Physical Activity Complex (PAC)";
/*  //87 Campus Drive   S7N 5B2
*/
    locs['6'] = new Array();
    locs['6']['lat'] = 52.12731;
    locs['6']['long'] = -106.626735;
    locs['6']['name']= "Saskatoon Field House";
/*  //2020 College Drive    S7N 2W4
*/
 Shadowbox.open({
     player: 'html',
     content: '',
     height: 400,
     width: 600,
     options: {
     onFinish: function(item){
             if(GBrowserIsCompatible()){
                 var map = new GMap2(document.getElementById('shadowbox_content'));
                 map.setMapType(G_HYBRID_MAP);
                 map.setCenter(new GLatLng(parseFloat(locs[location]['lat']),parseFloat(locs[location]['long'])), 16);
                 map.openInfoWindow(map.getCenter(),
                                  document.createTextNode(locs[location]['name']));
            
             }
            
             // add some simple controls
             map.addControl(new GSmallMapControl());
             map.addControl(new GMapTypeControl());
         }
         }
 });

};

function window_popup( url ) {
    if ( url.substring( 0, 10 ) == '/Home_Left'  ) {
        window.location.href = url;
    } else {
        window.open(url,'','toolbar=no,width=800,height=540,resizable=yes,scrollbars=yes'); 
    }
}

            
function runImageFix() {
    $('div.fixcontainer img').each( function(){
        $(this).attr("src","/"+$(this).attr("src"));
    } );
    $('div.fixcontainer a').each( function(){
        if ( $(this).attr('href').substr(0,5) != "http:" && $(this).attr('href').substr(0,6) != "https:" ) {
            $(this).attr('href',"/"+$(this).attr('href'));
        }
    } );
}
            
function show_detail( id, el ) { 
    $('#game_detail').html("<img src='/images/game_details_bg.png' width='362' height='103' class='fixPNG' /><div class='game_day_game'><img src='/images/ajax-loader.gif' border='0' /></div>");
    
    $('#game_detail').css({
        'top' : $('#calendar_month').css('top')-100+'px',
        'left' : $('#calendar_month').css('left')+'px'
    });
    //$('#game_detail').style.top = ( ( el.getCoordinates($('calendar_month')).top ).toInt() - 100 )+'px'; 
    //$('#game_detail').style.left = ( ( el.getCoordinates($('calendar_month')).left ).toInt() )+'px';  
    
    $('#game_detail').fadeIn();
    $.ajax({
        url : '/includes/ajax_functions.php',
        type : 'post',
        data : "action=load_event&id="+id,
        success : function( msg ) { $('#game_detail').html(msg); }
    });
    
    //var myajax = new Request.HTML( { url : '/includes/ajax_functions.php', method : 'get' , update : 'game_detail' } ); 
    //myajax.send('action=load_event&id='+id); 
 } 
            

    function validatemobileupdate() {
        
        var ret = true;
        var error = "";
        var checked = false;
        $("input[type='checkbox']").each( function(el) {
            if ( $(this).is(':checked') ) checked = true;
            });
        if ( !checked ) {
            ret = false;
            error += "Please select a sport you want to recieve updates.\n\r";
        }
        if ( $('#name').val() == '' ) {
            ret = false;
            error += "Please provide your name.\n\r";
        }
        if ( $('#email').val() == '' ) {
            ret = false;
            error += "Please provide your email.\n\r";
        }
        if ( $('#sms_carrier option:selected').length == 0 ) {
            ret = false;
            error += "Please select your cellular carrier.\n\r";
        }
        if ( $('#areacode').val().length != 3 )  {
            ret = false;
            error += "Please provide your area code.\n\r";
        }
        if ( $('#phoneprefix').val().length != 3 )  {
            ret = false;
            error += "Please provide your full cellular number.\n\r";
        }
        if ( $('#phonenumber').val().length != 4 ) {
            ret = false;
            error += "Please provide your full cellular number.\n\r";
        }
        if ( ret ) {
            $('#mobile_updates').submit();
        } else {
            alert( error );
        }
    }


function rotate_header_feature( el ) {
    var curr = $("#game_day_cont .feature_item:visible");
    var next = $(curr).next();
    if ( $(next).length == 0 ) next = $("#game_day_cont .feature_item:first");
    $(curr).fadeOut('fast',function(){ $(next).fadeIn('fast') });
}

function scrollSchedule( elid, direction ) {
    var el = $("#schedule_"+$("#current_schedule").val());
    var pos = $(el).css('left');
    if ( pos == 'auto' ) pos = '0px';
    var interval = 113;
    
    pos = parseInt( pos.replace('px','') );
    if ( direction == 'next' ) pos = pos - interval; 
    else pos = pos + interval;
    if ( pos <= 20 ) {
        $(el).animate({'left':pos});
        
        
        //var myEffect = new Fx.Morph(el, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
        //myEffect.start({ 'left': pos });
    }
}

function rotate_gameday( ) {
    var next_game;
    var games = new Array();
    var i = 0;
    $('#game_day_cont div.game_day_game').each( function(  ) {
        games[i] = $(this).attr('id');
        if ( $(this).is(":visible") ) {
            next_game = i + 1;
            $(this).hide();
        }
        i++;
    });
    i = 0;
    if ( next_game == games.length  ) next_game = 0;
    
    $('#banner div.banner_overlay').each( function(  ) {
        if ( $(this).is(":visible") ) {
            $(this).hide();
        }
        if ( i == next_game ) $(this).fadeIn(  );
        i++;
    } );
    
    $( "#"+games[next_game] ).fadeIn(  );
}

function last_live_score(  ) {
    var prev = $("#live_scores .score:visible").prev();
    if ( !prev ) prev = $("#live_scores .score")[$("#live_scores .score").length-1];
    $("#live_scores .score:visible").fadeOut('fast',function(){ $(prev).fadeIn('fast');});  
}
function next_live_score(  ) {
    var next = $("#live_scores .score:visible").next(".score");
    if ( next.length == 0 ) next = $("#live_scores .score")[0];
    $("#live_scores .score:visible").fadeOut('fast',function(){$(next).fadeIn('fast');});
}
function rotate_news( ) {
    if ( $("#news .primary_story").length > 0 && $("#news_roll").val() == 'true' ) {
        var id = $("#news .primary_story:visible").attr('id').replace("story_","");
        var next = $("#thumb_"+id).next(".thumb_img");
        if ( $(next).length == 0 ) next = $("#thumb_1");
        $(next).trigger('click'); 
        $("#news_roll").val('true');
        setTimeout( "rotate_news( )", 5000 );
    }
}

function swap_schedule( el ) {
    $('#schedule_'+$("#current_schedule").val()).fadeOut(  );
    $('#schedule_'+el).fadeIn();
    $("#"+$("#current_schedule").val()+'_btn').attr('src',"/images/btn_"+$("#current_schedule").val()+"_off.png");
    $("#"+el+'_btn').attr('src',"/images/btn_"+el+"_on.png");
    $('#current_schedule').val(el);
}
function swap_results( show_el ) {
    if ( show_el == 'results' ) {
        $('#brief_schedule').fadeOut(  );
        $('#schedule_buttons').fadeOut(  );
        $('#btn_main_results').attr('src','/images/btn_team_results_on.jpg');
        $('#btn_main_schedule').attr('src','/images/btn_team_schedule_off.jpg');
        setTimeout( "$('#results').fadeIn()", 300 );
    } else {
        $('#results').fadeOut(  );
        $('#btn_main_results').attr('src','/images/btn_team_results_off.jpg');
        $('#btn_main_schedule').attr('src','/images/btn_team_schedule_on.jpg');

        setTimeout( "$('#brief_schedule').fadeIn( )", 300 );
        setTimeout( "$('#schedule_buttons').fadeIn( )", 300 );
    }
}
 function swap_standings( el ) {
    var curr = $("#current_standings").val();
    $('#'+curr).fadeOut();
    $('#'+curr+'_btn').attr('src',"/images/btn_"+curr+"_off.jpg");
    $('#'+el+'_btn').attr('src',"/images/btn_"+el+"_on.jpg");
    $('#current_standings').val( el );
    $('#'+el).fadeIn();
}
    function next_page( id ) {
        $('#page_'+id).fadeOut( );
        id++;
        $('#page_'+id).fadeIn( );
    }
    function prev_page( id ) {
        $('#page_'+id).fadeOut( );
        id--;
        $('#page_'+id).fadeIn( );
    }
   function swap_calendars( el ) {
        if ( $('#game_detail').is(":visible") ) $('#game_detail').fadeOut( );
        if ( el == 'list' ) {
            $('#list_btn').attr('src','/images/btn_list_view_on.jpg');
            $('#month_btn').attr('src','/images/btn_calendar_view_off.jpg');
            $('#calendar_month').fadeOut( );
            $('#calendar_list').fadeIn( );
            $('#module_focus').val('list');
        } else {
            $('#list_btn').attr('src','/images/btn_list_view_off.jpg');
            $('#month_btn').attr('src','/images/btn_calendar_view_on.jpg');
            $('#calendar_list').fadeOut( );
            $('#calendar_month').fadeIn( );
            $('#module_focus').val('month');
        }
    }

//  window.addEvent( 'domready', function() { 
//        if ( $('results') ) {
//          new MooScroller($('results').getElement('div.scrollContent'), $('results').getElement('div.scrollKnob'), {
//              scrollLinks: {
//                  forward: $('results').getElement('div.scrollForward'),
//                  back: $('results').getElement('div.scrollBack')
//              }
//          });
//      }
    //if ( $('results') ) swap_results( 'results' );
//  });

$(function(){

    $("#nav_left span").bind( 'click', function() { 
        if ( $("#"+$(this).attr('section')+"_subnav span").length > 0 ) {
            $("#second_nav .subnav").each( function() { 
                if ( $(this).css("marginTop") == '-20px' ) $(this).animate({'marginTop':"0px"});
             } );
             $("#"+$(this).attr('section')+"_subnav").animate({'marginTop':"-20px"});
        }
    } )

    $("#news_archive_btn").bind('click',function(){ window.location.href = "http://huskies.usask.ca/news/index.php"; });
    runImageFix();
    
    $("#archived_stats_list .collapsable").hide();
    $("#archived_stats_list .title").bind('click',function(){ $(this).next(".collapsable").slideToggle(); });
});

$(window).load( function() {

    if ( $("#game_day_cont .game_day_game").length > 1 ) setInterval( "rotate_gameday( )", 5000 );
    if ( $("#game_day_cont .feature_item").length > 1 ) setInterval( "rotate_header_feature('null')", 5000 );
    setInterval( 'next_live_score()', 5000 );
    setTimeout( "rotate_news( )", 5000 );
    
    $("#news_archive .thumb_img").bind( 'click', function() {
        var next = $(this);
        $("#news_roll").val('false');
        $("#news_archive .thumb_img").animate({opacity:'0.3'},300,function(){
            $(next).animate({opacity:'1'});
            $( "#story_"+$(next).attr('id').replace("thumb_","") ).fadeIn();
        });
        $("#news .primary_story:visible").fadeOut(300);
    });
});
