// Avoid clashing with prototype
jQuery.noConflict();

// 0 means disabled; 1 means enabled;
var popupStatus = 0;

// When document is ready setup page
jQuery(document).ready(function() {
	// Setup galleries
	if (jQuery("a#single_image") != null) {
		// This is basic - uses default settings
		jQuery("a#single_image").fancybox();

		// Using custom settings
		jQuery("a#inline").fancybox({
			'hideOnContentClick': true
		});

		// Apply fancybox to multiple items
		jQuery("a.group").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false
		});
	}

	// Setup menu
	jQuery("div.menuItemContainer").hover(function() {
		var item = jQuery("span.menuItem", this);
		if (item != null) {
			item.addClass("menuItemOver");
			item.removeClass("menuItem");
			if(jQuery.browser.msie && jQuery.browser.version.substr(0,1)<8) {
				item.css({
					"padding-top" : "0px"
				});
			}
			item.css({
				"opacity" : "0.0"
			});
			item.animate({
				"opacity" : "1.0"
			}, "fast");
		}
	},
	function() {
		var item = jQuery("span.menuItemOver", this);
		if (item != null && popupStatus != 1) {
			item.addClass("menuItem");
			item.removeClass("menuItemOver");					
		}
	});
	
	// Setup submenus
	jQuery("div.submenuItem").hover(function() {
		item = jQuery("span.submenu0", this);
		if (item != null) {
			item.animate({
				"opacity" : "0.7"
			}, "fast");
		}
	},
	function() {
		item = jQuery("span.submenu0", this);
		if (item != null) {
			item.animate({
				"opacity" : "1.0"
			}, "fast");
		}
	});	
	
	// Setup project boxes
	jQuery("div.imageFrontPage").hover(function() {
		jQuery("span.imageFrontPageDescription", this).animate({
			"opacity": "1.0"
		}, "slow");
	},
	function() {
		jQuery("span.imageFrontPageDescription", this).animate({
			"opacity": "0.7"
		}, "fast");
	});

	// Setup search box
	jQuery("#searchInput").click(function() {
		var value = jQuery(this).val();
		if (value == "Search...") {
			jQuery(this).val("");
			jQuery(this).removeClass("searchInput");
			jQuery(this).addClass("searchInput2");			
		}
	});
	jQuery('#searchInput').blur(function() {
		var value = jQuery(this).val();
		if (value == "") {
			jQuery(this).val("Search...");
			jQuery(this).addClass("searchInput");
			jQuery(this).removeClass("searchInput2");			
		}
	});

	// Setup login box
	jQuery("div.adminButton").click(function() {
		centerPopup();
		loadPopup();
	});

	// Click the x event!
	jQuery("#popupContactClose").click(function(){
		disablePopup();
	});
	// Click out event!
	jQuery("#backgroundPopup").click(function(){
		disablePopup();
	});
	// Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});		
});

// Load popup
function loadPopup(){
	// Loads popup only if it is disabled
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.3"
		});
		jQuery("#backgroundPopup").fadeIn("fast");
		jQuery("#popupContact").fadeIn("fast");
		jQuery("#loginButton").addClass("menuItemOver");
		jQuery("#loginButton").removeClass("menuItem");		
		jQuery("#loginUsername").focus();
		popupStatus = 1;
	}
}

// Disable popup
function disablePopup(){
	// Disables popup only if it is enabled
	if(popupStatus==1) {
		jQuery("#loginButton").removeClass("menuItemOver");
		jQuery("#loginButton").addClass("menuItem");		
		jQuery("#backgroundPopup").fadeOut("fast");
		jQuery("#popupContact").fadeOut("fast");
		popupStatus = 0;
	}
}

// Centering popup on page
function centerPopup(){
	// Request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContact").height();
	var popupWidth = jQuery("#popupContact").width();
	
	// Centering
	var offset = jQuery("div.adminButton").offset();
	jQuery("#popupContact").css({
		"position": "absolute",
		"top": offset.top + jQuery("div.adminButton").height(),
		"left": offset.left-popupWidth/2
	});
	// Only need force for IE6
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
}
