// Copyright (c) 2009-2010 Daniel Ifrim

if(!BizCafeShop) var BizCafeShop = {};
BizCafeShop.ArticleSlider = Class.create();

BizCafeShop.ArticleSlider.prototype = {
	initialize: function(options) {
		var slider = this;
		this.options = options || {};
		
		this.basicLogger = (this.options.basic_logger || "my_logger");
		this.logEnable = (this.options.log_enable || false);
		
		this.hideDisplayClass = (this.options.hide_class || "no-display");
		this.showDisplayClass = (this.options.show_class || "show-display");
		this.contentPath = (this.options.content_path || null);
		this.triggerPath = (this.options.trigger_path || null);
		this.triggerHideClass = (this.options.trigger_hide_class || null);
		this.triggerShowClass = (this.options.trigger_show_class || null);
		this.tooltipPath = (this.options.tooltip_path || null);
		this.delay = (this.options.delay || null);
		this.delaySee = (this.options.delay_see || null);
		
		this.contentEls = $$(this.contentPath);
		this.triggerEls = $$(this.triggerPath);
		this.tooltipEls = $$(this.tooltipPath);

		//this.eventClick = this.clickTrigger.bindAsEventListener(this);
		
		this.active_index = null;
		this.next_index = null;
		this.runs = false;
		this.pipe_last = 0;
		this.pipe_current = this.pipe_last;
		
		this.init();
		this.contentEls.each( function(h,i) {
			//Event.observe(h, "mousedown", slider.eventMouseDown);
		});
	},
	init: function() {
		var slider = this;
		var found_active = false;
		this.contentEls.each( function(el,i) {
			if (!found_active && slider.isShown(el, slider.showDisplayClass)) {
				slider.active_index = i;
				slider.setNextIndex();
				found_active = false;
			} else {
				slider.hideEl(el, slider.showDisplayClass, slider.hideDisplayClass);
				slider.hideEl(slider.triggerEls[i], slider.triggerShowClass, slider.triggerHideClass);
			}
		});
		this.triggerEls.each( function(el,i) {
			Event.observe(el, "click", slider.clickTrigger.bind(slider, slider, i));
		});
		if (this.active_index != null)
			this.displayContent(slider, slider.runNewPipe(), this.active_index, false);
	},
	setNextIndex: function() {
		this.next_index = this.active_index + 1;
		if (this.next_index > this.contentEls.length - 1)
			this.next_index = 0;
	},
	getNewPipeId: function() {
		this.pipe_last = this.pipe_last + 1;
		
		return this.pipe_last;
	},
	runNewPipe: function() {
		return this.pipe_current = this.getNewPipeId();
	},
	isShown: function(el, showClass) {
		if (el)
			if (!el.hasClassName(showClass))
				return false;
			else
				return true;
		else
			return false;
	},
	showEl: function(el, showClass, hideClass) {
		if (el) {
			el.removeClassName(hideClass);
			if (!el.hasClassName(showClass)) {
				el.addClassName(showClass);
			}
		}
	}
	,
	hideEl: function(el, showClass, hideClass) {
		if (el) {
			el.removeClassName(showClass);
			if (!el.hasClassName(hideClass))
				el.addClassName(hideClass);
		}
	},
	clickTrigger: function (slider, index) {
		var pipe_local = slider.runNewPipe();
		this.displayContent(slider, pipe_local, index, true);
	},
	displayContent: function (slider, pipe_local, index, clicked) {
		slider.my_log(slider.pipe_current + "==" + pipe_local);
		if (slider.pipe_current == pipe_local && slider.runs == false) {
			slider.runs = true;
			slider.hideEl(slider.contentEls[slider.active_index], slider.showDisplayClass, slider.hideDisplayClass);
			slider.hideEl(slider.tooltipEls[slider.active_index], "sure-show-display", "sure-no-display");
			slider.hideEl(slider.triggerEls[slider.active_index], slider.triggerShowClass, slider.triggerHideClass);
			
			slider.active_index = index;
			
			slider.showEl(slider.contentEls[index], slider.showDisplayClass, slider.hideDisplayClass);
			slider.showEl(slider.tooltipEls[index], "sure-show-display", "sure-no-display");
			slider.showEl(slider.triggerEls[index], slider.triggerShowClass, slider.triggerHideClass);
			
			slider.setNextIndex(slider.active_index);
			slider.runs = false;
			
			var d = 0;
			if (clicked == true)
				d = slider.delaySee;
			else
				d = slider.delay;
			slider.displayContent.delay(d, slider, pipe_local, slider.next_index, false);
		}
	},
	my_log: function(msg) {
		if (this.logEnable == true)
			$(this.basicLogger).innerHTML += "<br />" + msg;
	}
}

BizCafeShop.Acordeon = Class.create();
BizCafeShop.Acordeon.prototype = {
    initialize: function(elem, clickableEntity, checkAllow, options) {
    	
    	this.options = options || {};
    	this.itemHideClass = (this.options.a_item_hide_class || "a-item-hide");
    	this.clickableActiveClass = (this.options.clickable_active_class || "active");
    	this.prlClass = (this.options.prl_class || "");
    	this.prlActiveClass = (this.options.prl_active_class || "main-active");
    	
    	this.clickableEntity = clickableEntity;
    	
        this.container = $(elem);
        this.checkAllow = checkAllow || false;
        this.disallowAccessToNextSections = false;
        this.sections = $$('#' + elem + ' .section');
        
        this.currentSection = (this.options.current_section_id || false);
        
        var headers = $$('#' + elem + ' .section ' + '.' + this.clickableEntity);
        headers.each(function(header) {
            Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
        }.bind(this));
    },

    sectionClicked: function(event) {
    	//if (!$(Event.element(event)).up('.section').hasClassName('section-active')) {
        	this.openSection($(Event.element(event)).up('.section'));
    	//} else {
    	//	this.closeExistingSection();
    	//}
        Event.stop(event);
    },

    openSection: function(section) {
        var section = $(section);

        // Check allow
        if (this.checkAllow && !Element.hasClassName(section, 'section-allow')){
            return;
        }
        if(section.id != this.currentSection) {
            this.closeExistingSection();
            this.currentSection = section.id;
            
            $(this.currentSection).addClassName('section-active');
            this.activateClickable(this.currentSection);
            this.itemShow(section);
            //Effect.SlideDown(contents[0], {duration:.2});

            if (this.disallowAccessToNextSections) {
                var pastCurrentSection = false;
                for (var i=0; i<this.sections.length; i++) {
                    if (pastCurrentSection) {
                        Element.removeClassName(this.sections[i], 'section-allow');
                        this.disableClickable(this.currentSection);
            			this.itemHide(section);
                    }
                    if (this.sections[i].id==section.id) {
                        pastCurrentSection = true;
                    }
                }
            }
        }
    },
    activateClickable: function(section) {
    	var clickable = $$('#' + section + ' ' + '.' + this.clickableEntity);
    	
    	if (!clickable[0].hasClassName(this.clickableActiveClass)) {
    		clickable[0].addClassName(this.clickableActiveClass);
    	}
    	
    	var prl = $$('#' + section + ' ' + '.' + this.prlClass);
    	if (!prl[0].hasClassName(this.prlActiveClass)) {
    		prl[0].addClassName(this.prlActiveClass);
    	}
    },
    disableClickable: function(section) {
    	var clickable = $$('#' + section + ' ' + '.' + this.clickableEntity);
    	
    	if (clickable[0].hasClassName(this.clickableActiveClass)) {
    		clickable[0].removeClassName(this.clickableActiveClass);
    	}
    	
    	var prl = $$('#' + section + ' ' + '.' + this.prlClass);
    	if (prl[0].hasClassName(this.prlActiveClass)) {
    		prl[0].removeClassName(this.prlActiveClass);
    	}
    },
    
    itemShow: function(section) {
    	var item = Element.select(section, '.a-item');
    	item = item[0];
    	if (item.hasClassName(this.itemHideClass)) {
    		//Effect.SlideDown(item, {delay:.2, duration:.7});
    		item.removeClassName(this.itemHideClass);
    	}
    },
    
    itemHide: function(section) {
    	var item = Element.select(section, '.a-item');
    	item = item[0];
    	if (!item.hasClassName(this.itemHideClass)) {
    		//Effect.SlideUp(item, {delay:.2, uration:.7});
    		item.addClassName(this.itemHideClass);
    	}
    },

    closeSection: function(section) {
        $(section).removeClassName('section-active');
        this.disableClickable(this.currentSection);
        this.itemHide(section);
    },

    openNextSection: function(setAllow){
        for (section in this.sections) {
            var nextIndex = parseInt(section)+1;
            if (this.sections[section].id == this.currentSection && this.sections[nextIndex]){
                if (setAllow) {
                    Element.addClassName(this.sections[nextIndex], 'section-allow')
                }
                this.openSection(this.sections[nextIndex]);
                return;
            }
        }
    },

    openPrevSection: function(setAllow){
        for (section in this.sections) {
            var prevIndex = parseInt(section)-1;
            if (this.sections[section].id == this.currentSection && this.sections[prevIndex]){
                if (setAllow) {
                    Element.addClassName(this.sections[prevIndex], 'section-allow')
                }
                this.openSection(this.sections[prevIndex]);
                return;
            }
        }
    },

    closeExistingSection: function() {
        if(this.currentSection) {
            this.closeSection(this.currentSection);
        }
    }
}

BizCafeShop.TooltipProduct = Class.create();
BizCafeShop.TooltipProduct.prototype = {
    initialize: function(id, tooltip_id, options) {
    	this.el = $(id);
    	this.tooltip = $(tooltip_id);
    	if (!$(this.el)) return;
    	if (!$(this.tooltip)) return;
    	//this.tooltip.up().style.position = "relative";

    	this.is_visible = false;
    	this.runs = false;
		this.pipe_last = 0;
		this.pipe_current = this.pipe_last;
    	
    	this.options = options || {};
    	this.showClass = (this.options.delay || 0.1);
    	this.showClass = (this.options.show_class || "tooltip-prod-show");
    	this.hideClass = (this.options.hide_class || "tooltip-prod-hide");
    	this.deltaLeft = (this.options.delta_left || 0);
        
		Event.observe(this.el,'mouseover',this.elMouseOver.bindAsEventListener(this));
		Event.observe(this.el,'mouseout',this.elMouseOut.bindAsEventListener(this));
		
		Event.observe(this.tooltip,'mouseover',this.elMouseOver.bindAsEventListener(this));
		Event.observe(this.tooltip,'mouseout',this.elMouseOut.bindAsEventListener(this));
		
		this.basicLogger = (this.options.basic_logger || "my_logger");
		this.logEnable = (this.options.log_enable || false);
    },
    
    getNewPipeId: function() {
		this.pipe_last = this.pipe_last + 1;
		
		return this.pipe_last;
	},
	runNewPipe: function() {
		return this.pipe_current = this.getNewPipeId();
	},
    
	elMouseOver: function() {
		this.doMouseOver.delay(this.delay, this, this.runNewPipe());
	},
	
    doMouseOver: function(obj, pipe_local) {
    	if (obj.runs == false && obj.pipe_current == pipe_local && obj.is_visible == false) {
    		obj.runs = true;
    		var some_left = Math.abs(obj.el.up().up().cumulativeOffset().left - obj.el.cumulativeOffset().left)
    						+ obj.deltaLeft;
    		obj.tooltip.setStyle({
	    		left: some_left + "px"
	    	});
    		obj.tooltip.addClassName(obj.showClass);
    		obj.tooltip.removeClassName(obj.hideClass);

			obj.is_visible = true;
			obj.my_log("show");
			obj.runs = false;;
    	}
    },
    elMouseOut: function() {
		this.doMouseOut.delay(this.delay, this, this.runNewPipe());
	},
    doMouseOut: function(obj, pipe_local) {
    	if (obj.runs == false && obj.pipe_current == pipe_local && obj.is_visible == true) {
    		obj.runs == true;
    		obj.tooltip.addClassName(obj.hideClass);
    		obj.tooltip.removeClassName(obj.showClass);

    		obj.is_visible = false;
    		
    		obj.my_log("hide");
    		obj.runs == false;
    	}
    },
	my_log: function(msg) {
		if (this.logEnable == true)
			$(this.basicLogger).innerHTML += "<br />" + msg;
	}
}

BizCafeShop.TabProductDetalii = Class.create();
BizCafeShop.TabProductDetalii.prototype = {
    initialize: function(options) {
    	this.options = options || {};
    	this.triggers = (this.options.triggers || []);
    	this.containers = (this.options.containers || []);
    	this.triggerOnClass = (this.options.trigger_on_class || []);
    	this.triggerOffClass = (this.options.trigger_off_class || []);
    	
    	this.initTabs();
    },
    
    initTabs: function() {
    	for (var i=0; i<this.triggers.length; i++)
    		if ($(this.triggers[i])) { this.triggers[i] = $(this.triggers[i]); }
    		else { return; }
    	for (var i=0; i<this.containers.length; i++)
    		if ($(this.containers[i])) this.containers[i] = $(this.containers[i]);
    		else return;
    	for (var i=0; i<this.triggers.length; i++) {
    		if (this.triggers[i].hasClassName(this.triggerOnClass)) {
    			this.activateTab(i);
    			break;
    		}
    	}
    	for (var i=0; i<this.triggers.length; i++) {
    		Event.observe(this.triggers[i], "click", this.clicked.bindAsEventListener(this, i));
    	}
    },
    activateTab: function(ord) {
    	for (var i=0; i<this.triggers.length; i++) {
    		if(i!=ord) {
    			this.triggers[i].removeClassName(this.triggerOnClass);
    			this.triggers[i].addClassName(this.triggerOffClass);
    			this.containers[i].hide();
    		}
    	}
    	this.triggers[ord].removeClassName(this.triggerOffClass);
		this.triggers[ord].addClassName(this.triggerOnClass);
		this.containers[ord].show();
    },
    clicked: function(ev, ord) {
    	this.activateTab(ord);
    }
}
BizCafeShop.HideProductPriceAdditional = Class.create();
BizCafeShop.HideProductPriceAdditional.prototype = {
	initialize: function(path, price_additional_class) {
		var els = $$(path);
		this.price_additional_class = price_additional_class;
		for(var i=0; i<els.length; i++) {
			Event.observe(els[i],'change',this.bubu.bindAsEventListener(this));
		}
	},
	bubu: function(ev) {
		var padds = $$("." + this.price_additional_class);
		for(var i=0; i<padds.length; i++) {
			padds[i].hide();
		}
	}
}
BizCafeShop.AdresaTippers = Class.create();
BizCafeShop.AdresaTippers.prototype = {
    initialize: function(options) {
    	this.options = options || {};
    	this.tippers = ($(this.options.tippers) || null);
    	this.sufix = (this.options.sufix || null);
    	this.company = ($(this.options.company) || null);
    	this.company_container = ($(this.options.company + this.sufix) || null);
    	this.cui = ($(this.options.cui) || null);
    	this.cui_container = ($(this.options.cui + this.sufix) || null);
    	this.regcom = ($(this.options.regcom) || null);
    	this.banca = ($(this.options.banca) || null);
    	this.banca_container = ($(this.options.banca + this.sufix) || null);
    	this.myiban = ($(this.options.myiban) || null);
    	
    	Event.observe(this.tippers,'change',this.eventChangeTippers.bindAsEventListener(this, this));
    	
    	this.initView(this);
    },
    initView: function() {
    	var adresa = this;
    	adresa.changeView(adresa);
    },
    eventChangeTippers: function(event, adresa) {
    	adresa.changeView(adresa);
    },
    changeView: function(adresa) {
    	if (adresa.tippers.value == 1) {
    		adresa.company.value = "";
    		adresa.cui.value = "";
    		adresa.regcom.value = "";
    		adresa.banca.value = "";
    		adresa.myiban.value = "";
    		
    		adresa.company_container.hide();
    		adresa.cui_container.hide();
    		adresa.banca_container.hide();
    	} else {
    		adresa.company_container.show();
    		adresa.cui_container.show();
    		adresa.banca_container.show();
    	}
    }
}

BizCafeShop.BccAcordeon = Class.create();
BizCafeShop.BccAcordeon.prototype = {
    initialize: function(container, options) {
    	
    	this.options = options || {};
    	if (!$(container)) return;
    	
    	this.clickableClass = (this.options.clickable_class || "trigger");
    	this.clickableExpandedClass = (this.options.clickable_expanded_class || "trigger-expanded");
    	this.itemClass = (this.options.item_class || "content");
    	this.itemHideClass = (this.options.item_hide_class || "content-hide");
    	
    	this.expandUI = ($(this.options.expand) || undefined);
    	this.collapseUI = ($(this.options.collapse) || undefined);
    	
        this.clickables = $$('#' + container + '  ' + '.' + this.clickableClass);
        this.clickables.each(function(clickable) {
            Event.observe(clickable,'click',this.sectionClicked.bindAsEventListener(this));
        }.bind(this));
        
        this.initView();
        
        if (this.expandUI != undefined)
        	Event.observe(this.expandUI,'click',this.expandAll.bindAsEventListener(this));
        if (this.collapseUI != undefined)
        	Event.observe(this.collapseUI,'click',this.collapseAll.bindAsEventListener(this));
    },
    initView: function() {
    	for (var i=0; i<this.clickables.length; i++) {
    		if (this.isExpanded(this.clickables[i])) {
    			this.showContent(this.clickables[i]);
    		} else {
    			this.hideContent(this.clickables[i]);
    		}
    	}
    },
    sectionClicked: function(ev) {
    	var clickable = Event.element(ev);
    	if (this.isExpanded(clickable)) {
			this.hideContent(clickable);
		} else {
			this.showContent(clickable);
		}
    },
    isExpanded: function(clickable) {
    	if (clickable.hasClassName(this.clickableExpandedClass)) {
    		return true;
    	} else {
    		return false;
    	}
    },
    showContent: function(clickable) {
    	if (!this.isExpanded(clickable)) {
    		clickable.addClassName(this.clickableExpandedClass);
    	}
    	var content = clickable.up().down('.' + this.itemClass);
    	if (content.hasClassName(this.itemHideClass)) {
    		content.removeClassName(this.itemHideClass);
    	}
    },
    hideContent: function(clickable) {
    	if (this.isExpanded(clickable)) {
    		clickable.removeClassName(this.clickableExpandedClass);
    	}
    	var content = clickable.up().down('.' + this.itemClass);
    	if (!content.hasClassName(this.itemHideClass)) {
    		content.addClassName(this.itemHideClass);
    	}
    },
    expandAll: function(ev) {
    	for (var i=0; i<this.clickables.length; i++) {
    		this.showContent(this.clickables[i]);
    	}
    },
    collapseAll: function(ev) {
    	for (var i=0; i<this.clickables.length; i++) {
    		this.hideContent(this.clickables[i]);
    	}
    }
}

BizCafeShop.ClubChildConditions = Class.create();
BizCafeShop.ClubChildConditions.prototype = {
	initialize: function(options) {
		this.options = options || {};
		this.trigger = ($(this.options.trigger) || null);
		if (!this.trigger) return
		if (!this.options.child_data) return;
		this.conditionValue = (this.options.condition_value || 0);
		if (!this.conditionValue > 0) return;
		this.childData = (this.options.child_data || null);
		if (this.childData.length <= 0) return
		this.inst = this;
		this.suma_after_validation = 0;
		
		/*this.init(this.inst);*/
	},
	
	/*init: function(inst) {
		Event.observe(inst.trigger, "submit", inst.submitTrigger.bind(inst, inst));
	},
	
	submitTrigger: function (inst, ev) {
		Event.stop(ev);
	},*/
	
	validateConditions: function() {
		var ret = false;
		this.suma_after_validation = 0;
		for(var i = 0; i < this.childData.length; i++) {
			var d = this.childData[i];
			var chk = $(d.id);
			if (!chk) continue;
			if (chk.disabled == true) continue;
			this.suma_after_validation += d.condition * chk.value;
		}
		if (parseInt(this.suma_after_validation) >= parseInt(this.conditionValue)) {
			ret = true;
		}
		return ret;
	}
}
