﻿
var ARGUS = {
	popup: function(sHref, nMode){
		var winPopup = [];
	
		switch(nMode){
			case 1: // RTE
				winPopup[nMode] = window.open(sHref, 'winPopup' + nMode, 'dependant=yes, toolbar=no, status=no, menubar=no, scrollbars=yes, movable=yes, resizable=yes, width=600, height=500');
				break;
			default:
				winPopup[nMode] = window.open(sHref, 'winPopup' + nMode, '');
				break;
		}
	},
	
	checkEmail: function(sEmail){
		var reFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (reFilter.test(sEmail)){
			return true;
		}else{
			return false;
		}
	},
	
	centerWindow: function(oWindow){
		var nY = new Number((window.screen.availHeight - oWindow.document.body.offsetHeight) / 2 - 200);
		var nX = new Number((window.screen.availWidth - oWindow.document.body.offsetWidth) / 2);
		oWindow.moveTo(nX, nY);
	},
	
	fitWindow: function(oContainer){
		window.resizeTo(oContainer.offsetWidth + 20, oContainer.offsetHeight + 85);
	},
	
	centerElement: function(oElement){
		oElement = $(oElement);
		var nY = window.getScroll().y + (window.getSize().y / 2);
		oElement.setStyle('top', nY - (oElement.getSize().y / 2));
		oElement.setStyle('left', (window.getSize().x / 2) - (oElement.getSize().x / 2));
	},
	
	setDefaultText: function(oElement, bMode){
		var sText = oElement.getProperty('default-text');
		if(bMode){
			if(oElement.value == sText) oElement.value = '';
		}else if(oElement.value.length < 1) oElement.value = sText;
	},
	
	popupImage: function(oRef, bBlink){
		oRef = $(oRef);
		if(bBlink && !$('opusPopupImage')){
			var oImg = new Element('img').setStyles({
				position: 'absolute',
				width: 500,
				opacity: '0',
				'cursor': 'pointer',
				'border': 'solid 1px #999',
				zIndex: '60'
			}).setProperty('id', 'opusPopupImage').injectInside(document.body);
			
			var oBlink = new Fx.Morph(oImg, {duration: 500, transition: 'expo:out'});
			
			oImg.src = oRef.src.replace('&width=145', '&width=500').replace('&width=165', '&width=500');
			//alert(oImg.src);
			oImg.addEvents({
				'load': function(){
					ARGUS.centerElement(oImg);
					oBlink.start({
						left: [oRef.getPosition().x, oImg.getPosition().x],
						top: [oRef.getPosition().y, oImg.getPosition().y],
						width: [oRef.getSize().x, oImg.getSize().x],
						height: [oRef.getSize().y, oImg.getSize().y],
						'opacity': [0, 1]
					});
				},
				'click': function(){
					ARGUS.centerElement(oImg);
					oBlink = new Fx.Morph(oImg, {duration: 500, transition: 'expo:in'})
					oBlink.start({
						left: [oImg.getPosition().x, oRef.getPosition().x],
						top: [oImg.getPosition().y, oRef.getPosition().y],
						width: [oImg.getSize().x, oRef.getSize().x],
						height: [oImg.getSize().y, oRef.getSize().y],
						'opacity': [1, 0]
					}).chain(function(){
						oImg.dispose();
					});
				}
			});
		}else{
			$('opusPopupImage').fireEvent('click');
		}
	},
	
	querystring: document.location.search.substring(1).parseQueryString()

}

var Search = {
	fetch: function(sTarget, sValue){
		new Request.HTML({
			url: 'Shop.aspx', 
			method: 'post',
			evalScripts: true,
			update: $(sTarget)
		}).send('txtCatID=' + sValue + '&txtSubCatID=' + ARGUS.querystring.txtSubCatID + '&txtActionID=201');
	},
	submit: function(sSessionID){
		var nCatID = $('txtHpCatID').value;
		var nSubCatID = $('txtHpSubCatID').value;
		var sFilter = $('txtHpFilter').value == $('txtHpFilter').getProperty('default-text') ? '' : $('txtHpFilter').value;
		
		if(nCatID == 0){
			alert('נא בחר/י חברה');
		}else{
			location.href = 'Default.aspx?txtCatID=' + nCatID + '&txtSubCatID=' + nSubCatID + '&txtFilter=' + sFilter + '&txtSID=' + sSessionID;
		}
	}
}

Element.extend({
	clearValue: function(){
		switch(this.getTag()){
			case 'select':
				$each(this.options, function(option){
					if (option.selected) option.selected = false;
				});
			case 'input':
				if (this.checked || ['hidden','submit','button'].contains(this.type)) {
					if (['checkbox', 'radio'].contains(this.type)) this.checked = false;
					break;
				}
			case 'textarea': this.value = '';
		}
		return false;
	},
 
	clearFormElements: function(){
		this.getFormElements().each(function(el){ el.clearValue() });
	}
 
});

window.addEvent('domready', function(){
	if($('txtHpCatID')) if($('txtHpCatID').value > 0) Search.fetch('txtHpSubCatID', $('txtHpCatID').value);
	Shop.updateCart();
	
	var oFlash = [];
	$$('.imageAsFlash').each(function(oItem, nIndex){
		var nWidth = oItem.getProperty('width') > 0 ? oItem.getProperty('width') : 200;
		var nHeight = oItem.getProperty('height') > 0 ? oItem.getProperty('height') : 150;
		var sSrc = oItem.getProperty('src');
		var oContainer = $('flash_' + nIndex) ? $('flash_' + nIndex) : $(oItem.parentNode);
		var oWrap = new Element('div').setProperties({
			'id': 'flash_' + nIndex
		}).inject(oItem, 'before');

		if(sSrc.length > 0){
			oFlash[nIndex] = new Swiff(sSrc, {
				id: 'flashMain' + nIndex,
				width: nWidth,
				height: nHeight,
				container: oContainer
			});
			oItem.destroy();
		}
	});
});

window.addEvent('load', function(){

});

window.addEvent('resize', function(){

});



