/*
 * jQuery scripts library - loads in behavior on demand
 *
 * Copyright (c) 2010 TUI Ski
 *
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Author URL:
 *   http://alstevens.co.uk
 *
 */



// importTweets  - loads the twitter feed using the google api jGFeed
// put your main funaction in here
// load the function here
$(document).ready(function(){
	// check dom for class name of library object
	
	
	if($('.importTweets').length) {
	//run the test to check for the neccessary scripts and on success load the function (callback)
	//parameters(object to check for,file url of plugin, function to callback once successfuly loaded);
	loadScript($.jGFeed,'/javascript/jquery.jgfeed.js',importTweets);
	}
	
	
	});


importTweets = function(){
		
			// retrieve data for resort, url and rss
			var resort = $('p.tweetData span').text();
			var url = $('p.tweetData a.twitter').attr('href');
			var rss = $('p.tweetData a.rss').attr('href');
			
function loadTwitterFeed(resort,url,rss){
			
			$.jGFeed(rss,
		  function(feeds){
			// Check for errors
			if(!feeds){
			  // there was an error
			  return false;
			}
			// do whatever you want with feeds here
			for(var i=0; i<feeds.entries.length; i++){
			  var entry = feeds.entries[i];
			  var feeddate = new Date(entry.publishedDate);
			  var niceDate = jQuery.timeago(feeddate);
			  var str = entry.title
			  var str2 = str.split(":");
			  var str3 = str2[1]+":"+str2[2]+":"+str2[3]+":"+str2[4]+":"+str2[5];
			  var str4 = str3.split(":undefined");
			  //console.log("str4 "+str4[0])
			  var div = "<div class='update'><p>"+str4[0]+"</p><p class='strong'><a href='"+url+"'>"+niceDate+"</a></p>";
			  $('.importTweets .rotator').append(div);
			}
		  }, 5);
		
		
		$('.quoteHolder').attr('style','display:none');
		
		}
		
		loadTwitterFeed(resort,url,rss);
		
		// next script
		
		if($('.rotator').length) {
		//run the test to check for the neccessary scripts and on success load the function (callback)
		//parameters(object to check for,file url of plugin, function to callback once successfuly loaded);
		loadScript($.fn.innerfade,'/javascript/jquery.innerfade.js',initiateRotator);
		}
		
}

// rotater
// animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), speed: Fadingspeed in milliseconds or keywords (slow, normal or fast)(Default: 'normal'), timeout: Time between the fades in milliseconds (Default: '2000'), type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), containerheight: Height of the containing element in any css-height-value (Default: 'auto')  runningclass: CSS-Class which the container get’s applied (Default: 'innerfade') }); 
initiateRotator = function(){
	$('.rotator').innerfade({ speed: 500, timeout: 9000, type: 'sequence', containerheight: '190px' });
	}








// This is the peice of code which checks for the script and if neccessary loads it into the page
// Values passes are: 
// obj: the function/object you need to check to determine whether the plugin has loaded
// fileurl: the filename of the js you want to load in
// callback: the function to call on success

function loadScript(obj, fileurl, callback){
	// start by checking whether the object is present
	if(typeof obj==='undefined'){
	 loadIt = function() {
     loadIt.getScript(fileurl);
     loadIt.tryReady(obj, fileurl, 0);
	}

  var time_elapsed = 0;

  loadIt.getScript = function(filename) {
  var script = document.createElement('script')
  script.setAttribute("type","text/javascript")
  script.setAttribute("src"	, filename)
  if (typeof script!="undefined")
  document.getElementsByTagName("head")[0].appendChild(script)
  }
  
  loadIt.tryReady = function(obj, fileurl, time_elapsed) {
  // Continually polls to see if obj has been loaded.
  if (typeof obj === "undefined") { // if obj isn't defined yet...
    if (time_elapsed <= 5000) { // and we havn't given up trying...
	
	var retry = function(){
		loadIt.tryReady((time_elapsed+200),fileurl,obj);
		}
	////console.log(time_elapsed)
     // set a timer to check again in 200 ms.
	 setTimeout(retry,200);
    } else {
      ////console.log("Timed out while loading "+fileurl+".")
	  
    }
  } else {
	
	// file is loaded into the DOM and we have checked that the object or function exists
	//  call the callback function
    callback();

  }
  }
 // initiates the whole loadIt function
 loadIt();
	}

else{
	// else if the object already exists
	// call the callback function
	callback();
	}

}

//ends global function loadScript


