/*
-------------------------------------------------------------------------
#
# JellyBelly JavaScript
# A honlaphoz szükséges JavaScript függvények.
#
# Írta: Bodonyi László
# E-mail cím: bodonyi.laszlo@profartis.hu
#
# Cégnév: Profartis Design Kft.
# Honlap: http://www.profartis.com
#
-------------------------------------------------------------------------
*/

	/*
	 *    Beviteli mezőkben lévő szöveg eltűnés/megjelenítés
	 * 	  written by Profartis Design Kft.
	 */
	function inputToggle(event,text)
	{
		var El = Event.element(event);
		if(!text){ text = El.readAttribute('title'); }
		if(event.type == 'focus' && El.value == text){  El.value = ''; if(El.name=='s'){ El.addClassName('active'); }  }
		if(event.type == 'blur' && El.value.blank()){  El.value = text; if(El.name=='s'){ El.removeClassName('active'); }}
	}
	
	function changeTab(nr)
	{
		$$('.webaruhaz_tabs_ul li a').invoke('removeClassName','active-tab');
		$$('.webaruhaz_tabs_ul li a')[nr-1].addClassName('active-tab');
		$$('.sb-web-tab').invoke('hide');
		$$('.sb-web-tab')[nr-1].show();
	}
	
	
	/*
	 *    Termékek kijelölése kosárban
	 * 	  written by Profartis Design Kft.
	 */
	
	function selectAllProduct(o)
	{
		var state = (o.rel == 'inverse') ? '' : 'checked';
		o.rel = !state ? '' : 'inverse';
		$$('.check').each(function(i){ i.checked = state; });			
	}
	
	function productUrl( url, orderby, order, limit )
	{
		var tags = new Array();		
		$$( '#filter-gyumolcs', '#filter-kategoria', '#filter-kiszereles' ).each(function(i){
			if( i.value ){ tags.push( i.value ); }
		});		
		var category = $('filter-palinkafozde').value;		
		var tags = !tags.join(',') ? '-' : tags.join(',');
		document.location.href = '/termekek/'+category+'/'+tags+'/'+orderby+'/'+order+'/'+limit+'/1';			
	}
	
	
	var Profartis = {};
	
	
	/*
	 *    Értékelés script
	 * 	  written by Profartis Design Kft.
	 */
	var Stars = {
	
		SetOriginal : function(obj){			
			Stars.Over(obj,obj.rel);
		},	
	
		Over : function(obj,n){
			var obj = (obj.className=='rating-stars') ? obj : obj.parentNode;
			$(obj).select('img').each(function(item,index){
				if ( ((index+1)/2) <= parseFloat(n))
					Stars.Change(item, 'over', (String(item.alt).indexOf(".") > -1 ? 'left' : 'right'));
				else
					Stars.Change(item, 'blank', (String(item.alt).indexOf(".") > -1 ? 'left' : 'right'));
			});			
		},
	
		Change : function(obj,status,type){
			obj.src = obj.src.replace(/rating_stars_(.*)\.png$/,'rating_stars_'+status+'_'+type+'.png');
		},				
		
		Set: function(obj, n, cookie){
			var obj = (obj.className == 'rating-stars') ? obj : obj.parentNode;
			$(obj).rel = n;
			if (!cookie) { $(obj).select('.rating-value')[0].value = n; }
			else { PM_SetCookie('comment_product_rating', n, 1); }
		}
		
	}
	
	function PM_SetCookie(name,value,days)
	{
		var exdate = new Date();
	    exdate.setDate(exdate.getDate() + days);
	    document.cookie = name + "=" + value + ";path=/;expires=" + exdate.toGMTString();
	}
	
	
	
	/*
	 *    Lenyíló menü
	 * 	  written by Profartis Design Kft.
	 */
	
	var SubMenu = {
		
		dropThis : function(nr)
		{
			$('submenu-'+nr).style.display = 'block';
		},
		
		hideThis : function(nr)
		{
			$('submenu-'+nr).style.display = 'none';
		}
		
	}
	
	/*
	 *    Textarea mező függőleges méretezése
	 * 	  written by Profartis Design Kft.
	 */
	Profartis.TextArea_Resize = Class.create({
		
		textareaObject : null,
		originalHeight : null,
		originalTop : null,
		minHeight: 58,
		
		initialize : function(element,event,minheight){
			
			this.textareaObject = $(element.parentNode).select('textarea')[0];			
			this.originalHeight = this.textareaObject.offsetHeight;
			this.originalTop = Event.pointerY(event);
			this.minHeight = minheight ? minheight : this.minHeight;
			
			this.eventMouseMove = this.MouseMove.bindAsEventListener(this);
			document.observe('mousemove', this.eventMouseMove); 
			
			this.eventMouseUp = this.MouseUp.bindAsEventListener(this);
			document.observe('mouseup', this.eventMouseUp); 
			
		},
		
		MouseMove : function(e){
			var nH = (this.originalHeight+(Event.pointerY(e)-this.originalTop));
			this.textareaObject.setStyle({
				height: (nH < this.minHeight ? this.minHeight : nH) + 'px'
			});
		},
		
		MouseUp : function(){
			Event.stopObserving(document, 'mouseup', this.eventMouseUp);
			Event.stopObserving(document, 'mousemove', this.eventMouseMove);
		}
		
	});
	
	
	/*
	 *    Űrlap ellenőrzés
	 * 	  written by Profartis Design Kft.
	 */
	function validateForm(f,s)
	{
		var filled = true, validEmail = true, accepted = true, focusThis = false;					
		$(f).select('*').invoke('removeClassName','error').invoke('removeClassName','textarea_error');
				
		$(f).select(".required").each(function(item)
		{		
			if (filled == true) {
			
				// Beviteli mezők ellenőrzése				
				if (!item.value || (item.title && item.title == item.value)) {
					filled = false;
				}
				
				// Ha e-mail címet vár a beviteli mező, akkor a helyesség ellenőrzése
				if (item.name.search('email') > -1) {
					if (validEmail) {
						validEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(item.value);
					}
				}
				
				// Ha checkbox-ot találtunk, pipálás ellenőrzése
				if (item.type == 'checkbox' && !item.checked) {
					accepted = false;
				}
				
				// Ha bármi hiba történt, beviteli mező megjelölése 'error' class-szal
				if ((!filled || !accepted || !validEmail) && !focusThis) {
					focusThis = item;
					if (item.type != 'checkbox') {
						//if (item.type != 'textarea') {
							item.addClassName('error');
						//}
						/*else {
							item.parentNode.addClassName('textarea_error');
						}*/
					}
				}
				
			}
			
		});																				
	
		// Ha minden rendben van...
		if(filled && validEmail && accepted)
		{ 		
			// Captcha ellenőrzése	
			var Captcha = $(f).select('#anti-spam')[0];					
			if( Captcha )
			{				
				new Ajax.Request('/captcha-checker.php', {
					method: 'post',
					parameters: '&name=' + f.id.replace('form-','') + '&value=' + Captcha.value,
					onComplete: function(t){						
						if (t.responseText == 1 ){	
							if (typeof(s) == 'function') {
								s();
							}
							else {			
								$(f).submit();
							}
						} else {							
							alert('Hibás ellenőrző kód!');
							Captcha.focus();
						}
					}
				});	
				return false;		
			}
			else {
				if (typeof(s) == 'function') {
					s();
				}
				else {
					$(f).submit();
				}
			}	
			return false;		
		}
		
		// Ha hiba történt
		else 
		{			
			if(!filled && validEmail){ alert('A *-gal jelölt mezők kitöltése kötelező!'); } 
			else if( !validEmail ){ alert('Hibás e-mail cím!'); }
			else { alert(focusThis.title); }			
			focusThis.focus();
			return false;
		}
	}
	
	Element.addMethods({
		
		validateForm : function(element,submit)
		{
			return validateForm(element,submit);
		}
		
	});
	
	function ForFriend()
	{
		new Ajax.Request('/post-forward.php', {
			method: 'post',
			parameters: $('form-to-friend').serialize(),
			onComplete: function(t){
				alert(t.responseText);
				if(t.responseText.search('sikeresen') > -1){
					ForwardThis.Close();
				}
			}
		});
	}
	
	
	/*
	 *    TabSlideShow 1.0
	 *    written by Profartis Design Kft.
	 */
		
	var SlideTabs;
	var TabSlideShow = function(options)
	{
			
		this.active = 0;
		this.executer = null;
		this.elements = options.elements;
		this.contents = options.contents;
		this.counter = $$(this.elements).length-2;
		this.step = options.step ? options.step : 10;						
		
		this.play = function(t)
		{						
			this.executer = new PeriodicalExecuter(this.next.bind(this), this.step);
			$$(this.elements+'.controls .play')[0].hide();
			$$(this.elements+'.controls .pause')[0].show();
		}						
		
		this.pause = function()
		{				
			this.executer.stop();
			$$(this.elements + '.controls .pause')[0].hide();
			$$(this.elements + '.controls .play')[0].show();	
		}
			
		this.next = function(manual)
		{				
			if(manual=='m' && this.executer){ this.pause(); }
			this.active = (this.active != this.counter) ? this.active+1 : 0;
			this.doIt();
		}
			
		this.prev = function(manual)
		{
			if(manual=='m' && this.executer){ this.pause(); }
			this.active = (this.active != 0) ? this.active-1 : this.counter;
			this.doIt();
		}
			
		this.jump = function(nr)
		{
			this.active = nr;
			if(this.executer){ this.pause(); }			
			this.doIt();
		}
			
		this.doIt = function()
		{						
			// Change tab
			$$(this.elements+' a').invoke('removeClassName','active-tab');
			$$(this.elements+' a')[this.active].addClassName('active-tab');
			
			// Change content
			$$(this.contents).invoke('hide');
			if ($$(this.contents)[this.active]) {
				$$(this.contents)[this.active].setStyle({ display: 'block' });
			}
	
		}
			
		// Auto start 
		if(options.autoplay){ this.play(); }			
		
		// onClick events			
		$$(this.elements).each(function(e,i){
			if (!e.hasClassName('controls')) {
				e.observe('click', function(){ this.jump(i); }.bind(this));
			}
		}.bind(this));
			
	}
	
	
	/*
	 * 	  Termék gyorskereső
	 * 	  written by Profartis Design Kft.
	 */

	var ProductBrowser = {
						
		slider : false,
		number : 0,	
		
		Init : function()
		{		
			ProductBrowser.slider = new Glider('product-browser', {
				duration: 0.7,
				initialSection: $$('section')[0]
			});																		
		},
		
		Next : function()
		{								
			ProductBrowser.slider.next();
		},
	
		Previous : function()
		{
			ProductBrowser.slider.previous();	
		}
		
	}
	
	
	/*
	 *    Terméklista szűrés
	 * 	  written by Profartis Design Kft.
	 */
	function FilterProductList(url)
	{
		var category = $$('.product-list-filter .category')[0].value;
		var tag = $$('.product-list-filter .flavor')[0].value;
		var orderby = $$('.product-list-filter .orderby')[0].value;
		window.location = url.replace('#category#',category).replace('#tag#',tag).replace('#orderby#/#order#',orderby);
	}
	
	
	/*
	 * 	 CopyAddress, FillAddress
	 */	
	 
	function CopyAddress(c)
	{
		if (c.checked) {
			$('s_nev').value = $('c_nev').value;
			$('s_iranyitoszam').value = $('c_iranyitoszam').value;
			$('s_varos').value = $('c_varos').value;
			$('s_utca').value = $('c_utca').value;
		} else {
			$('s_nev').value = $('s_nev').title;
			$('s_iranyitoszam').value = $('s_iranyitoszam').title;
			$('s_varos').value = $('s_varos').title;
			$('s_utca').value = $('s_utca').title;	
		}
	}
	
	function FillAddress(w,a)
	{			
		a = a.split('|');
		$(w+'_nev').value = a[0];
		$(w+'_iranyitoszam').value = a[1];
		$(w+'_varos').value = a[2];
		$(w+'_utca').value = a[3];
	}
	
	
	function sendMessage(name,w)
	{
		$('sendMessageForm').action = 'mailto:'+name+(!w ? '@szicsek.hu' : '@szicsek.hu');
		$('sendMessageForm').submit();
	}
	
	function SearchTab(nr,obj)
	{
		obj.style.color = '';
		$('search-tab-'+(nr==1?2:1)).style.color = '#A9B4AE';
		$('search-content-'+nr).show();
		$('search-content-'+(nr==1?2:1)).hide();
	}
	
	function replyComment(id)
	{
		$('comment_parent').value = id;
		var url = document.location.href.search('#') ? document.location.href.split('#') : new Array(document.location.href);
		document.location = url[0]+'#respond';
	}

	