/**
* lightbox for Corporate Responsibility page
*
* @author Tomas Rasek
*/
var jqf = jQuery.noConflict();

/**
* destroy (close) lightbox
*/
function closeLightBox() {
	jqf('#lightBox').slideUp('slow', function() {
		jqf('#lightBox').html('');
		jqf('#lightBox').remove();
		jqf('#lightBoxBackground').remove();
	});
	return false;
}

/**
* Show next and previous links
*/
function showNextLink() {
	jqf('#next').show();
	jqf('#previous').show();
}


/**
* Hide next and previous links
*/
function hideNextLink() {
	jqf('#next').hide();
	jqf('#previous').hide();
}

/**
* show lightbox
*/
function showLightBox(thisLink) {
		// get URL //
		//var thisLink = this;

		// remove old lightbox //
		if (jqf('#lightBox').length > 0) {
			jqf('#lightBox').html('');
			jqf('#lightBox').remove();
		}

		// create overlay div //
		jqf('body').append('<div id="lightBoxBackground" onclick="closeLightBox();"></div>');

		// create lightbox div //
		var lightBoxClass = '';
		if (window.location.href.match(/.*whos-who$/)) {
			lightBoxClass = ' class="background2"';
		}
		jqf('body').append('<div id="lightBox"' + lightBoxClass + '></div>');

		// center lightBox //
		if ((!jqf.browser.msie && !jqf.browser.opera) || (jqf.browser.msie && jQuery.browser.version != '6.0')) {
			var top = 0;
			var left = 0;
			if (jqf(window).width() > 0 && jqf(window).height() > 0) {
				if (jqf(window).height() > 505) {
					top = (jqf(window).height() - 505) / 2;
				}
				if (jqf(window).width() > 750) {
					left = (jqf(window).width() - 750) / 2;
				}
			}
			jqf('#lightBox').css('top', top + 'px');
			jqf('#lightBox').css('left', left + 'px');
		}

		// create header //
		jqf('#lightBox').append(
			'<p id="lightBoxClose">'
				+ '<a href="#" title="Close" onclick="closeLightBox(); return false;">'
					+ '<img src="/templates/main/images/lightBox/close.gif" width="27" height="26" alt="Close" />'
				+ '</a>'
			+ '</p> '
		);


		// get content //
		if (thisLink.href.match(/\.jpg$/)) {
			var imageNumber = parseInt(thisLink.rel.replace(/-.*/, ''));
			var allImages = parseInt(thisLink.rel.replace(/.*-/, ''));
			var previous = '';
			var next = '';

			if (imageNumber > 1) {
				previous = '<p id="previous" onmouseover="showNextLink();"><a href="' + jqf('a[rel=' + (imageNumber - 1) + '-' + allImages + ']').attr('href') + '" onclick="showLightBox(this); return false;" rel="' + (imageNumber - 1) + '-' + allImages + '">Previous</a></p>';
			}
			if (imageNumber < allImages) {
				next = '<p id="next" onmouseover="showNextLink();"><a href="' + jqf('a[rel=' + (imageNumber + 1) + '-' + allImages + ']').attr('href') + '" onclick="showLightBox(this); return false;" rel="' + (imageNumber + 1) + '-' + allImages + '">Next</a></p>';
			}

			// create body //
			jqf('#lightBox').append(previous + next + '<div id="lightBoxContainer" onmouseover="showNextLink();" onmouseout="hideNextLink();"><div id="lightBoxBody"></div></div>');
			jqf('#lightBoxBody').html('<p class="textCenter" class="clearfix"><img src="' + thisLink.href + '" alt="" /></p>');
		}
		else {
			// create body //
			jqf('#lightBox').append('<div id="lightBoxContainer"><div id="lightBoxBody"></div></div>');
			jqf('#lightBoxBody').load(thisLink.href);
		}

		// show lightbox //
		jqf('#lightBoxBackground').show();
		jqf('#lightBox').slideDown('slow');

		return false;
}

jqf(document).ready(function() {

	/**
	* close lightbox on Escape
	*/
	jqf(document).keypress(function(e) {
		if(e.keyCode == 27) {
			closeLightBox();
		}
	});

	/**
	* bind lightBox
	*/
	jqf('.corpmore').click(function() {
		showLightBox(this);
		return false;
	});

	/**
	* bind lightBox
	*/
	jqf('.item124 a').click(function() {
		showLightBox(this);
		return false;
	});

	/**
	* bind lightBox
	*/
	jqf('.lightBox').click(function() {
		showLightBox(this);
		return false;
	});

	/**
	* bind lightBox
	*/
	jqf('.webgallery_default li a').click(function() {
		showLightBox(this);
		return false;
	});
});
