var documentReady = false;


function initHomePage(){
  // Attempt to open the flash get flash link in a new window here so the html still validates.
  $('#home-main-alternate a').click(function(){window.open($(this).attr("href"));return false;});

  // Main flash movie embed
  var flashvars = {};
  // Set up the links
  flashvars.academics_calendar = "http://www.stetson.edu/academicaffairs/calendar.php";
  flashvars.academics_programs = "http://www.stetson.edu/academics/programs.php";
  flashvars.academics_studies = "http://www.stetson.edu/admissions2/graduates.php";
  flashvars.academics_research = "http://www.stetson.edu/research/surcas.php";
  flashvars.academics_learn_btn = "http://www.stetson.edu/academics/learnmore.php";

  flashvars.admissions_req_info = "http://www.stetson.edu/contact/admissions.php";
  flashvars.admissions_tour = "http://www.stetson.edu/visitus/";
  flashvars.admissions_visit = "https://www.stetson.edu/secure/forms/admissions/campustour/";
  flashvars.admissions_app_status = "#";
  flashvars.admissions_deposit = "http://www.stetson.edu/accepted/";
  flashvars.admissions_fees = "http://www.stetson.edu/financialaid/tuition.php";
  flashvars.admissions_learn_btn = "http://www.stetson.edu/admissions2/index.php";

  flashvars.campus_learn_btn = "http://www.stetson.edu/campuslife/home/learnmore.php";

  flashvars.discover_learn_btn = "https://www.stetson.edu/secure/apps/wordpress/?cat=18";

	var params = {};
	params.allowfullscreen = "true";
	params.allowscriptaccess = "sameDomain";

	var attributes = {};
	attributes.id = "main-flash";

	swfobject.embedSWF("media/flash/home-main.swf", "home-main-alternate", "760", "394", "9.0.0", "media/flash/playerProductInstall.swf", flashvars, params, attributes);

	// Featured column
	$('#homepage-scrollable').scrollable({horizontal:true, size:1, navi:'#scrollable-navi', onSeek:function(){
	  var text = $('#scrollable-navi li:eq(' + this.getPageIndex() + ')').html();
	  $('#scrollable-title').text(text);
	}});

	// Show the featured column nav since they have js
	$('#scrollable-navi').show();
	// Clean up a few things that were left in a state optimised for no js.
	$('#homepage-scrollable div.items > div').css('float', 'left')
	$('#homepage-scrollable div.items > div h1').css('display', 'none');
	$('#homepage-scrollable div.items').css('position', 'absolute');
	$('#homepage-scrollable').css('overflow', 'hidden');
	$('#homepage-scrollable').css('position', 'relative');
}

function loadDsicoverRss(){

  var data = [];

  data.push({'title':'Accepted students connect with Stetson', 'link':'http://www2.stetson.edu/wordpress/?p=9983'});
  data.push({'title':'455 receive degrees at Spring Commencement', 'link':'http://www2.stetson.edu/wordpress/?p=10181'});
  data.push({'title':'Students build fellowship going Into the Streets', 'link':'http://www2.stetson.edu/wordpress/?p=9998'});
  data.push({'title':'Stetson grad named UK Intl Student of Year', 'link':'http://www2.stetson.edu/wordpress/?p=9958'});

  var obj = swfobject.getObjectById('main-flash');

  if(obj){
    obj.setDiscoverRss(data);
  }

  /* This is what used to get it from wordpress.
   $.jGFeed('http://www2.stetson.edu/wordpress?feed=rss2', function(feeds){

		if(feeds){
			var obj = swfobject.getObjectById('main-flash');

      if(obj){
        obj.setDiscoverRss(feeds.entries);
      }
   	}
 }, 4);*/
}

function initCommon(){
  // Google Analytics
  //$.ga("UA-820967-1");

  // Attempt to open the social links in a new window here so the html still validates.
  $('.social a').click(function(){window.open($(this).attr("href"));return false;});

  // -1 in quotes for ie.
  $('#left-nav').accordion({autoHeight:false, collapsible:true, active:'-1'});

  $("div.tooltip").tooltip({delay:0, effect:'toggle', position:['top', 'center'], offset:[10, 0]});

  // Delay applying the large bg image so the important stuff can load first. Plus we are doing it randomly.
  chooseBg();

  // Show the temperature.
  getTemp();
}

function initLandingPage(section){
  // Main flash movie embed
  var flashvars = {dataPath:'media/xml/landing-' +  section + '-config.xml'};
	var params = {};
	params.allowfullscreen = "true";
	params.allowscriptaccess = "sameDomain";
  params.wmode = "transparent";
	var attributes = {};
	attributes.id = "main-flash";
	swfobject.embedSWF("media/flash/coverFlip3D.swf", "landing-main-alternate", "760", "542", "9.0.0", "media/flash/playerProductInstall.swf", flashvars, params, attributes);
}


function initBlog(){
  $('#page.wordpress #content ul.nav').superfish({dropShadows:true});

  // Set up the archives to hide all but a few until the more button is clicked.
  var archiveLi = $('#page.wordpress #content #sidebar .archives li');
  var numToShow = 5;
  if(archiveLi.size() > numToShow){
    $('#page.wordpress #content #sidebar a.more-button').show();
    $('#page.wordpress #content #sidebar .archives li:gt(' + (numToShow - 1) +')').hide();
  }
  $('#page.wordpress #content #sidebar a.more-button').click(function(){
    $('#page.wordpress #content #sidebar .archives li').slideDown('fast');
  });
}

function chooseBg(){
  var max = 12; // Change this to match the number of image choices. They must be named sequentially.
  var i = Math.ceil(Math.random() * max);

  $('body').css("background-image", "url('media/img/bgs/" + i + ".jpg')");
}

function loadHomepageNews(){
 $.jGFeed('https%3A%2F%2Fwww.stetson.edu%2Fsecure%2Fapps%2Fwordpress%3Ffeed%3Drss2%26cat%3D4', function(feeds){
		if(feeds){
			var e = feeds.entries;
			var len = e.length;
			var news = $('#homepage-news');
      var d, dateDisplay;

    	for(var i = 0; i < len; i++){
        d = new Date(e[i].publishedDate);
        dateDisplay = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
				news.append('<tr><td class="quiet">' + dateDisplay + '</td><td class="loud"> | </td><td><a href="' + e[i].link + '">' + e[i].title + '</a></td></tr>');
			}
   	}
 }, 5);
}

function loadHomepageEvents(){
 $.jGFeed('https://www.stetson.edu/secure/feeds/all_calendars.rss', function(feeds){
		if(feeds){
			var e = feeds.entries;
			var len = e.length;
			var events = $('#homepage-events');
		var d, dateDisplay;

    	for(var i = 0; i < len; i++){
    	  d = new Date(e[i].publishedDate);
        dateDisplay = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
				events.append('<tr><td class="quiet">' + dateDisplay + '</td><td class="loud"> | </td><td><a href="' + e[i].link + '">' + e[i].title + '</a></td></tr>');
			}
   	}
 }, 4);
}

// Provided by YAHOO Weather.
function getTemp(){
  var yql= "http://query.yahooapis.com/v1/public/yql?q=select%20item.condition.temp%20from%20weather.forecast%20where%20location%3D%2032723&format=json&callback=?";

  $.getJSON(yql, function(data){
    try{
      var temp = data.query.results.channel.item.condition.temp;

      if(temp){
        $("#temp span.value").html(temp + "&deg;");
        return;
      }
    }catch(e){}

    $("#temp").remove();
  });
}
