// Custom javascript for FunXion
// Developed by TheMouseArtist.com



$(function() {
	$('#mycarousel').jcarousel({
		scroll: 1,
		auto: 6,
		wrap: 'last',
		initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null

	});
   $('#pager a').click(function(){
		//idx=$('#pager a').index(this);
		$('#pager a').removeClass("selected");
		$(this).addClass('selected');
		return false;
	});
   
   $('a.fb').fancybox({
				'frameWidth': 830,
				'frameHeight': 550,
				'overlayOpacity': 0.8,
				'overlayColor': '#000'
					  });
   
   $("ul#ticker01").liScroll({travelocity: 0.05});	   
		   
		   
		   
	var cIsDown = false;
	var dIsDown = false;
	
	$("a.contact").click(function() {
			var drop = $("div.contact-drop");
			
			var origH = drop.css("top");
			var finalH = origH.substr(0,3);
			finalH = finalH * 1;
			finalH = finalH + 100;
			//alert(finalH);
			if(cIsDown === false) {
				if(dIsDown === true) {
					dysfunxionUp(525);
					contactDown(finalH);
				} else {
				contactDown(finalH);
				}
			} else {
				contactUp(finalH);
			}
			
			
	return false;			
	});
	
	function contactUp(finalH) {
			$("div.contact-drop").stop().animate({top: finalH - 200},1000);
			cIsDown = false;
			$("span#ctree").html("+");
	}
	function contactDown(finalH) {
			$("div.contact-drop").stop().animate({top: finalH},1000);
			cIsDown = true;
			$("span#ctree").html("-");
	}
	$("a.dysfunxion").click(function() {
			var drop = $("div.dysfunxion-drop");
			
			var origH = drop.css("top");
			var finalH = origH.substr(0,3);
			finalH = finalH * 1;
			finalH = finalH + 150;
			//alert(finalH);
			if(dIsDown === false) {
				if(cIsDown === true) {
					contactUp(475);
					dysfunxionDown(finalH);
				} else {
					dysfunxionDown(finalH);
				}
			} else {
				dysfunxionUp(finalH);
			}
			return false;
			});
	
	function dysfunxionUp(finalH) {
			$("div.dysfunxion-drop").animate({top: finalH - 300},1000);
			dIsDown = false;
			$("span#dtree").html("+");
	}
	
	function dysfunxionDown(finalH) {
			$("div.dysfunxion-drop").animate({top: finalH},1000);
			dIsDown = true;
			$("span#dtree").html("-");
	}
	
	
	
	$("#parallax").jparallax({});
	//outer();
	
	getSearch();
	
	
	//Calendar scripts
	
	$('div.calendar').load('includes/calendar.php');
	
	$('div.calendar a').click(function() {
		var href = $(this).attr('href');
		$('div.calendar').load('includes/calendar.php', href);
		return false;
	});
	
	
	
	$('div#yt-cats a').click(function() {
					$('div#yt-cats a').removeClass('bold');
					$(this).addClass('bold');
					loadPlaylist($(this).attr('href'));
					
					return false;
					});
});


function getCal(month,year) {
	$('div.calendar').fadeOut("fast");
	$.get("includes/calendar.php", {"month":month, "year":year}, showCal, "html");
}

function showCal(result) {
	$('div.calendar').html(result);
	$('div.calendar').fadeIn("fast");
}

function getEvent(eid) {
	$('div.eventlist').fadeOut("fast");
	$.get("includes/event.php", {id: eid}, showEvent, "html");
}

function showEvent(result) {
	$('div.eventlist').html(result);
	$('div.eventlist').fadeIn("fast");
}

function viewBox(linkID, doc_width, doc_height) {
				
				//if IE
				if($.browser.msie) {
					//expanded the window
					doc_width = doc_width;
					doc_height = doc_height;
				}
				
				//trigger the window
				$("a#"+ String(linkID)).fancybox({'frameWidth': 780,'frameHeight':
	550, 'overlayOpacity': 0.8, 'overlayColor': '#000'	}).trigger('click');
			}


function mycarousel_initCallback(carousel) {
    jQuery('#pager a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
}


function init() {
	//setHeader();
	loadVideo();
	loadPlaylist();
	return false;
}

function getTheDate() {
	var d = new Date();
	
	var m_names = new Array("jan", "feb", "mar", 
"apr", "may", "jun", "jul", "aug", "sep", 
"oct", "nov", "dec");
	
	var month = d.getMonth();
	
	var output = m_names[month] + " " + d.getDate() + ", " + d.getFullYear();

	
	return output;
}

function setHeader() {
	var d = getTheDate();
	var day = new Date();
	document.getElementById('date').innerHTML = "<span class='date1'>" + d + "</span> | Day " + day.getDate() + " of a more funxional world";
	return false;
}

var timer;

function getSearch()
{
clearTimeout(timer);
var results;
var theQuery = "#ifunxion";
 
	$.post("includes/twitter.php", {query: theQuery},  function(xml){
		$('entry',xml).each(function(i){
			var title = $(this).find("title").text();
			var image = $(this).find("link:last").attr('href');
			var pub =  $(this).find("published").text();
			results = "<img src='"+image+"' width='48' height='48' /> <p>" + title + "</p><br style='clear:both' />";
			});
		var entry = $("<div />").addClass('entry');
		entry.append(results).hide();
		$("#twitters").append(entry);
		$(".entry").fadeIn();
		if($('.entry').size() > 2) {
		$(".entry:first").slideUp("normal", function(){ $(this).remove(); });
		}
	});
//console.log("call");
timer = setTimeout('getSearch()', 60000);
}

function getYoutube()
{
//clearTimeout(timer);
var results;
var theQuery = "@infunxion";
 
	$.post("http://gdata.youtube.com/feeds/api/users/alexss3/uploads", null,  function(xml){
		$('entry',xml).each(function(i){
			var title = $(this).find("title").text();
			var image = $(this).find("link:last").attr('href');
			var pub =  $(this).find("published").text();
			results = "<img src='"+image+"' width='48' height='48' /> <p>" + title + "</p><br style='clear:both' />";
			});
		var entry = $("<div />").addClass('entry');
		entry.append(results).hide();
		$("#twitters").append(entry);
		$(".entry").fadeIn();
		if($('.entry').size() > 2) {
		$(".entry:first").slideUp("normal", function(){ $(this).remove(); });
		}
	});
//console.log("call");
//timer = setTimeout('getSearch()', 60000);
}

function loadVideo() {
	var vidURL = 'http://www.youtube.com/v/wxddsEPkXLM&amp;hl=en_US&amp;fs=1&amp;';
	var video = '<object width="390" height="240"><param name="movie" value="'+vidURL+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always" /><param name="wmode" value="transparent"></param><embed src="'+vidURL+'" width="390" height="240" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" movie="'+vidURL+'" wmode="transparent"></embed></object>';	
	$('div#video_port').html(video);	
}

function changeVideo($pappa) {
	var $port = $('div#video_port');
	var vidURL = $pappa;
	var video = '<object width="390" height="240"><param name="movie" value="http://www.youtube.com/v/'+vidURL+'&autoplay=1&amp;hl=en_US&amp;fs=1&amp;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"><embed src="http://www.youtube.com/v/'+vidURL+'&autoplay=1&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="390" height="240" wmode="transparent"></embed></object>';	
	
	$port.html(video);
	return false;
	
}

function loadPlaylist(user) {
	if(user == null) {
		user = 'hbo';
		$('div#yt-cats a:first').addClass('bold');
	}
	if(user != null) {
		$("#hold-videos").html('Loading...');
	}
	$("#hold-videos").load("includes/youtube_api.php", {'user': user}, function() {
					$('div.yt-item a').click(function() {
					
					changeVideo($(this).attr('rel'));
					//console.log($(this).attr('rel'));
					
					return false;
										  });
																				});
	
}
