/**
 * Probox - A Prototype Thickbox Imitation
 * By Bryan Tong (http://www.bryantong.com)
 * Copyright 2008 Bryan Tong
 *
 * Version 1.1
 */


//Instantiate Class
Event.observe(window, 'load',

	function(){

		new Probox('[class="probox"]', pathToProboxGIF);

	}

);


var Probox = Class.create({

	//Init
	initialize: function(selector,loadingImage){

		//User Agent
		var userAgent = navigator.userAgent.toLowerCase();

		//Get Browser Version
		Prototype.Browser.Version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1];

		//Get Main Elements
		this.body = $$('body')[0];
		this.html = $$('html')[0];

		//Preload Loading Image
		this.loadingImageSrc = loadingImage;
		this.loadingImage = new Image();
		this.loadingImage.src = this.loadingImageSrc;

		//Style Body And HTML Elements
		this.body.setStyle({width: "100%", height: "100%"});
		this.html.setStyle({width: "100%", height: "100%"});

		//Append Hidden HTML
		this.initOverlay();
		this.initLoading();
		this.initWindow();

		//Add Events to Selected Elements
		$$(selector).each(

			function(el){

				el.observe('click',

					function(event){

						//Stop Bubbling
						event.stop();

						//Init Event
						this.show(el);

					}.bindAsEventListener(this)

				);

			}.bind(this)

		);

	},

	//Show Probox
	show : function(el){

		//Clear Elements
		this.clearElements();

		this.bhide = this.hide.bindAsEventListener(this);

		//Register Close Key
		Event.observe(document, 'keydown', this.bhide);

		//Show Elements
		$('PB_HideSelect').show();
		$('PB_Overlay').show();

		//Show Loading
		this.showLoading();

		//Get Title
		var title = el.title || el.name || '';

		//Get Url
		var url = el.href || el.alt;

		//Get Image Group
		var group = el.rel || false;

		//Set Title
		this.setTitle(title);

		//Check For Image
		var imageRule = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;

		//Image Loading
		if(url.match(imageRule)){

			//Load Image
			this.loadImage(url,group);

		}

		//Show HTML
		else
		{
			//Set Window Dimensions
			this.windowDimensions(600,400);

			//Load Html
			this.loadHTML(url);

		}




	},

	//Clear Elements
	clearElements : function(){

		$("PB_Window").update("");

	},

	//Set Title
	setTitle : function(title){

		//Create Title Element
		var titleElement = new Element('div', {id : "PB_Title"}).update(title + ' (apasati ESC pentru a <a href="javascript: {}" id="PB_Close">inchide</a>)');

		//Append Title Element
		$("PB_Window").appendChild(titleElement);

		//Add Event
		$('PB_Close').observe('click', function(){ this.hide(); }.bindAsEventListener(this) );

	},

	//Show Loading
	showLoading : function(){

		$('PB_Load').show();

	},

	//Hide Loading
	hideLoading : function(){

		$('PB_Load').hide();

	},

	//Initialize Page Overlay
	initOverlay : function(){

		//Add iFrame to Fix IE6 Select Issues
		var iframeElement = new Element('iframe', {id : 'PB_HideSelect', style : 'display: none;'});

		//Append to Body
		this.body.appendChild(iframeElement);

		//Add Overlay Element
		var overlayElement = new Element('div',{id : 'PB_Overlay', style : 'display: none;'});

		//Add BG Class
		overlayElement.addClassName('PB_OverlayBG');

		//Add Close Event
		overlayElement.observe('click', function(){this.hide()}.bindAsEventListener(this));

		//Append to Body
		this.body.appendChild(overlayElement);

	},

	//Initialize Loading HTML
	initLoading : function(){

		//Init Element
		var loadingElement = new Element('div', {id : 'PB_Load', style : 'display: none;'});

		//Init Image
		var loadingImage = new Element('img', {src : this.loadingImageSrc});

		//Append to Loading
		loadingElement.appendChild(loadingImage);

		//Append Element to Body
		this.body.appendChild(loadingElement);

	},

	//Initialize Window HTML
	initWindow : function(){

		//Load Element
		var windowElement = new Element('div', {id : 'PB_Window', style : 'display: none;'});

		//Append to Body
		this.body.appendChild(windowElement);

	},

	//Hide Probox
	hide : function(){

		//Remove Window Content
		if($(this.iframeId) != null){

			$(this.iframeId).remove();

		}

		if($("PB_Image") != null){

			$("PB_Image").remove();

		}

		Event.stopObserving(document, 'keydown', this.bhide);

		this.hideLoading();

		$('PB_HideSelect').hide();
		$('PB_Overlay').hide();
		$('PB_Window').hide();

	},

	//Set Window Dimensions
	windowDimensions : function(width,height){

		this.width = width;
		this.height = height;

		$("PB_Window").setStyle(
			{
				marginLeft: '-' + parseInt((width / 2),10) + 'px',
				width: width + 'px'
			}
		);

		// take away IE6 -- Taken from Thickbox Thanks!
		if(!(Prototype.Browser.IE && Prototype.Browser.Version < 7)){

			$("PB_Window").setStyle({marginTop: '-' + parseInt((height / 2),10) + 'px'});

	  	}

		$("PB_Window").setStyle({width : width+"px", height: height+"px"});

	},

	//Load Image
	loadImage : function(image,group){

		//Unload Loading
		setTimeout('this.hideLoading',300);

		imgPreloader = new Image();
      	imgPreloader.onload = function(){

      		imgPreloader.onload = null;

			// Resizing large images - orginal by Christian Montoya edited by me.
			var x = document.viewport.getWidth();
			var y = document.viewport.getHeight();

			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;

			if (imageWidth > x) {

				imageHeight = imageHeight * (x / imageWidth);
				imageWidth = x;

				if (imageHeight > y) {

				  imageWidth = imageWidth * (y / imageHeight);
				  imageHeight = y;
				}

			}
			else
			if (imageHeight > y) {

				imageWidth = imageWidth * (y / imageHeight);
				imageHeight = y;

				if (imageWidth > x) {
			  		imageHeight = imageHeight * (x / imageWidth);
				  	imageWidth = x;
				}
			}
			// End Resizing

			this.windowDimensions((imageWidth + 30),(imageHeight + 60));

			var ImageElement = new Element('img', {id: "PB_Image", src : image});

			this.hideLoading();

			$('PB_Window').show();

			$("PB_WindowConetnt").appendChild(ImageElement);


      	}.bind(this);

		imgPreloader.src = image;

	},

	//Load HTML
	loadHTML : function(url){

		//Get Random Iframe ID
		this.iframeId = "PB_AjaxContent"+Math.round(Math.random()*1000);

		//Create Container
		var ajaxContent = new Element('iframe', {name: this.iframeId, frameborder: 0, id : this.iframeId});

		//Set Ajax Content Dimensions
		ajaxContent.setStyle({width: (this.width)+'px', height: (this.height - 45)+'px'});

		//Append Container
		$("PB_Window").appendChild(ajaxContent);

		//Make Ajax Call
		new Ajax.Request(
			url,
			{
				method : 'get',
				evalScripts: 'true',
				onComplete: function(response){

					frames[this.iframeId].document.write(response.responseText);
					frames[this.iframeId].document.close();

					$('PB_Window').show();
					this.hideLoading();

				}.bind(this)
			}
		);

	}

});
