function enable_drag(){
	if(BrowserDetect.browser == "Explorer"){
		
		dd = new YAHOO.util.DDProxy("liger", "default", {
			centerFrame: true 
		});
		dd.startDrag = function(x,y) {

		}
		dd.unlock();
	}else {
		$("body.widget div#liger").draggable("enable");
		$("body.widget div#liger").draggable({ cursorAt: { cursor: 'move', top: 170, left: 160 }, cancel: "#yt_vid", opacity: 0.7, handle: '#drag_surface', cursor: 'move', stop: function(){
			$("#meta .left").text($("#liger").css("left"));$("#meta .top").text($("#liger").css("top"));
		}});
	}
}
function disable_drag(){
	if(BrowserDetect.browser == "Explorer"){
		dd.lock();
	}else {
		$("div#liger").draggable("disable");
	}
}
function adjust_width(){
	var name = $("div.barNames").width();
	var menu = $("div#menu").width();
	var advert = $("div#advertising").width();
	var controls = $("div#controls").width();
	var total = name+menu+advert+controls;
	var newTotal = total + 105;
	var winWidth = $(window).width();
	var widthTest = $('body').hasClass("tray");

	if (winWidth < newTotal && widthTest){
		$("div#liger,#external").css("width", newTotal); 
	}else { $("div#liger,#external").css("width", "100%");}
	
}
function resize_height() {
	var height = $(window).height() - 4;	 
	$("#external").height(height);			
}
function adjust_tray(){
  $("#liger").css("top", "auto").css("left", "auto");  
}
function adjust_widget(){
	  $("#liger").css("top", $("#meta .top").text()).css("left", $("#meta .left").text());
	}

	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		  },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
};
BrowserDetect.init();

$(document).ready(function() {
	resize_height();
	adjust_width();
	$(window).resize(resize_height);
	$(window).resize(adjust_width);

	$('#twitter_link').click(function(){
		window.open("http://twitter.com/home?status=I'm browsing the "+$(this).attr('name')+" GummyJungle widget, and thought you would like it. "+$(this).attr('href'),'Twitter','toolbar=0,status=0,resizable=1,width=800,height=500');
		return false;
	});
	$('#email_link').click(function(){
		window.location.href = 'mailto:?subject=Check out the '+$(this).attr('name')+' GummyJungle widget!%20&body=Hi there. I\'m browsing the sites in the '+$(this).attr('name')+' GummyJungle Widget, and thought you would like them. '+$(this).attr('href')+' %0D%0APowered by GummyJungle.';	
		return false;
	});
	
	$('#share_box .close').click(function(){
		$('#share_box').hide();
	});
	$('#email_box .close').click(function(){
		$('#email_box').hide();
	});

	$('#embedLink').click(function(){
		$('#embed_box').load('embed.php').show();	
	});
	$('#shareLink').click(function(){
		$('#share_box').show();	
	});

	$("body a.min").click(function(){	
		$("body").toggleClass("widget");
		$("body").toggleClass("tray");
	
		if($('body').hasClass("tray")){
			$.post('set_session.php', {type:"tray"});
			resize_height();
			adjust_width();
			adjust_tray();
			disable_drag();
		}
		else {
			$.post('set_session.php', {type:"widget"});
			resize_height();
			adjust_width();		
			adjust_widget();
			enable_drag();
			//$("#liger").draggable("enable");
			
		}
	});
	
	function animate() {
		var img = $("div.two img:eq(1)");
		$(img).animate({opacity: 1.0}, 5000).fadeOut("slow", function(){
			$(img).animate({opactity: 1.0}, 5000).fadeIn("slow", function(){
				animate();
			});
		});
	}
	animate();
	
	function animate2() {
		var img = $("div#advertising.two img:eq(1)");
		$(img).animate({opacity: 1.0}, 5000).fadeOut("slow", function(){
			$(img).animate({opactity: 1.0}, 5000).fadeIn("slow", function(){
				animate2();
			});
		});
	}
	animate2();

	$(".carousel").jCarouselLite({auto: 5000, speed: 2000});

	if ($('body').hasClass("tray")){
		$(".bigimages.carousel").css("text-indent","-9000px");
		$("body a.min").attr('title', 'Expand');
	} else { 
		$("body a.min").attr('title', 'Minimize');
	}

	var curUrl = $("div#meta a").attr("title");
	$("div#controls a.close").click( function() {
		window.location=curUrl;
		return false;
	});

	$("#close a").click(function(){
		$("#embed_box").hide();
	});

	enable_drag();

	$("li.image").css("width", "auto");
	

	$(".bigimages.carousel").css("width", "279px");
	$("body.tray #liger, #liger.embeddable").draggable("disable");
	
	$("a.morenav, ul.more_btns").hover(function(){
		$("ul.more_btns").show();
	}, function(){$("ul.more_btns").hide();})

});