function sendusyourvideo(product_id) {
	window.open('/v/upload/?product_id='+product_id,'addvideo2','width=500,height=540,resizable=yes,scrollbars=yes');
}

function review(product_id) {
	window.open('/popup.html?dir=admin&action=review&id='+product_id,'r'+product_id,'width=450,height=550,resizable=yes,scrollbars=yes');
}

function enlarge(product_id) {
	window.open('/popup.html?dir=admin&action=enlarge&id='+product_id,'e'+product_id,'directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizeable=yes,width=750,height=750');
	

}function addimage(product_id) {
	window.open('/popup.html?dir=images&action=addimage&product_id='+product_id,'r1109','width=450,height=500,resizable=yes,scrollbars=yes');
}

function viewimage(imageID, imageWidth, imageHeight) {
	window.open('/popup.html?dir=images&action=viewimage&imageID='+imageID,'imageID', 'width='+imageWidth+',height='+imageHeight+',resizable=yes,scrollbars=no');
}

/**
* adapted from: http://www.michaelminella.com/javascript/prototype-script.aculo.us-lightbox.html
* Lightbox
*
* This libary is used to create a lightbox in a web application.  This library
* requires the Prototype 1.6 library and Script.aculo.us core, effects, and dragdrop
* libraries.  To use, add a div containing the content to be displayed anywhere on 
* the page.  To create the lightbox, add the following code:
*
*	var test;
*	
*	Event.observe(window, 'load', function () {
*		test = new Lightbox('idOfMyDiv');
*	});
*	
*	Event.observe('lightboxLink', 'click', function () {
*		test.open();
*	});
*
*	Event.observe('closeLink', 'click', function () {
*		test.close();
*	});
*     
*/

var Lightbox = Class.create({
	open_with_image : function(url, width, height) {
		photo_element = $(this.photo_element);
		photo_element.src = "/i/spacer.gif";
		if ( width && height ) {
			photo_element.width = width;
			photo_element.height = height;
			$('light_box_container_header').style.width = width+"px";
		}
		this.open();
		photo_element.src = url;
	},
	
	open : function() {
		this._centerWindow(this.container);
		this._fade('open', this.container);
		this.binded_handle_keypress = this.handle_keypress.bind(this)
		document.observe('keydown', this.binded_handle_keypress);
	},
	
	close : function() {
		this._fade('close', this.container);
		photo_element = $(this.photo_element);
		photo_element.src = "/i/spacer.gif";
		document.stopObserving('keydown', this.binded_handle_keypress);
	},
	
	handle_keypress : function(event) {
		if (Event.KEY_ESC == event.keyCode ) {
			this.close();
		}
	},
	
	_fade : function(userAction, id){
		if ( userAction == 'close' ) {
			new Effect.Opacity('light_box_bg_fade',
					   {duration:0.2,
					    from:0.8,
					    to:0,
					    afterFinish:this._makeInvisible,
					    afterUpdate:this._hideLayer(id)});
		} else {
			$('light_box_bg_fade').style.height = $(document.body).getHeight() + 'px';
			new Effect.Opacity('light_box_bg_fade',
					   {duration:0.2,
					    from:0,
					    to:0.8,
					    beforeUpdate:this._makeVisible,
					    afterFinish:this._showLayer(id)});
		}
	},
	
	_makeVisible : function(){
		$("light_box_bg_fade").style.visibility="visible";
	},

	_makeInvisible : function(){
		$("light_box_bg_fade").style.visibility="hidden";
	},

	_showLayer : function(id){
		$(id).style.display="block";
	},
	
	_hideLayer : function(id){
		$(id).style.display="none";
	},
	
	_centerWindow : function(element) {
		var windowHeight = parseFloat($(element).getHeight())/2; 
		var windowWidth = parseFloat($(element).getWidth())/2;
		var offset = $(document.body).viewportOffset();
		
		var top;
		var left;
		
		if ( typeof window.innerHeight != 'undefined' ) {
			top = Math.round(document.body.offsetTop + ((window.innerHeight - $(element).getHeight()))/2);
			left = Math.round(document.body.offsetLeft + ((window.innerWidth - $(element).getWidth()))/2);
		} else {
			top = Math.round(document.body.offsetTop + ((document.documentElement.offsetHeight - $(element).getHeight()))/2);
			left = Math.round(document.body.offsetLeft + ((document.documentElement.offsetWidth - $(element).getWidth()))/2);
		}
		if ( top < 0 ) {
			top = 0;
		}
		
		if ( left < 0 ) {
			left = 0;
		}
		top -= offset.top;
		left -= offset.left;
		$(element).style.top = top + 'px';
		$(element).style.left = left + 'px';
		
	},
	
	initialize : function(containerDiv) {
		this.container = containerDiv;
		this.photo_element = this.container + '_photo';
		if ( $('light_box_bg_fade') === null ) {
			var screen = new Element('div', {'id': 'light_box_bg_fade'});
			screen.style.height = $(document.body).getHeight() + 'px';
			document.body.appendChild(screen);
		}
		this._hideLayer(this.container);
	}
});

function load_public_reviews(product_id, dom_id, page_num) {
    load_ajax(product_id, 'public_reviews', dom_id, page_num);
}

function load_customer_images(product_id, dom_id, page_num) {
    load_ajax(product_id, 'customer_images', dom_id, page_num);
}

function load_ajax(product_id, type, dom_id, page_num) {
    new Ajax.Updater( dom_id, '/firebox/product_ajax', {
      parameters: { 
          type: type,
          page_num: page_num,
          pid: product_id
      }
    });
}

var lightbox;

document.observe("dom:loaded", function () {
	lightbox = new Lightbox('light_box_container');
	$('light_box_bg_fade').observe('click', function(event){
		lightbox.close();
	 });
});
