// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};


// String.reverse() - Returns the string backwards
String.prototype.reverse = function(){
splitext = this.split("");
revertext = splitext.reverse();
reversed = revertext.join("");
return reversed;
}



var nieuwsHeight = new Array();
var curNieuwsId;
var pageName;
var oldPageName = "home";

$(function(){
	externalLinks();
  $(".popupLink").bind('click', showPopup);
  checkHash();
  prepareGallery();
  prepareNavigation();
 $("a[href^=http://www.gertiesound.com][class!=thickbox]").unbind('click');
  $(".popupLink").bind('click', showPopup);
	
  //$(".gallery .leesmeer a").bind('click', loadNieuws);
});

function checkHash(){
  if (location.hash != ""){
	  pageName = location.hash.substr(1);
	  $("#contents_wrapper").scrollTo("#content_"+location.hash.substr(1), {'axis': 'x', 'onBefore': hideNieuws2, 'onAfter': finishNavigate, 'lock': true});
  }
}

function prepareNavigation(){
	$("#menu").localScroll({'target': '#contents_wrapper', 'axis': 'x', 'lock': true, 'onBefore': setName, 'onAfter': finishNavigate});
}

function setName(){
  pageName = $(this).text().toLowerCase();	
}

function showPopup(event){
	event.preventDefault();
	var image = $(this).attr("href");
	var caption = $(this).attr("title");
$.post("/site/getImageTag.php", {"href":image}, function(data){
  var data = eval('('+data+')');
  
  var overlay = $('<div class="overlay"></div>');
  overlay.css('background-color', '#000')
         .css('position', 'fixed')
         .css('top', '0')
         .css('left', '0')
         .css('display', 'block')
         .css('height', '100%')
         .css('width', '100%')
         .css('z-index', '5')
         .css('opacity', 0);
	
	    // append it to the body
			$("body").append(overlay);
			// and fade it in
			$(".overlay").fadeTo(500, 0.5, function() {
				// create the image
				$.post("/site/getImageTag.php", {"href": image}, function(data) {
					imageWidth = data.width;
					imageHeight = data.height;
					// resize into the window if necessary
					maxWidth = $(window).width() - 100;
					maxHeight = $(window).height() - 100;
					
					var heightFactor = imageHeight / maxHeight;
	        var widthFactor = imageWidth / maxWidth;
	        if (heightFactor > widthFactor){
	    			if (heightFactor > 1) {
							finalHeight = maxHeight;
	    			  finalWidth = imageWidth / heightFactor;
						} else {
							finalHeight = imageHeight;
							finalWidth = imageWidth;
						}
	  			} else {
						if (widthFactor > 1) {
	    			  finalHeight = imageHeight / widthFactor;
						  finalWidth = maxWidth;
						} else {
							finalHeight = imageHeight;
							finalWidth = imageWidth;
						}
					}
					// create the image tag
					var captionTag = $('<h4 class="captionTag" style="margin-top: 0px; margin-bottom: 10px; color: rgb(255, 255, 255);">'+caption+'</h4>');
					var imageTag = $('<img src="'+image+'" height="'+finalHeight+'" width="'+finalWidth+'" />');
					
					finalWidth += captionTag.outerWidth();
					finalHeight += captionTag.outerHeight();
					
					// create the image container
					thickbox_imagebox = $("<div class='thickbox_imagebox'></div>");
					thickbox_imagebox.css("height", "2px")
													 .css("width", "0px")
					                 .css("background-color", "transparent")
					                 .css("z-index", "300")
					                 .css("position", "fixed")
					                 .css("top", $(window).height() / 2 - 1)
					                 .css("left", $(window).width() / 2);
					$("body").append(thickbox_imagebox);
					thickbox_imagebox.append(captionTag);
					thickbox_imagebox.append(imageTag);
					finalWidth += $(".captionTag").width();
					finalHeight += $(".captionTag").height();
					$(".thickbox_imagebox").animate({"width": finalWidth+"px", "left": (($(window).width() / 2) - (finalWidth / 2)) + "px"}, 1500);
					$(".thickbox_imagebox").animate({"height": finalHeight+"px", "top": (($(window).height() / 2) - (finalHeight / 2)) + "px"}, 1500);
					$(".overlay, .thickbox_imagebox").bind('click', function(event) {
						$(".thickbox_imagebox").animate({"height": "2px","top": ($(window).height() / 2 - 1) + "px"}, 1500, function() { 
              $(".thickbox_imagebox").animate({"width": "0px","left": $(window).width() / 2}, 1500, function() { 
						    $(".thickbox_imagebox").empty();
						    $(".thickbox_imagebox").remove();
						    $(".overlay").fadeTo(500, 0.0, function() {
							    $(".overlay").remove();
  					    }); 
						  }); 
						});
					});
				}, "json");
		   });
});
return false;
}

function closePopup(event){
  event.preventDefault();
  $(".overlay, .popup").fadeTo(750, 0, function(){
    $(".overlay, .popup").remove();
  });
}

function externalLinks(){
	$("a[href^=http://]").bind('click', function(){
      $(this).attr('target', '_blank');
    });
}



