function removeMe(id) {$("#header").css({"background":"transparent url(http://images.jayisgames.com/robotwantsstatic-error.png) no-repeat bottom left"}); removeID(id);}
function removeID(id) {$('#'+id).remove();}
$(document).ready(function(){
	var style = ($.cookie('style'))? $.cookie('style'):"robotwants";
	switch(style) {
		case "oblea":
			$("#banner-link").append('<a href="http://joblea.webs.com/" target="_blank"><span><img alt="Visit the Jay Oblea website" src="/mt4/mt-static/themes/oblea/hotspot.gif" /></span></a>');
			break;
		case "paulstridenoble":
			$("#banner-link").append('<a href="http://www.s-nd.com/" target="_blank"><span><img alt="Visit the Paul Stride-Noble website" src="/mt4/mt-static/themes/paulstridenoble/hotspot.gif" /></span></a>');
			break;
		case "roballen1":
			$("input[name='cx']").attr('value','partner-pub-9626478286810156:2802wq-4emm');
			break;
		case "robotwants":
			if(swfobject.ua.pv[0]>=10) {
				$("#header").mouseover(function() {$(this).css('cursor', 'pointer');});
				$("#header").one('click',function() {
					if($.cookie('CmtrID') && $.cookie('CmtrID') > 0) robotWantsBanner({id:$.cookie('CmtrID')});
					else $.getJSON('http://jayisgames.com/mt4/ct.cgi?__mode=cmtrid',robotWantsBanner);
				});
				$("#header").css({"background":"transparent url(http://images.jayisgames.com/robotwantsstatic-start.png) no-repeat bottom left"});
			}
			break;
		default: //do nothing
	}
	function robotWantsBanner(x) {
		$.cookie('CmtrID',x.id,{path: '/'});
		var movie = '/images/robotwantsjig_locked_v1.swf';
		var n = (x.id!=-1)? x.id:0;
		movie += ('?id='+n);
		var swf = movie+'&walkthrough';
		var flashvars = {};
		var params = (navigator.appName.indexOf("Microsoft") != -1 || navigator.userAgent.indexOf("Linux") != -1)? {allowscriptaccess: "sameDomain", quality: "high"}:{allowscriptaccess: "sameDomain", quality: "high"};
		var attributes = {};
		swfobject.embedSWF(swf, 'flashgame', "960", "150", '10.1', 'http://jayisgames.com/games/expressInstall.swf', flashvars, params, attributes);
		setTimeout(function(){$("#flashgame").tabIndex=0; $("#flashgame").focus();},1500);
	}
	$("#tagsearch-form input[name='sort']").click(function() {
		if($(this).attr("value") === "date") {
			var loc = 'http://jayisgames.com/tag/'+$("input[name='tag']").attr("value");
			window.location = loc;
		} else {
			var loc = 'http://jayisgames.com/tag/'+$("input[name='tag']").attr("value")+'/rating';
			window.location = loc;
		}
	});
	$("#walkthroughs-form input[name='sort']").click(function() {
		if($(this).attr("value") === "date") {
			var loc = 'http://jayisgames.com/walkthroughs/';
			window.location = loc;
		} else {
			var loc = 'http://jayisgames.com/walkthroughs/a';
			window.location = loc;
		}
	});
	if($("#sort_form").css("display") == "none") $("#sort_form").slideDown("slow");
	$("a.addGame").each(function(i){
		$(this).get(0).onclick = null;
   		$(this).click(function() {
	 		if($("div.Gamecode"+i).css("display") == "none") $("div.Gamecode"+i).slideDown("slow");
			else $("div.Gamecode"+i).slideUp("slow");
			return false;
		});
 	});
	$("#banner-link img").hover(
      function () {
        $(this).attr('src','/mt4/mt-static/themes/'+style+'/hover.gif');
      }, 
      function () {
        $(this).attr('src','/mt4/mt-static/themes/'+style+'/hotspot.gif');
      }
    );

	$(".comment a").not($(".comment-update-link a")).attr('target', '_blank');
	$(".comment-content a[href*='/images/']").addClass('aptureEnhance');
	$(".comment-content a[href*='images.jayisgames.com']").addClass('aptureEnhance');
	$("#walkthrough-notes").slideUp("slow");

	$("a.addJIGame").click(function() {
		if($("div.JIGamecode").css("display") == "none") $("div.JIGamecode").slideDown("slow");
		else $("div.JIGamecode").slideUp("slow");
		return false;
	});
	$("a.toggle-recommended").click(function() {
 		if($("span.icons-recommended").css("display") == "none") {
			$("span.icons-toprated").hide();
			$.get("/icons_sql.php", function(data){
			  $("span.icons-recommended").html(data);
			});
			$("span.icons-recommended").show();
			$(this).css({"text-decoration":"underline"});
			$("a.toggle-toprated").css({"text-decoration":"none"});
			$.cookie('panelpref','recommend',{expires: 30, path: '/'});
		} else {
			// refresh recommended list
			$.get("/icons_sql.php", function(data){
			  $("span.icons-recommended").html(data);
			});
		}
		return false;
	});
	$("a.toggle-toprated").click(function() {
 		if($("span.icons-toprated").css("display") == "none") {
			$("span.icons-recommended").hide();
			$("span.icons-toprated").show();
			$(this).css({"text-decoration":"underline"});
			$("a.toggle-recommended").css({"text-decoration":"none"});
			$.cookie('panelpref','toprated',{expires: 30, path: '/'});
		}
		return false;
	});
	if($.cookie('panelpref') && $.cookie('panelpref') == "recommend")
		$("a.toggle-recommended").css({"text-decoration":"underline"});
	else
		$("a.toggle-toprated").css({"text-decoration":"underline"});
	$('a[href*="casualgameplay.com"]').removeAttr('target');
	$('.comment a[href*="#walkthrough"]').removeAttr('target');
	var lights = true;
	function toggleLights() {
		if(lights) {
			$('#game-wrapper').css({'z-index':1001});
			$('#game-social-right').css({'z-index':1001});
			$('#mask').css({'width':$(window).width(),'height':$(document).height()}); 
			$('#mask').fadeIn(1000);
		} else {
			$('#mask').fadeOut(1000, function() {
				$('#game-wrapper').css({'z-index':0});
				$('#game-social-right').css({'z-index':0});
  			});
		}
		lights = !lights;
	}
	$("#lights-button").click(function() {
		toggleLights();
		return false;
	});
	(
		function() {
			var
				// options
				animationTime = 1500,
				intervalTime = 15000,
				panelsOnClass = 'panel-on',
				panelsOffsetData = 'feature-offset',
				$container = $('.feature'),
				$controlsContainer = $('#panel-buttons', $container),
				$panels = $('.new-games-panel', $container),
				getNextDirectionClick =
					function(lastDirection) {
						return (lastDirection + 1) % 4;
					},
				getNextDirectionAuto =
					function(lastDirection) {
						return Math.floor(Math.random() * 4);
					},

				// book-keeping variables
				animation = false,
				interval = null,
				containerHeight = $container.height(),
				containerWidth = $container.width(),
				$controls = $('a', $controlsContainer),
				panelsCurrent = 0,
				lastDirection = 0,
				panelsTotal = $panels.length,
				featureCycle = Math.floor(Math.random()*8)+(panelsTotal),

				// helper functions
				slideTo =
					function(i, options) {
						if (animation || panelsCurrent == i) return;
						animation = true;
						var
							opt = { direction: 4 },
							o = $.extend({}, opt, options),
							$panelCurrent = $panels.eq(panelsCurrent),
							$panelNext = $panels.eq(i),
							animationObject = {},
							cssObject = {};

						$controls
							.eq(i)
								.addClass(panelsOnClass)
							.end()
							.eq(panelsCurrent)
								.removeClass(panelsOnClass)
							.end();

						switch (o.direction) {
							case 'up':
							case 0:
								animationObject = {top: '-=' + containerHeight};
								cssObject = {left: $panelNext.data(panelsOffsetData), top: containerHeight};
								break;
							case 'right':
							case 1:
								animationObject = {left: '+=' + containerWidth};
								cssObject = {left: -containerWidth + $panelNext.data(panelsOffsetData), top: 0};
								break;
							case 'down':
							case 2:
								animationObject = {top: '+=' + containerHeight};
								cssObject = {left: $panelNext.data(panelsOffsetData), top: -containerHeight};
								break;
							case 'left':
							case 3:
							default:
								animationObject = {left: '-=' + containerWidth};
								cssObject = {left: containerWidth + $panelNext.data(panelsOffsetData), top: 0};
								break;
						}
						$panelCurrent.animate(animationObject, animationTime);
						$panelNext
							.css(cssObject)
							.animate(
								animationObject,
								animationTime,
								function() {
									panelsCurrent = i;
									animation = false;
									lastDirection = o.direction;
								}
							);
					},
				slideToNext =
					function() {
						slideTo((panelsCurrent+1) % panelsTotal, { direction: getNextDirectionAuto(lastDirection) });
						if(featureCycle--<=0) {
							clearInterval(interval);
							interval = null;
						}
					},
				startInterval =
					function() {
						slideToNext();
						interval = setInterval(slideToNext, intervalTime);
					};

			// initialising behavior
			$panels
				.each(
					function(i, panel) {
						var
							$panel = $(panel),
							offset = -$panel.position().left;
						$panel.data(panelsOffsetData, offset);
					}
				);

			$controlsContainer
				.bind(
					'click',
					function(e) {
						var $target = $(e.target);
						if ($target.is('a')) {
							clearInterval(interval);
							interval = null;
							slideTo($controls.index($target), { direction: getNextDirectionClick(lastDirection) });
							return false;
						}
					}
				);
			timeout = setTimeout(startInterval,1500);
		}
	)();
});

