
// main script for sayagain v2

var slideIndex=-1;
var slideLinks=[], slideTitle=[], slideImages=[], slideSection=[], slideBody=[];
var numSliders=0;

$(document).ready(function(){
						   
	if ($.browser.msie && /MSIE 6\./.test(navigator.userAgent)) // test for IE6
	{
		$('div#logoTop').css("background-image", "url(images/logo_top.gif)");
		$('div#logoBottom').css("background-image", "url(images/logo_bottom.gif)");
		$("div#bubble").css("background-image", "url(images/bubble.gif)");
		// remove background imagery from side buttons in IE6
		$("div[id^='button']").css("backround-image","none");
		
		$("img.sideButton").each(function(){
			$(this).supersleight( { shim: "images/blank.gif" } ); // fixes IE6 png issues
			});
	}
	
	if ($.browser.msie) // test for any IE
	{
		// also fix the sliding background IE bug (go IE!  you're the greatest!)
		$("div#sideMainBar img.sideButton").each(function(){
			$(this).stop().css("background-position", "0% 50%").animate( { backgroundPosition: "(100% 50%)" }, 0 );
		});
	}
	
	// set main content div width to appropriate amount
	var w = $(window).width() - 225;
	$("div#content2").css("width", w+"px");
	
	// option rollovers
	$("div[id^='button']").each(function(){
		$(this).mouseover(function(){
			$(this).css("background-position", "left bottom");
		});
		$(this).mouseout(function(){
			$(this).css("background-position", "left top");
		});
	});
	
	// store data for menus and other buttons into logo for easy retrieval
	var bod = $("body");
	bod.data("buttonIllustration", { title: "Picture Galleries",
		 body: "View the four galleries of images - there are galleries for Illustrations, Silly pictures, Photo-manipulations, and Cute pictures"} );
	bod.data("buttonSilly", { title: "Silly and Cute Images",
		 body: "View a gallery of pictures that are cute, fluffy, and often very silly"} );
	bod.data("buttonWord", { title: "Word Nerd Corner",
		 body: "Written language humour section, with areas for funny phrase origins, silly celebrity obituaries, and other word-based jocularity"} );
	bod.data("buttonFurballs", { title: "Furballs Comic",
		 body: "A webcomic with no particular theme or direction, just silly jokes"} );
	bod.data("buttonFun", { title: "Fun Stuff",
		 body: "Fun stuff to do and play.  Here you'll find fun interactive stuff that doesn't fit in any of the other categories"} );
	
	// footer option bubble pop ups
	bod.data("footerHome", { title: "Go To Homepage",
		 body: "Return to the main page for Say Again"} );
	bod.data("footerAbout", { title: "About Say Again",
		 body: "View information about the site, it's reason for being, and other fun facts, fact-seekers!"} );
	bod.data("footerContact", { title: "Contact Details",
		 body: "Visit the contact page to email the site admin, or to fill out a feeedback form"} );
	bod.data("footerHelp", { title: "Help and FAQ",
		 body: "Visit the Frequently Asked Questions, or view other helpful topics about Say Again"} );
	bod.data("footerSiteMap", { title: "Site Map",
		 body: "See the Say Again pages in a quick and simple layout"} );
	// set data for all possible sidebar buttons here
	bod.data("origins", { title: "Origins of the Speeches",
		 body: "Funny or silly histories of how certain phrases and figure of speeches came into being"} );
	bod.data("celebs", { title: "Celebrity Obituaries",
		 body: "Funny or silly fake celebrity obituaries, and how they died"} );
	bod.data("analogies", { title: "Like a... thingy",
		 body: "Funny or silly analogies, similies, or metaphors"} );
	bod.data("illustrations", { title: "Illustrations Gallery",
		 body: "A gallery of illustrations, sketches, and digital paintings"} );
	bod.data("photoEdits", { title: "Photo-Edit Gallery",
		 body: "A gallery of photo manipulations and funky effects"} );
	bod.data("silly", { title: "Silly Pictures",
		 body: "A gallery of silly, sometimes funny, pictures and animations"} );
	bod.data("cute", { title: "Cute Pictures",
		 body: "A gallery of cute pictures and animations to make you go awwwww"} );
	bod.data("furballs", { title: "Furballs Web-Comic",
		 body: "A web-comic with no direction and no theme.  It's like a comedy sketch show in picture form!"} );
	bod.data("newOrigins", { title: "Add a New Phrase",
		 body: "Add a new figure of speech or colloquialism to Origin of the Speeches.  Create your own funny origin for the phrase, and allow other people to add theirs too"} );
	bod.data("newCeleb", { title: "Add a New Celbrity",
		 body: "Add a new Celebrity to the Obituaries.  Create your own funny demise for the celebrity, and allow other people to add theirs too"} );
	bod.data("newAnalogy", { title: "Add an Analogy / Similie",
		 body: "Add a new analogy / metaphor / similie to Like a Thingy.  Create your own funny similie for the phrase, and allow other people to add theirs too"} );
	
	SetSelected(); // sets current page button
	
	SetBubbleHovers(); // set button hover/bubble popups
	
	SetQuestionSlides(); // set slide events for question/answer pages
	
	setTimeout("NewSlidingContent();", 1500); // start the sliding content div moving
	
	// hover opacity for section buttons/titles
	$("div#sectionBlock a img").hover(
	    function(){
	  		$(this).css("opacity", "0.5");
	    }, function(){
			$(this).css("opacity", "1.0");
	  	}
	);
	
	// preload slider images
	if (numSliders > 0)
	{
		for (var i=0; i!=numSliders; ++i)
		{
			img = new Image();
			img.src = slideImages[i];
		}
	}
});

// set the main div size on refresh
$(window).resize(function(){
	var w = $(window).width() - 225;
	$("div#content2").css("width", w+"px");
});

function SetBubbleHovers()
{
	// set main menu hovers
	$("div#headerMenu div[id^='button']").each(function(){
		$(this).mouseenter(function(evt){
			ShowBubble("top", $(this).attr("id"), evt);
		});
		$(this).mouseleave(function(evt){
			HideBubble();
		});
	});
	
	// set footer hovers
	$("div#bottom>a[id^='footer']").each(function(){
		$(this).mouseenter(function(evt){
			ShowBubble("bottom", $(this).attr("id"), evt);
		});
		$(this).mouseleave(function(evt){
			HideBubble();
		});
	});
	
	// set sidebar hovers (set all in this script, keeps it more centralized)
	$("img.sideButton").each(function(){
		var i = $(this).parent().parent(); // double parent fetch because images are inside anchors inside the divs
		i.mouseenter(function(evt){
			ShowBubble("left", i.attr("id"), evt);
			SlideSideButtonOut($(this));
		});
		i.mouseleave(function(evt){
			HideBubble();
			SlideSideButtonIn($(this));
		});
	} );
}

function SetSelected()
{
	if (typeof(window['page']) == "undefined")
		return;
	if (page=="gallery")
	{
		$("div#buttonIllustration").css("background-position", "left bottom").unbind("mouseout");
	}
	else if (page=="silly")
	{
		$("div#buttonSilly").css("background-position", "left bottom").unbind("mouseout");
		// special case sillyCute due to wierd IE bug
		if ($.browser.msie)
		{
			$("div#contentBottomBar").css("top", $(this).position().top + 75 + "px");
		}
	}
	else if (page=="word")
	{
		$("div#buttonWord").css("background-position", "left bottom").unbind("mouseout");
	}
	else if (page=="furballs")
	{
		$("div#buttonFurballs").css("background-position", "left bottom").unbind("mouseout");
	}
	else if (page=="fun")
	{
		$("div#buttonFun").css("background-position", "left bottom").unbind("mouseout");
	}
}

function SlideContent()
{
	$("div#slidingContent").stop().animate({ top: "5px"}, 1500).animate({ top: "5px"}, 5000).animate({ top: "160px"}, 1500);
	setTimeout("NewSlidingContent()", 8200);
}

function NewSlidingContent()
{
	// tbd: make contents of sliding div change in cyclic fashion
	if (numSliders==0)
		return; // don't slide if no content
	
	slideIndex++;
	
	if (slideIndex>=numSliders)
		slideIndex=0;
	
	//$("div#slidingContent").html(sliderArray[slideIndex]);
		
	if (slideLinks[slideIndex].indexOf("showID")!=-1)
	{
		$("div#slidingContainer").html("<div id='slidingContent'><h2>Latest "+slideSection[slideIndex]+":<br><br>"+
			slideTitle[slideIndex]+"</h2><br>"+slideBody[slideIndex]+"</div>");
	}
	else
	{
		$("div#slidingContainer").html("<div id='slidingContent'><h3>Latest "+slideSection[slideIndex]+":<br>"+
			slideTitle[slideIndex]+"</h3></div>");
		$("div#slidingContent").css("background-image", "url("+slideImages[slideIndex]+")");
	}
	// set sliding content div clickability
	$("div#slidingContent").click(function(){
		window.location = slideLinks[slideIndex];
	}).mouseenter(function(){
		//$(this).css( "opacity", "0.65" );
		$(this).css( "color", "#fff" );
	}).mouseleave(function(){
		//$(this).css("opacity", "1.0");
		$(this).css("color", "#000");
	});
	
	//$("div#slidingContiner").html(sliderArray[slideIndex]);
	
	// slide the new content
	SlideContent();
}

function ShowBubble(pos, id, evt)
{
	var x = 0;
	var y = 0;
	var title = "";
	var text="";
	
	if (pos=="left")
	{
		x = 160;
		y = evt.pageY;
	}
	else if (pos=="top")
	{
		y = $("div#headerMenu").offset().top + 30;
		x = evt.pageX-100;
	}
	else if (pos=="bottom")
	{
		y = $("div#bottom").offset().top - 120;
		x = evt.pageX-100;
	}
	
	if (x+200 > $(window).width())
		x = $(window).width()-200;
	
	title = $("body").data(id).title;
	text = $("body").data(id).body;
	
	$("div#bubble").html("<div id='bubbleTitle'>"+title+"</div><br><div id='bubbleBody'>"+text+"</div>")
		.css( { left: x+"px", top: y+"px" } ).show();
}

function HideBubble()
{
	$("div#bubble").hide().css( { left: "-500px", top: "0px" } );
}

function SlideSideButtonOut(i)
{
	$("img.sideButton",i).stop().animate( { backgroundPosition: "(0% 50%)" }, 500 );
}

function SlideSideButtonIn(i)
{
	$("img.sideButton",i).stop().animate( { backgroundPosition: "(100% 50%)" }, 500 );
}

function SetQuestionSlides()
{
	$("div#question").each(function(){		
		var a = $("div#answer", $(this));
		if (a.size()>0)
		{
			a.hide();
			$("span", $(this)).click(function(){
				if (a.is(":visible"))
				{
					a.slideUp(500);
				}
				else
				{
					$("div#question div#answer").each(function(){
						if ($(this).is(":visible"))
							$(this).slideUp(500);
					 });
					a.stop().slideDown(500);
				}
			}).mouseenter(function(){
				$(this).css( { color:"#04a", cursor: "pointer" });
			}).mouseleave(function(){
				$(this).css( { color:"#000", cursor: "default" });
			});
		}
	});
}

function LogOut()
{
	// log out of user session
	$.post("bin/LoginSystem.php?action=logout", function(){
		if (window.location.href.indexOf("/byoa/")!=-1)
			$("div#userDiv").html("You are now logged out.  Would you like to <a href='../login.php'>login</a>?");
		else
			$("div#userDiv").html("You are now logged out.  Would you like to <a href='login.php'>login</a>?");
	});
	return false; // stop link click from going any further
}

function LogInReturn()
{
	if (window.history.length>2)
		window.history.go(-2);
	else
		window.history.back();
}


// supersleight function by Drew McLellan
jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};