/* Utility */
function copy_size(source, destination){
	var source = $(source);	var destination = $(destination);
	destination.setStyle({
		position: 'absolute',
		top: '0', left: '0',
		width: source.getWidth()+'px',
		height: source.getHeight()+'px'
	});
}

/* Lightboxing */
function startOverlay(){
	var arrayPageScroll = document.viewport.getScrollOffsets();
	$('overlay').setStyle({ 
		width: document.viewport.getWidth() + 'px', 
		height: document.viewport.getHeight() + 'px',
		top: arrayPageScroll[1] +'px'
	});
	
	$('overlay').setStyle({
		position: 'fixed',
		top: '0px'
	});
	
	new Effect.Appear($('overlay'), { duration: 0.5, from: 0.0, to: 0.8 });
}
function stopOverlay(){
	$('overlay').hide();
}
function closeLightboxedContent(){
	try {if(edits_occurred){document.location.reload();}} catch(e){}
	$('lightboxed_content').update('');
	$('lightboxed_content').hide();
	stopOverlay();
}


/* Form Screening */
function screen_form(form){
	form = $(form);
	var s = document.createElement('div');
	s = $(s);
	s.id = form.id+'_screen';
	s.addClassName('form_screen');
	s.innerHTML = '<div id="form_screen_content"></div>';
	s.setStyle({width: form.getWidth()+'px', height: form.getHeight()+'px'});
	form.insert({top: s});
	$('form_screen_content').setStyle({marginTop: (s.getHeight()/2.3)+'px'});
}

function remove_form_screen(){
	$$('div.form_screen').each(function(form_screen){
		// Element.remove($(form_screen)); // Without scriptaculous
		Effect.Fade( $(form_screen) ); // With scriptaculous
	 })
}

/* Content Tabs */
/*
var currentTab = "photos";
function switchTab(tab) {
	$('tab-' + currentTab).hide();
	$('tab-' + tab).show();
	currentTab = tab;
}
*/

/* Inquiry Popup */
function showInquiryForm(navItemID) {
	var inquiryWin = window.open("/inquiry/" + navItemID, "iw", "height=550,width=700,resizable=yes");
	inquiryWin.focus();
}

/* Product Details */
function view_product_details(product_id){
	startOverlay();
	$('lightboxed_content').show();
	new Ajax.Updater('lightboxed_content','/product/view/'+product_id, {method: 'get', evalScripts: true});
	$('lightboxed_content').setStyle({
		top: (document.viewport.getScrollOffsets()[1]+10)+'px',
		width: '750px',
		left: ((document.body.getWidth()-750)/2).toString()+'px'
	});
}

function swapProductImage(image_id) {
	new Ajax.Updater('product_image_container','/product/product_images/'+image_id, {method: 'get', evalScripts: true});
}

function toggleMetric(toggler){
	$$(['table.metric','table.imperial']).each( function(table){
		Element.toggle(table);
	});
}
function setUnits( unit_system ){
	if(unit_system == 'metric'){
		$$(['table.metric']).each(function(table){ Element.show(table);});
		$$(['table.english']).each(function(table){ Element.hide(table);});
		$$('a.metricToggler').each(function(toggler){ $(toggler).addClassName('inactive');});
		$$('a.englishToggler').each(function(toggler){ $(toggler).removeClassName('inactive');});
		// "deactivate" toggle style
	} else {
		$$(['table.metric']).each(function(table){ Element.hide(table);});
		$$(['table.english']).each(function(table){ Element.show(table);});
		$$('a.metricToggler').each(function(toggler){ $(toggler).removeClassName('inactive');});
		$$('a.englishToggler').each(function(toggler){ $(toggler).addClassName('inactive');});
		// "deactivate" toggle style
	}
	return false;
}

function init(){
	// Hide all of the metric tables
	$$('table.metric').each(function(table){ table.hide(); });
	
	// Add the Metric / English toggle buttons above each relevant table
	$$(['table.english','table.imperial']).each(function(table){
		var metricToggler = $(Builder.node('a',{href:'javascript:void(0);', onclick:"setUnits('metric');"})).update('Metric');;
		var englishToggler = $(Builder.node('a',{href:'javascript:void(0);', onclick:"setUnits('english');"})).update('English');
		var togglerContainer = Builder.node('div',{}, [metricToggler, englishToggler]);
		$(togglerContainer).addClassName('togglers');
		$(metricToggler).addClassName('metricToggler');
		$(englishToggler).addClassName('englishToggler');
		$(englishToggler).addClassName('inactive');
		new Element.insert(table, {before: togglerContainer});
	});
}
