/**
 * This file holds javscript functions that are used by the templates in the Theme
 * 
 */
 
 // AJAX FUNCTIONS 
function loadNewPage( el, url ) {
	
	var theEl = $(el);
	var callback = {
		success : function(responseText) {
			theEl.innerHTML = responseText;
			if( Lightbox ) Lightbox.init();
		}
	}
	var opt = {
	    // Use POST
	    method: 'post',
	    // Handle successful response
	    onComplete: callback.success
    }
	new Ajax( url + '&only_page=1', opt ).request();
}

function handleGoToCart() { document.location = 'index.php?option=com_virtuemart&page=shop.cart&product_id=' + formCartAdd.product_id.value ; }

// dewak

function removeInfoMessage (){
	if($('modalbox-overlay')){
			
			var centerFX = new Fx.Style('modalbox-center-wrap', 'opacity', {duration: 500, onComplete: function() { 																																									
				$('modalbox-center-wrap').remove();
				var overlayFX = new Fx.Style('modalbox-overlay', 'opacity', {duration: 500, onComplete: function() { 																																								
					$('modalbox-overlay').remove();
				}}).start(0.8,0);
				
			}}).start(1,0);
		
	}
}

function handleAddToCart(productId,productType) {
	
	var overlay = new Element('div', {'id': 'modalbox-overlay'}).injectInside(document.body);
	overlay.setOpacity(0);
	
	overlay.setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});

	var overlayFX = new Fx.Style('modalbox-overlay', 'opacity', {duration: 500, onComplete: function() { 																		 
		
		var centerwrap = new Element('div', {'id': 'modalbox-center-wrap' } ).injectInside(document.body);
		var center = new Element('div', {'id': 'modalbox-center' } ).injectInside(centerwrap);
		centerwrap.setOpacity(0);
		centerwrap.setStyles({"top": ((window.getHeight()/2) - (parseInt(centerwrap.getStyle('height'))/2) )+'px', "left": ((window.getWidth()/2) - (parseInt(centerwrap.getStyle('width'))/2) )+'px'});
		center.setHTML(storeModalbox);
		
		var centerFX = new Fx.Style('modalbox-center-wrap', 'opacity', {duration: 500, onComplete: function() {
			
			//overlay.addEvent('click', function(){ removeInfoMessage(); });
			
			var opt = {
				method: 'get',
				data:'option=com_virtuemart&page=shop.cart&func=cartAdd&quantity[]=1&product_id='+productId+'&Product'+productId+'='+productType+'&hackie='+$random(0,90000),
				onComplete: callback,
				evalScripts: true
			}
		
			var myAjax = new Ajax('index2.php?ajax_request=1', opt);
			myAjax.request();
			
		}}).start(0,1); 
	
	 }}).start(0,0.8);	
	
	var callback = function(responseText) {
		updateMiniCarts();
	}

}

// dewak

/**
* This function searches for all elements with the class name "vmCartModule" and
* updates them with the contents of the page "shop.basket_short" after a cart modification event
*/
function updateMiniCarts() {
	var callbackCart = function(responseText) {
		removeInfoMessage();
		var carts = $$( '.vmCartModule' );
		if( carts ) {
			try { 
				for (var i=0; i<carts.length; i++){
					carts[i].innerHTML = responseText;
				}
			} catch(e) {}
		}
	}
	
	var option = { 
			method: 'get',
			data:'page=shop.basket_short&option=com_virtuemart&hackie='+$random(0,90000),
			onComplete: callbackCart,
			evalScripts: true
	}
	var myAjax2 = new Ajax('index2.php?only_page=1', option);
	myAjax2.request();
}
/**
* This function allows you to present contents of a URL in a really nice stylish dhtml Window
* It uses the WindowJS, so make sure you have called
* vmCommonHTML::loadWindowsJS();
* before
*/
function fancyPop( url, parameters ) {
	
	parameters = parameters || {};
	popTitle = parameters.title || '';
	popWidth = parameters.width || 700;
	popHeight = parameters.height || 600;
	popModal = parameters.modal || false;
	
	window_id = new Window('window_id', {className: "mac_os_x", 
										title: popTitle,
										showEffect: Element.show,
										hideEffect: Element.hide,
										width: popWidth, height: popHeight}); 
	window_id.setAjaxContent( url, {evalScripts:true}, true, popModal );
	window_id.setCookie('window_size');
	window_id.setDestroyOnClose();
}