/**
 * RokVM - VirtueMart Class for handling RocketTheme templates
 * Djamil Legato - (c) RocketTheme, LLC
 * 
 */

var RokVM = {};
RokVM.autoHide = 3; // seconds before auto closing the Panel

RokVM.Base = {
    uri: window.templatePath,
    start: function () {
        RokVM.Base.quantityBox();
        RokVM.Cart.init()
    },
    featuredHeight: function (b) {
        b = $(b);
        if (!b) {
            return RokVM.Base
		}
        var c = b.getElements(".home-featured-inner");
        var d = 0;
        c.each(function (a) {
		d = Math.max(a.getSize().size.y, d)
		    });
        c.setStyle("height", d)
    },
    quantityBox: function () {
        var h = $$(".cart-quantity-box, .home-featured-cartblock");
        if (h.length) {
            h.each(function (b) {
		    var c = b.getElement(".quantity_box_button_up-bk");
		    var d = b.getElement(".quantity_box_button_down-bk");
		    var f = b.getElement(".inputboxquantity-bk");
		    if (!c || !d || !f) {
                    return
			}
		    var g = f.getValue().toInt();
		    f.addEvent("keyup", function (e) {
			    e = (new Event(e)).stop();
			    var a = this.value;
			    if (a < 0) {
                        a = 0
			    }
			    if (isNaN(a)) {
				this.value = 1;
                        a = 1
			    }
			    if (e.key == "up") {
				if ($type(this.getValue().toInt()) == "number") {
                            this.value = ++a
				}
			    }
			    if (e.key == "down") {
				if (a > 0 && $type(this.getValue().toInt()) == "number") {
                            this.value = --a
				}
			    }
			});
		    f.addEvent("mousewheel", function (e) {
			    e = (new Event(e)).stop();
			    var a = this.value;
			    if (a < 0) {
                        a = 0
			    }
			    if (isNaN(a)) {
				this.value = 1;
                        a = 1
			    }
			    if (e.wheel == 1) {
				if ($type(this.getValue().toInt()) == "number") {
                            this.value = ++a
				}
			    }
			    if (e.wheel == -1) {
				if (a > 0 && $type(this.getValue().toInt()) == "number") {
                            this.value = --a
				}
			    }
			});
		    c.addEvent("click", function (e) {
			    (new Event(e)).stop();
			    if ($type(f.getValue().toInt()) == "number") {
                        f.value = ++f.value
			    }
			});
		    d.addEvent("click", function (e) {
			    (new Event(e)).stop();
			    if (f.getValue().toInt() > 0 && $type(f.getValue().toInt()) == "number") {
                        f.value = --f.value
			    }
			})
			})
		}
    }
};
RokVM.Cart = {
    init: function () {
        RokVM.Cart.cartPanel = $("cart-panel");
        RokVM.Cart.cartButton = $("cart-button");
        RokVM.Cart.cartSurround = $("cart-panel-surround");
        if (!RokVM.Cart.cartPanel || !RokVM.Cart.cartButton || !RokVM.Cart.cartSurround) {
            return RokVM.Cart
		}
        if (!RokVM.Cart.Fx) {
            RokVM.Cart.Fx = (new(Fx.Slide)(RokVM.Cart.cartPanel, {
			duration: 400,
			transition: Fx.Transitions.Expo.easeOut
		    })).hide()
        }
        RokVM.Cart.cartSurround.setStyle("visibility", "visible");
        RokVM.Cart.cartButton.getFirst("a").addEvent("click", function (e) {
		(new Event(e)).stop();
		if (!RokVM.Cart.cartPanel.getText().length && !window.webkit) {
                return
		    }
		if (!RokVM.Cart.cartPanel.innerHTML.clean().length && window.webkit) {
                return
		    }
		var a = RokVM.Cart.cartButton.getElement("strong");
		if (!a || !a.getText().toInt() && (!RokVM.Cart.cartPanel.getText().length && !window.webkit)) {
                return
		    }
		RokVM.Cart.clicked = true;
		if (window.fontFx && window.fontFx.open) {
		    window.fontFx.toggle()
			}
		if (window.loginFx && window.loginFx.open) {
		    window.loginFx.toggle()
			}
		RokVM.Cart.Fx.toggle()
		    });
        RokVM.scrollWindow = new(Fx.Scroll)(window);
        RokVM.Cart.getShortList();
        RokVM.Cart.XHRify()
    },
    XHRify: function () {
        var b = $$(".addtocart_form");
        if (!b.length) {
            return RokVM.Cart
		}
        b.each(function (a) {
		a.addEvent("submit", RokVM.Cart.add.bindWithEvent(a, true))
	    })
    },
    add: function (e, c) {
        (new Event(e)).stop();
        (new Ajax(this.getProperty("action"), {
		data: $(this),
		    evalScripts: true,
		    onComplete: function (r) {
		    var a = new Element('div').setHTML(r);
		    var b = a.getElement('.shop_tip');
		    RokVM.Cart.getShortList((r.length && b) ? r : false);
                RokVM.Cart.clicked = c || false
		    }
        })).request()
    },
    getShortList: function (b) {
        var c = {
            onComplete: function (a) {
                RokVM.Cart.update(a, b)
            },
            evalScripts: true,
            data: {
                only_page: 1,
                page: "shop.basket_short",
                option: "com_virtuemart"
            }
        };
        (new Ajax(RokVM.Base.uri + "index2.php", c)).request()
    },
    handleError: function (a) {
        var b = RokVM.Cart.cartPanel;
        var c = new Element('div').setHTML(a);
        var d = b.getElement('.show-cart-button');
        var e = c.getElement('.shop_tip').inject((d) ? d : b, (d) ? 'before' : 'inside')
    },
    update: function (a, b) {
        RokVM.Cart.cartPanel.setHTML(a);
        var c = RokVM.Cart.cartPanel.getElement(".total_products"),
	total = 0;
        if (c) {
            total = c.innerHTML.split(" ")[0].toInt()
		}
        if (!total) {
            RokVM.Cart.cartButton.getFirst().setProperty("class", "cart-button-desc")
        } else {
            RokVM.Cart.cartButton.getFirst().setProperty("class", "cart-button-desc-full")
        }
        RokVM.Cart.cartButton.getElement("strong").setText(total);
        if (b) RokVM.Cart.handleError(b);
        if (RokVM.Cart.clicked && a.length || b) {
            if (window.fontFx && window.fontFx.open) {
                window.fontFx.toggle()
		    }
            if (window.loginFx && window.loginFx.open) {
                window.loginFx.toggle()
		    }
            RokVM.scrollWindow.toTop();
            RokVM.Cart.show();
            RokVM.Cart.hide.delay(RokVM.autoHide * 1000)
		}
        RokVM.Cart.clicked = false
    },
    show: function () {
        RokVM.Cart.Fx.slideIn()
    },
    hide: function () {
        RokVM.Cart.Fx.slideOut()
    }
};
window.addEvent("domready", RokVM.Base.start);
window.addEvent(window.webkit ? "load" : "domready", function () {
	RokVM.Base.featuredHeight("home-featured")
	    });

/*************************
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2.u={V:6.1o,W:5(){2.u.X();2.3.Y()},Z:5(b){b=$(b);4(!b){p 2.u}7 c=b.1p(".J-K-1q");7 d=0;c.L(5(a){d=1r.1s(a.1t().1u.y,d)});c.10("1v",d)},X:5(){7 h=$$(".q-1w-1x, .J-K-1y");4(h.s){h.L(5(b){7 c=b.k(".1z");7 d=b.k(".1A");7 f=b.k(".1B");4(!c||!d||!f){p}7 g=f.m().j();f.n("1C",5(e){e=(9 v(e)).w();7 a=8.i;4(a<0){a=0}4(11(a)){8.i=1;a=1}4(e.12=="1D"){4($x(8.m().j())=="z"){8.i=++a}}4(e.12=="1E"){4(a>0&&$x(8.m().j())=="z"){8.i=--a}}});f.n("1F",5(e){e=(9 v(e)).w();7 a=8.i;4(a<0){a=0}4(11(a)){8.i=1;a=1}4(e.13==1){4($x(8.m().j())=="z"){8.i=++a}}4(e.13==-1){4(a>0&&$x(8.m().j())=="z"){8.i=--a}}});c.n("M",5(e){(9 v(e)).w();4($x(f.m().j())=="z"){f.i=++f.i}});d.n("M",5(e){(9 v(e)).w();4(f.m().j()>0&&$x(f.m().j())=="z"){f.i=--f.i}})})}}};2.3={Y:5(){2.3.l=$("q-14");2.3.t=$("q-D");2.3.N=$("q-14-1G");4(!2.3.l||!2.3.t||!2.3.N){p 2.3}4(!2.3.o){2.3.o=(9(o.1H)(2.3.l,{1I:1J,1K:o.1L.1M.1N})).O()}2.3.N.10("1O","1P");2.3.t.P("a").n("M",5(e){(9 v(e)).w();4(!2.3.l.Q().s&&!6.E){p}4(!2.3.l.15.1Q().s&&6.E){p}7 a=2.3.t.k("16");4(!a||!a.Q().j()&&(!2.3.l.Q().s&&!6.E)){p}2.3.F=G;4(6.A&&6.A.H){6.A.C()}4(6.B&&6.B.H){6.B.C()}2.3.o.C()});2.17=9(o.1R)(6);2.3.R();2.3.18()},18:5(){7 b=$$(".1S");4(!b.s){p 2.3}b.L(5(a){a.n("1T",2.3.19.1U(a,G))})},19:5(e,c){(9 v(e)).w();(9 1a(8.1V("1W"),{1b:$(8),1c:G,1d:5(r){7 a=9 1e(\'1f\').S(r);7 b=a.k(\'.1g\');2.3.R((r.s&&b)?r:T);2.3.F=c||T}})).1h()},R:5(b){7 c={1d:5(a){2.3.1i(a,b)},1c:G,1b:{1X:1,1Y:"1Z.20",21:"22"}};(9 1a(2.u.V+"23.24",c)).1h()},1j:5(a){7 b=2.3.l;7 c=9 1e(\'1f\').S(a);7 d=b.k(\'.U-q-D\');7 e=c.k(\'.1g\').25((d)?d:b,(d)?\'26\':\'27\')},1i:5(a,b){2.3.l.S(a);7 c=2.3.l.k(".28"),I=0;4(c){I=c.15.29(" ")[0].j()}4(!I){2.3.t.P().1k("1l","q-D-1m")}2a{2.3.t.P().1k("1l","q-D-1m-2b")}2.3.t.k("16").2c(I);4(b)2.3.1j(b);4(2.3.F&&a.s||b){4(6.A&&6.A.H){6.A.C()}4(6.B&&6.B.H){6.B.C()}2.17.2d();2.3.U();2.3.O.2e(2.2f*2g)}2.3.F=T},U:5(){2.3.o.2h()},O:5(){2.3.o.2i()}};6.n("1n",2.u.W);6.n(6.E?"2j":"1n",5(){2.u.Z("J-K")});',62,144,'||RokVM|Cart|if|function|window|var|this|new|||||||||value|toInt|getElement|cartPanel|getValue|addEvent|Fx|return|cart||length|cartButton|Base|Event|stop|type||number|fontFx|loginFx|toggle|button|webkit|clicked|true|open|total|home|featured|each|click|cartSurround|hide|getFirst|getText|getShortList|setHTML|false|show|uri|start|quantityBox|init|featuredHeight|setStyle|isNaN|key|wheel|panel|innerHTML|strong|scrollWindow|XHRify|add|Ajax|data|evalScripts|onComplete|Element|div|shop_tip|request|update|handleError|setProperty|class|desc|domready|templatePath|getElements|inner|Math|max|getSize|size|height|quantity|box|cartblock|quantity_box_button_up|quantity_box_button_down|inputboxquantity|keyup|up|down|mousewheel|surround|Slide|duration|400|transition|Transitions|Expo|easeOut|visibility|visible|clean|Scroll|addtocart_form|submit|bindWithEvent|getProperty|action|only_page|page|shop|basket_short|option|com_virtuemart|index2|php|inject|before|inside|total_products|split|else|full|setText|toTop|delay|autoHide|1000|slideIn|slideOut|load'.split('|'),0,{}))
**************************/


var live_site = RokVM.Base.uri;


/// Following is VM stuff, kept for compatibility


/**
 * 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: 'get',
	    // Handle successful response
	    onComplete: callback.success
    }
	new Ajax( url + '&only_page=1', opt ).request();
}

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

function handleAddToCart( formId, parameters ) {
	formCartAdd = document.getElementById( formId );
	
	var callback = function(responseText) {
		updateMiniCarts();
		// close an existing mooPrompt box first, before attempting to create a new one (thanks wellsie!)
		/*if (document.boxB) {
			document.boxB.close();
			clearTimeout(timeoutID);
		}

		document.boxB = new MooPrompt(notice_lbl, responseText, {
				buttons: 2,
				width:400,
				height:150,
				overlay: false,
				button1: ok_lbl,
				button2: cart_title,
				onButton2: 	handleGoToCart
			});
			
		setTimeout( 'document.boxB.close()', 3000 );*/
	}
	
	var opt = {
	    // Use POST
	    method: 'post',
	    // Send this lovely data
	    data: $(formId),
	    // Handle successful response
	    onComplete: callback,
	    
	    evalScripts: true
	}

	new Ajax(formCartAdd.action, opt).request();
}
/**
* 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) {
		carts = $$( '.vmCartModule' );
		if( carts ) {
			try { 
				for (var i=0; i<carts.length; i++){
					carts[i].innerHTML = responseText;
		
					try {
					color = carts[i].getStyle( 'color' );
					bgcolor = carts[i].getStyle( 'background-color' );
					if( bgcolor == 'transparent' ) {
						// If the current element has no background color, it is transparent.
						// We can't make a highlight without knowing about the real background color,
						// so let's loop up to the next parent that has a BG Color
						parent = carts[i].getParent();
						while( parent && bgcolor == 'transparent' ) {
							bgcolor = parent.getStyle( 'background-color' );
							parent = parent.getParent();
						}
					}
					var fxc = new Fx.Style(carts[i], 'color', {duration: 1000});
					var fxbgc = new Fx.Style(carts[i], 'background-color', {duration: 1000});

					fxc.start( '#222', color );							
					fxbgc.start( '#fff68f', bgcolor );
					
					
					
					if( parent ) {
						setTimeout( "carts[" + i + "].setStyle( 'background-color', 'transparent' )", 1000 );
					}
					} catch(e) {}
				}
			} catch(e) {}
		}
	}
	var option = { method: 'post', onComplete: callbackCart, data: { only_page:1,page: "shop.basket_short", option: "com_virtuemart" } }
	new Ajax( live_site + '/index2.php', option).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();
}
