$(document).ready(function() {
    var config = {
        over: makeTall, // function = onMouseOver callback (REQUIRED)    
        timeout: 185, // number = milliseconds delay before onMouseOut
        out: makeShort, // function = onMouseOut callback (REQUIRED)   
        sensitivity: 50,
        interval: 100
    };
    
    $("#mainNav ul li").hoverIntent(config);


    function makeTall() {
        $(this).addClass("hover");
        $('ul:first', this).css('display', 'block');
    }

    function makeShort() {
        $(this).removeClass("hover");
        $('ul:first', this).css('display', 'none');
    }
    
    $("#accordian").accordion({ autoHeight:false, collapsible: true, active: false });
    
    
	 $("ul.videosuccess li a").live('click', function() {
		loadStory(this);
		return false;
	 });

	 
	 //use title of img as <a>value</a> to be displayed to user and src as background image
	 $("ul.takeaction li a").each(function (i) {
		$(this).attr("style",'background-image: url("' + $(this).find("img").attr('src')+ '");');
		$(this).html($(this).html() + $(this).find("img").attr('title'));
		$(this).find("img").remove();
	});
	//use title of img as <a>value</a> to be displayed to user and src as background image
	 $("ul.iconlist li a").each(function (i) {
		$(this).attr("style",'background: url("' + $(this).find("img").attr('src')+ '") no-repeat;');
		$(this).html($(this).html() + $(this).find("img").attr('title'));
		$(this).find("img").remove();
	});
	$("ul.videosuccess li a").each(function (i) {
		$(this).attr("style",'background: url("' + $(this).find("img").attr('src')+ '") no-repeat;');
		$(this).html($(this).html() + $(this).find("img").attr('title'));
		$(this).find("img").remove();
	});

	if(typeof(jsonList) != 'undefined') {
		var storyDefaultId = jsonList.first;
	
		//Load default story
		loadStory($('#'+storyDefaultId));
	}
	//TakeAction
	selectTakeAction();
});
function selectTakeAction() {
	if($('#takeaction-page').hasClass('takeaction-donate')) {
		$('#takeaction-donate').addClass('current');
	}
	if($('#takeaction-page').hasClass('takeaction-fundraise')) {
		$('#takeaction-fundraise').addClass('current');
	}
	if($('#takeaction-page').hasClass('takeaction-internship')) {
		$('#takeaction-internship').addClass('current');
	}
	if($('#takeaction-page').hasClass('takeaction-volunteer')) {
		$('#takeaction-volunteer').addClass('current');
	}
	if($('#takeaction-page').hasClass('takeaction-attendevent')) {
		$('#takeaction-attendevent').addClass('current');
	}
	if($('#takeaction-page').hasClass('takeaction-stayconnected')) {
		$('#takeaction-stayconnected').addClass('current');
	}
	if($('#takeaction-page').hasClass('takeaction-bookspeaker')) {
		$('#takeaction-bookspeaker').addClass('current');
	}
}
function loadStory(obj) {
	//Remove active class from ALL a elements and add it to current one
	//THis is needed inside loadStory() because we can pass a different video via URL like ?video=<VIDEO>
	$('ul.videosuccess li a').removeClass('active');
	$(obj).addClass('active');
	var currentVidLink = $(obj).attr('href');
	var id = $(obj).attr('id');
	var currentVidTitle = jsonList.story[id].title;
	var currentVidDesc =  jsonList.story[id].desc;
	var currentVidBody =  jsonList.story[id].body;
	if(currentVidTitle == '') {
		$("#jwVideoTitle").html('');
	} else {
		$("#jwVideoTitle").html(currentVidTitle);
	}
	if(currentVidDesc == '') {
		$("#jwVideoDesc").html('');
	} else {
		$("#jwVideoDesc").html(currentVidDesc);
	}
	if(currentVidBody == '') {
		$("#jwVideoBody").html('');
	} else {
		$("#jwVideoBody").html(currentVidBody);
	}
	if((currentVidLink == '') || (currentVidLink == 'http://')) {
		jwplayer('player').stop();
		$("#player_wrapper").css("display","none");
	} else {
		$("#player_wrapper").css("display","");
		loadJwPlayer('player',currentVidLink);
	}
	
	//Go to Anchor above player
	
    var new_position = $('#player').offset();
    window.scrollTo(new_position.left,new_position.top);
}
function loadJwPlayer(id,currentVidLink) {
	var fileJwSwf = "/sites/all/libraries/jwplayer/player.swf";

	jwplayer(id).setup({
		flashplayer: fileJwSwf,
		file: currentVidLink,
		autostart: true,
		width: 600,
		height: 350,
		stretching : 'exactfit',
		controlbar: 'over',
		image: currentVidLink.replace(/\.(flv|mov)$/,'.jpg')
	});
}





