﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var active_profile = '';

//loading popup with jQuery magic!
function loadPopup(profile_name){
	// Get Profile via Ajax Call
	$('#popupContact').load('/index__/fetch-profile.ajax.php', { 'name' : profile_name }, function() {
		// Shift Image
		shiftImage();

		// Add "Close Popup" Event Handlers
		// (User clicked the x, or outside the pop-up.)
		$('#popupContactClose, #backgroundPopup').click(function(){
			disablePopup();
		});
		
		// Add "Next Profile" Event Handler
		$('#popupContactNext').click(function() {
			getNextProfile();
		});
		
		// Add "Previous Profile" Event Handler
		$('#popupContactPrev').click(function() {
			getPreviousProfile();
		});

		// Loads popup only if it is disabled
		if (popupStatus == 0) {
			//centering with css
			centerPopup();
		
			$("#backgroundPopup").css({
				"opacity": "0.7"
			});

			$('#backgroundPopup, #popupContact').fadeIn("slow");
			popupStatus = 1;
		}
	});
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$('#backgroundPopup, #popupContact').fadeOut('slow');
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

// Get Next Profile
function getNextProfile() {
	// Only load next profile if popup is visible
	if (popupStatus == 1) {
		// Trigger Next Profile
		$temp = $('#' + active_profile).parent().next().click();
	}
}

// Get Previous Profile
function getPreviousProfile() {
	// Only load next profile if popup is visible
	if (popupStatus == 1) {
		// Trigger Next Profile
		$temp = $('#' + active_profile).parent().prev().click();
	}
}

// Shift the Image into it's correct position
function shiftImage() {
	// Move Image (if given)
	$('#popupImage').append($('#popupScrollContent img'));
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	//LOADING POPUP
	//Click the button event!
	$("#peopleThumbs a").click(function(){
		// Get name of profile
		var $img = $('img', this);
		active_profile = $img.attr('id');
		var profile_name = $img.attr('alt').toLowerCase();

		// Load Profile - Pass profile name
		loadPopup(profile_name);
	});

	// CLOSING POPUP
	// Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});
