/*
	File: anja.js
	Purpose: Anja Pärson - Official website
	Author: David Södermark
	Copyright: 2009 Metacue IT Innovation
*/

var base_url = "http://www.anjapaerson.com/";
var mediaThumbs = new Array();
var mediaLarges = new Array();
var mediaDescriptions = new Array();
var mediaPages = 0;       // Total number of media pages.
var mediaPageLoaded = 0;  // Number of media pages loaded into browser.
var mediaPageShowing = 0; // Media page that is currently showing in the media menu.
var mediaOrigLeft = 40;   // Original left position of the wide media content div.
var mediaLeft = 0;        //
var box;
var eventDate = '';
var eventPlace = '';
var eventCountryShort = '';
var eventCountryEn = '';
var eventCountrySv = '';
var eventDisciplineEn = '';
var eventDisciplineSv = '';
var eventMapX = 0;
var eventMapY = 0;
var eventTextEn = '';
var eventTextSv = '';

jQuery(document).ready(function(){
	/*jQuery("body").append('<div id="menu"><img src="images/navigation_menu.png" width="993px" height="240px" border="0" /></div>');
	jQuery("body").append('<div id="media_menu"></div>');*/

	positionMenus();
	jQuery(window).resize(positionMenus);

	jQuery("#upper_toggle").add("#media_toggle")
		.css("background-color", "#fff")
		.css("opacity", "0")
		.css("filter", "alpha(opacity=0)");

	// Upper menu.
	/*jQuery("#upper_toggle").click(function(){
		if (jQuery("#upper_nav").css("top") == "0px"){
			jQuery("#upper_nav").animate({"top": "-64px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
		}else{
			jQuery("#upper_nav").animate({"top": "0px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
		}
	});*/
	jQuery("#upper_nav").hover(function(){
			jQuery("#upper_nav").animate({"top": "0px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
		}, function(){
			jQuery("#upper_nav").animate({"top": "-64px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
	});
	/*jQuery("#upper_toggle").hover(function(){
			jQuery(this).css("cursor", "pointer").css("opacity", ".05").css("filter", "alpha(opacity=5)");
		}, function(){
			jQuery(this).css("cursor", "default").css("opacity", "0").css("filter", "alpha(opacity=0)");
	});*/

	// Media menu.
	jQuery("#media_toggle").click(function(){
		if (jQuery("#media_nav").css("bottom") == "0px"){
			jQuery("#media_nav").animate({"bottom": "-199px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
		}else{
			jQuery("#media_nav").animate({"bottom": "0px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
		}
	});
	/*jQuery("#media_nav").hover(function(){
			jQuery("#media_nav").animate({"bottom": "0px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
		}, function(){
			jQuery("#media_nav").animate({"bottom": "-199px"}, {"duration": 500, "easing": "easeOutExpo", "queue": false});
	});*/
	jQuery("#media_toggle").hover(function(){
			jQuery(this).css("cursor", "pointer").css("opacity", ".05").css("filter", "alpha(opacity=5)");
		}, function(){
			jQuery(this).css("cursor", "default").css("opacity", "0").css("filter", "alpha(opacity=0)");
	});

	// TODO: Fix png and transparency in IE 6.
	fixpng();

	// Place on earth.

	jQuery("#placeonearth_leftnav").click(function(){
		jQuery.ajax({url: base_url+"get_event/previous/"+eventDate,
			type: "post", dataType: "json",
			success:
				function(data){
					eventCountryShort = data["country_shortname"];
					eventCountryEn = data["country_name_en"];
					eventCountrySv = data["country_name_sv"];
					eventPlace = data["place_name"];
					eventMapX = data["map_x"];
					eventMapY = data["map_y"];
					eventTextEn = data["about_en"];
					eventTextSv = data["about_sv"];
					eventDisciplineEn = data["discipline_name_en"];
					eventDisciplineSv = data["discipline_name_sv"];
					eventDate = data["event_date"];
					// Put info where it belongs.
					var eventLocation = " - "+eventPlace+", "+eventCountryEn;
					var eventDiscipline = "<span>Discipline:</span> "+eventDisciplineEn+"<br />";
					var eventText = "<span>Quick facts:</span> "+eventTextEn+"<br />";
					if (eventPlace == ""){
						eventLocation = "";
						eventDiscipline = "";
						eventText = eventTextEn;
						jQuery("#cross").hide();
					}else{
						jQuery("#cross").show();
					}
					jQuery("#placeonearth_info").html(
						"<h3>"+eventDate+eventLocation+"</h3>"+
						"<p>"+eventDiscipline+eventText+"</p>"
					);
					//jQuery("#cross").css("left", eventMapX+"px").css("top", eventMapY+"px");
					if (jQuery("#cross").is(":visible")){
						jQuery("#cross")
							.animate({"width": "36px", "height": "36px"}, {"duration": 500, "easing": "easeOutQuad"})
							.animate({"width": "6px", "height": "6px", "left": eventMapX+"px", "top": eventMapY+"px"}, {"duration": 500, "easing": "easeInQuad"});
					}
				}
		});
	});
	jQuery("#placeonearth_rightnav").click(function(){
		jQuery.ajax({url: base_url+"get_event/next/"+eventDate,
			type: "post", dataType: "json",
			success:
				function(data){
					eventCountryShort = data["country_shortname"];
					eventCountryEn = data["country_name_en"];
					eventCountrySv = data["country_name_sv"];
					eventPlace = data["place_name"];
					eventMapX = data["map_x"];
					eventMapY = data["map_y"];
					eventTextEn = data["about_en"];
					eventTextSv = data["about_sv"];
					eventDisciplineEn = data["discipline_name_en"];
					eventDisciplineSv = data["discipline_name_sv"];
					eventDate = data["event_date"];
					// Put info where it belongs.
					var eventLocation = " - "+eventPlace+", "+eventCountryEn;
					var eventDiscipline = "<span>Discipline:</span> "+eventDisciplineEn+"<br />";
					var eventText = "<span>Quick facts:</span> "+eventTextEn+"<br />";
					if (eventPlace == ""){
						eventLocation = "";
						eventDiscipline = "";
						eventText = eventTextEn;
						jQuery("#cross").hide();
					}else{
						jQuery("#cross").show();
					}
					jQuery("#placeonearth_info").html(
						"<h3>"+eventDate+eventLocation+"</h3>"+
						"<p>"+eventDiscipline+eventText+"</p>"
					);
					//jQuery("#cross").css("left", eventMapX+"px").css("top", eventMapY+"px");
					if (jQuery("#cross").is(":visible")){
						jQuery("#cross")
							.animate({"width": "36px", "height": "36px"}, {"duration": 500, "easing": "easeOutQuad"})
							.animate({"width": "6px", "height": "6px", "left": eventMapX+"px", "top": eventMapY+"px"}, {"duration": 500, "easing": "easeInQuad"});
					}
				}
		});
	});
	jQuery.ajax({url: base_url+"get_event/current",
		type: "post", dataType: "json",
		success:
			function(data){
				eventCountryShort = data["country_shortname"];
				eventCountryEn = data["country_name_en"];
				eventCountrySv = data["country_name_sv"];
				eventPlace = data["place_name"];
				eventMapX = data["map_x"];
				eventMapY = data["map_y"];
				eventTextEn = data["about_en"];
				eventTextSv = data["about_sv"];
				eventDisciplineEn = data["discipline_name_en"];
				eventDisciplineSv = data["discipline_name_sv"];
				eventDate = data["event_date"];
				// Put info where it belongs.
				var eventLocation = " - "+eventPlace+", "+eventCountryEn;
				var eventDiscipline = "<span>Discipline:</span> "+eventDisciplineEn+"<br />";
				var eventText = "<span>Quick facts:</span> "+eventTextEn+"<br />";
				if (eventPlace == ""){
					eventLocation = "";
					eventDiscipline = "";
					eventText = eventTextEn;
					jQuery("#cross").hide();
				}else{
					jQuery("#cross").show();
				}
				jQuery("#placeonearth_info").html(
					"<h3>"+eventDate+eventLocation+"</h3>"+
					"<p>"+eventDiscipline+eventText+"</p>"
				);
				jQuery("#cross").css("left", eventMapX+"px").css("top", eventMapY+"px");
			}
	});

	// Get media.
	jQuery.ajax({url: base_url+"get_media",
		type: "post", dataType: "json",
		success:
			function(data){
				//mediaPages = data.length;
				jQuery.each(data, function(k,v){
					mediaThumbs.push(data[k]["thumb"]);
					mediaLarges.push(data[k]["original"]);
					mediaDescriptions.push(data[k]["description"]);
					mediaPages++;
				});
				mediaPages = Math.ceil(mediaPages/5);
				if (mediaPages > 1) {
					addMediaPages(2);
				} else if (mediaPages > 0) {
					addMediaPages(1);
				}
				mediaPageShowing = 1;
			}
	});

	// Media arrows.
	jQuery("#leftarrow img").click(function(){
		if (mediaPageShowing > 1){
			mediaPageShowing--;
			jQuery("#media_content").animate({"left": (40-((mediaPageShowing-1)*952))+"px"}, {"duration": 1000, "easing": "easeOutExpo"});
		}
	});
	jQuery("#rightarrow img").click(function(){
		if (mediaPageShowing < mediaPages){
			if ((mediaPageShowing > (mediaPageLoaded - 2)) &&
				(mediaPageLoaded < mediaPages)) {
				addMediaPages(1);
			}
			mediaPageShowing++;
			jQuery("#media_content").animate({"left": (40-((mediaPageShowing-1)*952))+"px"}, {"duration": 1000, "easing": "easeOutExpo"});
		}
	});

	jQuery("table.result_table tbody tr:odd").addClass("results_darkrow"); //css("background-color", "#d3d3d4");
	jQuery("table.result_table tbody tr").hover(function(){
			jQuery(this).addClass("results_hover");
		},function(){
			jQuery(this).removeClass("results_hover");
	});
	jQuery("#results_accordion").accordion({header: "h3", autoHeight: false, active: "none"});

	// Contact form.
	if (jQuery("#contactform").length>0){
		jQuery("#progress").append("<img id=\"loading\" src=\""+base_url+"images/ajax-loader.gif\" alt=\"loading...\" />").hide();
		jQuery("#formmessage").hide();
		jQuery("#changeimage").click(function(){
			changeImage();
		});
		jQuery("#contactform").ajaxForm({
			dataType:	"json",
			beforeSubmit:	function(){
								// Set size for #progress and #formmessage equal to #contactform.
								var contactFormWidth = jQuery("#contactform").width();
								var contactFormHeight = jQuery("#contactform").height();
								jQuery("#progress").css("height",contactFormHeight+"px");
								jQuery("#progress").css("width",contactFormWidth+"px");
								jQuery("#formmessage").css("height",contactFormHeight+"px");
								jQuery("#formmessage").css("width",contactFormWidth+"px");

								// Set position for animated "loading..."-gif.
								var loadingPositionTop = parseInt((contactFormHeight / 2) - 12);
								var loadingPositionLeft = parseInt((contactFormWidth / 2) - 12);
								jQuery("#loading").css("margin-top",loadingPositionTop+"px");
								jQuery("#loading").css("margin-left",loadingPositionLeft+"px");

								// Fade out contactform and show "loading..."-gif.
								jQuery("#contactform").fadeOut(400, function(){
									jQuery("#progress").show();
								});
							},
			success:	function(data,success,matchedset){
							var error = data["error"];
							if(error == ""){
								jQuery("#formmessage").
									append("<div id=\"messagesent\">"+data["message"]+"</div>");
							} else {
								jQuery("#formmessage").
									append("<div id=\"messagesent\">" +
										"The following error(s) occured: "+error+"</div>");
							}
							jQuery("#formmessage").append(
								"<div id=\"sendagain\"><a href=\"javascript:removeFormMessage();\">" +
								"Show the form again</a></div>");

							// Make sure the loading-gif is visible before showing result message.
							jQuery("#progress").delay(1000).fadeOut(1, function(){
								jQuery("#formmessage").fadeIn();
							});
						}
		});
	}

	positionMenus();
	
	// Open "Get Anja's autograph" in a multibox.
	/*if (jQuery(".autographbox").length>0){
		autographbox = new MultiBox('autographbox');
	}*/
	box = null;
	box = new MultiBox('mb05', {ajax: true, descClassName: 'mBoxDesc', useOverlay: true});
});

function fixpng(){
	if (jQuery.browser.msie && jQuery.browser.version == 6){
		jQuery("#media_nav img").add("#menu img").add("#leftarrow img").add("#rightarrow img").css("filter", "alpha(opacity=88)");
	}
	//jQuery("#media_menu img").ifixpng();
}

function positionMenus(){
	var pos = jQuery("#container").offset();
	jQuery("#upper_nav").add("#media_nav").css("left", pos.left+"px");
	jQuery("#artwork_column").css("left", (pos.left+731)+"px");
	jQuery("#media_nav_side_left").css("left", (pos.left-530)+"px");
	jQuery("#media_nav_side_right").css("left", (991+(pos.left))+"px");
}

function opacitateMediaImages() {
	// Dim media images.
	jQuery(".media_container a img").css("opacity", ".8").css("filter", "alpha(opacity=80)");

	// Highlight media images on hover.
	jQuery(".media_container a img").hover(function(){
			jQuery(this).animate({"opacity": "1"}, {"duration": 100, "queue": false}).css("filter", "alpha(opacity=100)");
		}, function(){
			jQuery(this).animate({"opacity": ".8"}, {"duration": 100, "queue": false}).css("filter", "alpha(opacity=80)");
	});
}

function addMediaPages(pages) {
	for(var p=0;p<pages;p++){
		var mediaContainer = "<div class=\"media_container\">";
		for(var i=0;((i<5)&&(((mediaPageLoaded*5)+i)<mediaLarges.length));i++){
			mediaContainer +=
				"<a href=\""+base_url+mediaLarges[(mediaPageLoaded*5)+i]+"\" id=\"p"+mediaPageLoaded+"i"+i+"\" class=\"mb"+mediaPageLoaded+"\">"+
				"<img src=\""+base_url+mediaThumbs[(mediaPageLoaded*5)+i]+"\" alt=\"\" /></a>"+
				"<div class=\"mBoxDesc p"+mediaPageLoaded+"i"+i+"\">"+mediaDescriptions[(mediaPageLoaded*5)+i]+"</div>";
		}
		mediaContainer += "</div>";
		var curWidth = jQuery("#media_content").width();
		jQuery("#media_content").css("width", (curWidth+952)+"px").append(mediaContainer);
		positionMenus();
		box = null;
		box = new MultiBox('mb'+mediaPageLoaded, {descClassName: 'mBoxDesc', useOverlay: true});
		opacitateMediaImages();
		mediaPageLoaded++;
	}
}

/*
 | changeImage
 | Changes captcha image in the contact form.
 */
function changeImage(){
	jQuery("#captcha").attr("src",base_url+"securimage/securimage_show.php?"+Math.random());
}

/*
 | removeFormMessage
 | Removes contact form result message and makes a new contact form visible.
 */
function removeFormMessage() {
	jQuery("#formmessage").fadeOut(400, function(){
		jQuery("#messagesent").remove();
		jQuery("#sendagain").remove();
		jQuery("#contactform").resetForm();
		changeImage();
		jQuery("#contactform").fadeIn();
	});
}
