/*
 *
 * ecmsbox by Marcin Krawiec,
 * copyright (c) 2007 Marcin Krawiec (http://calek.info)
 * 
 * Unauthorized using, copying, modyfing and any other action is prohibited
 * 
 * If you want to use this software or any part of it contact the author.
 * 
 * Software is based on Thickbox 2.1 by Codey Lindley 
 * (XHTML structure, CSS style sheet)
 */
  
/*
 * Thickbox 2.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2006 cody lindley
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 * Thickbox is built on top of the very light weight jQuery library.
 */ 



function ecmsBox()
{
	
}

ecmsBox.prototype = {

 	initialize: function()
 	{
 		// preload 'loading' icon
 		var loadingPre = new Image();
 		loadingPre.src = 'images/loadingAnimation.gif'
 	},
 	
 	createBox: function(aHref)
 	{
		this.createLoadIcon();
 		if (document.getElementById("eBoxIFrame") == null) 
		{
		
			$('body')
				.append('<iframe id="eBoxIFrame"></iframe>')
				.append('<div id="eBoxContainer" style="display:none;"></div>');
				
			var arrPageDim = getPageSize();	
			
			$('#eBoxContainer')
				.height(arrPageDim[1])
				.width(arrPageDim[0])
				.click(this.removeBox)
				.fadeTo(250, 0.6)
				.css('filter', 'alpha(opacity=60)');
			
			$('body')
				.append('<div id="eBoxWrapper" style="display: none"></div>');
				
			$('#eBoxIFrame')
				.css('width','100%')
				.css('height','100%');
			
			$('#eBoxWrapper')
				.append('<div id="eBoxtitle"><div id="eBoxajaxWindowTitle"></div><div id="eBoxcloseAjaxWindow"><a href="#" id="eBoxClose">zamknij</a></div></div><div id="eBoxajaxContent"></div>');
				
			$(window).resize(this.preparePosition);
			$(window).scroll(this.preparePosition);
			var selfECMS = this;	
			$('#eBoxClose').click( this.removeBox );
		}
		var selfECMS = this;
		$('#eBoxajaxContent')
			.load(aHref, {}, function() { selfECMS.initWindow(); });
 	},
 	
 	
 	initSidebar: function()
 	{
		debugMsg('initSidebar invoked');
		var selfECMS = this;
 		$('.ajaxGallerySidebar a').click(
 			function()
 			{
				selfECMS.loadImage(this.href, selfECMS);
				return false;
 			}
 		);
		debugMsg('initSidebar finished');
 	},
 	
 	loadImage: function(imgHref, selfECMS, loadTime)
 	{
		var imageExists = false;
		if($('div.ajaxGalleryContent img').is('img'))
		{
			if(selfECMS.aImagesLoaded)
			{
				for(var i=0; i<selfECMS.aImagesLoaded.length; i++)
				{
					debugMsg(selfECMS.aImagesLoaded[i]);
					if(selfECMS.aImagesLoaded[i] && selfECMS.aImagesLoaded[i].src == imgHref)
					{
						imageExists = true;
						$('div.ajaxGalleryContent img').remove();
						$('div.ajaxGalleryContent').append(selfECMS.aImagesLoaded[i]);
						if(!$('div.ajaxGalleryContent img').is('img'))
						{
							$('div.ajaxGalleryContent').append('<img src="'+imgHref+'" />');
						}
						
					}
				}
				if(!imageExists)
				{
					if(loadTime != 2)
					{
						selfECMS.addImagePreload(imgHref);
						selfECMS.preloadStart();
						imageExists = selfECMS.loadImage(imgHref, selfECMS, 2);
					}
				}
			}
			if(!imageExists)
			{
				$('div.ajaxGalleryContent img').remove;
			}
		}
		if(!imageExists)
		{
			$('div.ajaxGalleryContent').append('<img src="'+imgHref+'" />');
			imageExists = true;
		}
		return imageExists;
 	},
 	
 	initWindow: function()
 	{
		debugMsg('initWindow invoked');
		debugMsg('window intialized');
		this.initSidebar();
		debugMsg('window after init sidebar');
		this.preloadImages();
		debugMsg('window after preload images');
 		var arrPageDim = getPageSize();
		this.preparePosition();
		$('#eBoxWrapper')
			.fadeIn(250, this.removeLoadIcon);
//		$('#eBoxajaxContent')
//			.fadeIn(250);
		this.preparePosition();
 	},
 	
 	
 	removeLoadIcon: function()
 	{
 		$('#eBoxLoading')
 			.fadeOut(300, $('#eBoxLoading').remove);
 			
 	},
 	
 	removeBox: function()
 	{
 		$('#eBoxLoading')
 			.fadeOut(300, $('#eBoxLoading').remove);
 			
 		$('#eBoxContainer')
 			.fadeOut(300, $('#eBoxContainer').remove);
 		
 		$(window).unbind('scroll', this.preparePosition);
 		$(window).unbind('resize', this.preparePosition);
 		$('#eBoxWrapper').remove();
 		$('#eBoxIFrame').remove();
 		return false;
 	},
 	
 	
 	createLoadIcon: function()
 	{
 		var arrPageDim = getPageSize();
 		if(!($('#eBoxLoading').is('div')))
 		{
			$('body').append('<div id="eBoxLoading"><img src="images/loadingAnimation.gif" width="100" height="100" /></div>')
 		}
		$('#eBoxLoading')
 			.css('display','block')
 			.css('left', (arrPageDim[0]/2 - 50)+'px')
 			.css('top', (arrPageDim[1]/2 - 50)+'px')
 			.fadeIn(300);
 	},
 	
 	
 	
 	preloadImages: function()
 	{
 		this.aImages = new Array();
 		this.aImagesObj = new Array();
 		this.aImagesLoaded = new Array();
 		this.imagesToLoad = 0;
 		this.imagesLoaded = 0;
 		var selfECMS = this;
 		$('.ajaxGallerySidebar a').each(
 			function()
 			{
 				selfECMS.addImagePreload(this.href);
 			}
 		);
 		this.preloadStart();
/* 		$('.ajaxGalleryContent img').each(
 			function()
 			{
 				this.onload = function()
 				{
 					selfECMS.onImageLoad(this);
 					return true;
 				}
 			}
 		);*/
 	},
 	
 	addImagePreload: function(imgHref)
 	{
		var preImage = new Image();
 		var selfECMS = this;
 		
 		this.aImages.push(imgHref);
 		this.aImagesObj.push(preImage);
 		
 		this.imagesToLoad++;
 		$(preImage).bind('load', function()
		{
			selfECMS.onImageLoad(preImage);
		});
		preImage.onabort = function()
		{
			selfECMS.onImageAbort(preImage);
		}
		preImage.onerror = function()
		{
			selfECMS.onImageError(preImage);
		}
		preImage.bLoaded = false;
	},
	
	preloadStart: function()
	{
		if(this.aImagesObj[0] && this.aImages[0])
		{
			var elToLoad;
			elToLoad = this.aImagesObj.shift();
			elToLoad.src = this.aImages.shift();
			this.aImagesLoaded.push(elToLoad);
			debugMsg('init loading '+elToLoad.src);
		}
 	},
 	
 	onImageLoad: function(elLoaded)
 	{
 		this.imagesLoaded++;
 		elLoaded.bLoaded = true;
		debugMsg('finished loading '+elLoaded.src+'; total:'+this.imagesLoaded+'/'+this.imagesToLoad);
		this.preloadStart();
 	},
 	
 	onImageAbort: function(elLoaded)
 	{
		debugMsg('aborted loading '+elLoaded.src+'; total:'+this.imagesLoaded+'/'+this.imagesToLoad);
		this.preloadStart();
 	},
 	
 	onImageError: function(elLoaded)
 	{
		debugMsg('error loading '+elLoaded.src+'; total:'+this.imagesLoaded+'/'+this.imagesToLoad);
		this.preloadStart();
 	},
 	
 	
 	
 	preparePosition: function()
 	{
		var arrPageDim = getPageSize();
		var arrPageScroll = getPageScroll();
		var eBox = $('#eBoxWrapper');
		var wid = eBox.width();
		var hei = eBox.height();
		debugMsg('Positioning - width: '+wid+' height: '+hei);
		
		if(arrPageDim[3] < 660)
			arrPageDim[3] = 660;
		
		if(arrPageDim[3] < hei)
		{
			eBox
				.css('height', ((arrPageDim[3]-58)+'px'));
			$('div.ajaxgGalleryContent').height((arrPageDim[3]-60)+'px')
			$('#eBoxajaxContent').height((arrPageDim[3]-60)+'px');
		} else {
			eBox
				.css('height', (arrPageDim[3] - 58) + 'px');
				
			$('div.ajaxgGalleryContent').height((arrPageDim[3]-60)+'px')
			$('#eBoxajaxContent').height((arrPageDim[3] - 60) + 'px');
		}
		
		var wid = eBox.width();
		var hei = eBox.height();
		
		eBox
			.css('left', (((arrPageDim[2] - wid)/2) - 10)+'px')
			.css('top', (((arrPageDim[3] - hei)/2) + arrPageScroll[1] - 10)+'px');
			
		$('#eBoxContainer')
			.height(arrPageDim[1])
			.width(arrPageDim[0]);
 	}
 	
}

function debugMsg(msg)
{
/*	$('body').append(msg+'<hr />');*/
}
 

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


var myEcmsBox = new ecmsBox();
function ecmsInit()
{
	myEcmsBox.initialize();
	if($('img.main').is('img'))
	{
		var imgEl = $('img.main');
		var idEl = imgEl.get(0).id.replace('image-id', '');
		imgEl.wrap('<a href="#" class="ecmsBox main" rel="images-gallery"></a>');
		$('a.ecmsBox').append('<div class="more"><a id="powiekszJeden" href="#" class="ecmsBox" rel="images-gallery"><img src="gfx/powieksz.gif" alt="powiększ" title="powiększ" /></a><a id="powiekszDwa" href="#" class="ecmsBox" ><img src="gfx/wiecej.gif" alt="więcej zdjęć" title="więcej zdjęć" /></a></div>');
			
		// assign events
		$('a.ecmsBox').click(
			function()
			{
				myEcmsBox.createBox('index.php?ECProductsAjaxGallery='+idEl+'&ajax=1');
				return false;
			}
		);
	}
}

$(document).ready(ecmsInit);


