/** ******** HOT FLASH ********** */

var hot_flash = function ( text, duration ) {
	if(!duration) {
		duration= 2000;
	}
	$('alert_box_message').set('html', text);
	$('alert_box').setStyle('top',window.getScrollTop() + 150);
	$('alert_box').fireEvent('open_close', duration );
}

var alert_running = false;

window.addEvent('domready', function() {
	var alert_open = new Fx.Morph($('alert_box'), { link: 'ignore', transition: 'quad:in:out' });
	var alert_fade_in;
	var alert_fade_out;
	
	if($('termek_jobb_box')) {
		var top = $('termek_jobb_box').getSize().y;
		
		if(top < $('termek_bal_box').getSize().y) {
			top = $('termek_bal_box').getSize().y;
		}
		
		$('termek_jobb_box').getChildren().each(function(item, index){
		 top -= item.getSize().y;
		});
		
		$('termek_jobb_arbox').setStyle("margin-top",top);
	}
	
	$('alert_box').addEvents({
		
		'open': function() { 
			alert_running = true;
			alert_open.set('.alert_fade_open');
			alert_open.start({opacity:[0,1]});
		},
		'close': function() {
			if( alert_running ) {
				alert_running = false;
				alert_open.set('.alert_fade_close');
				alert_open.start( { opacity: [1,0] } );
			}
		},
		'open_close': function( ms ){  
		   	$('alert_box').fireEvent('open');
			setTimeout("$('alert_box').fireEvent('close');",3000);
		},
	   'click': function() {
			$('alert_box').fireEvent('close');
		}
	});	

	window.addEvents({
			keydown: function() {
				$('alert_box').fireEvent('close');
			}
	});
});

/** ****** HOT FLASH end ***************** */
/**
 * 
 * Base64 encode / decode http://www.webtoolkit.info/
 * 
 */
 
var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

/** ********************************************************************************************** */


function validate_email(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   return reg.test(email);
}

window.addEvent('domready', function() {
	
	/* menü */
	$$('#fo ul').each(function(item)  {
		if(item.getParent().hasClass('off')) {
			item.hide();
		}
	});
	
	/*
	 * $$('#fo li a').each(function(item) {
	 * item.addEvent('click',function(event) {
	 * this.getParent().getChildren("ul").each(function(el) { el.toggle(); });
	 * 
	 * if(this.getParent().getChildren("ul").length > 0) { event.stop(); } });
	 * });
	 */
	
	/* terméklista */
	$$('div.box_green_big div.termekek').each(function(item,index) {
		if(index % 2 == 1) {
			item.addClass('zold_hatter');
		}
	});
	
	/* kosár */
	$$('div.kosar div.termek').each(function(item,index) {
		if(index % 2 == 1) {
			item.addClass('szines');
		}
	});	
	
	$$('#kosar-tabla tr').each(function(item,index) {
		if(index % 2 == 1) {
			item.addClass('szines');
		}
	});		
	
	
	if($('kiskepek')) {
		$$('#kiskepek div').getLast().setStyle('margin','0');
	}
});

window.addEvent('load', function() {
	
	$$('div.box_green_big div.csoport a img').each(function(item) {
		var ml = (160 - item.getSize().x) / 2;
		item.setStyle('margin-left',ml);
	});
	
	$$('div.box_green_big div.termek_lista div.termek div.kep img').each(function(item) {
		var hor = (120 - item.getSize().x) /2;
		var ver = (120 - item.getSize().y) /2;
		
		item.setStyle('padding-left',hor);
		item.setStyle('padding-top',ver);
	}); 
	
	$$('div.termek #kiskepek img').each(function(item) {
		elhelyez(item);
	}); 
	
	$$('#kosar-tabla img').each(function(item) {
		elhelyez(item);
	}); 
	
	$$('div.top_termek img').each(function(item) {
		elhelyez(item);
	});
	
	/* termék adatlap */
	if($$('div.termek #fokep').length > 0) {
		elhelyez($('fokep'));
		setTimeout("z = new Zoomer('fokep');",300);
	}

	
});

function elhelyez(item) {
	var parent = item.getParent();
	if(parent.get('tag') == 'a') {
		parent = parent.getParent();
	}
	
	var hor = Math.floor((parent.getStyle('width').toFloat() - item.getSize().x.toFloat()) /2);
	var ver = Math.floor((parent.getStyle('height').toFloat() - item.getSize().y.toFloat()) /2);
	
	item.setStyle('padding-left',hor);
	item.setStyle('padding-right',hor);
	item.setStyle('padding-top',ver);
	item.setStyle('padding-bottom',ver);
}

function fokep_csere(img) {
	$('fokep').fade('out');
	$('fokep_container').empty();
	
	var newimg = new Element('img', {src: img.src.replace('tiny','small'), big: img.get('big'), id: 'fokep'});
	
	// var bigimg = new Element('img', {src: img.get('big'), 'events': {'load':
	// function(){ } } });
	
	newimg.setStyle('opacity','0');
	$('fokep_container').appendChild(newimg);
	
	if(!$('fokep').complete) {
		$('fokep').addEvent('load', function(){
			$('fokep').fade('in');
			elhelyez(this);
			setTimeout("z.initialize($('fokep'));",1000);
		}.bind($('fokep')));
	}
	else {
		$('fokep').fade('in');
		elhelyez($('fokep'));
		setTimeout("z.initialize($('fokep'));",1000);
	}
	
}
function keypress_product_ujraszamol(event,el) {
	var keyCode = event.keyCode ? event.keyCode : event.which;
	if(keyCode == 13) {
		return false;
	}
	setTimeout('product_ujraszamol();',200);
	setTimeout('fix_float("' + el.id +'")',100);
}

function fix_float(element_id) {
	var el = $(element_id);

	el.value = el.value.replace(",",".");
	var f  = parseFloat(el.value);
	if(! isNaN(f)) {
		// el.value = f;
	}
	else {
		el.value = '';
	}
}

vasarolhato = false;

function product_ujraszamol() {
	$('termek_jobb_arbox').fade("out");
	$('din_props').set('send',{
		onComplete: function(response) { 
		var ret = JSON.decode(response);
		if(ret.hiba == 'hiba') {
			alert('A feldolgozás során hiba történt! Kérjük indítsa újra  böngészőjét!');
		}
		else {
			$('termek_jobb_arbox').fade("in");
			$('teljes_ar').set('html',ret.listaar);
			$('ar').set('html',ret.ar);
			$('props-store').value = JSON.encode(ret.props);
			
			if(ret.merethiba) {
				if($('egyedi-hiba')) {
					$('egyedi-hiba').set('html',ret.merethiba);
				}
			}
			else {
				if($('egyedi-hiba')) {
					$('egyedi-hiba').set('html','');
				}
			}
			vasarolhato = ret.vasarolhato;
		}
	}});
	
	$('din_props').send();
}


function product_kosarba() {
	$('din_props').set('send',{
		onComplete: function(response) {
		var ret = JSON.decode(response);
		if(ret.hiba == 'hiba') {
			alert('A feldolgozás során hiba történt! Kérjük indítsa újra  böngészőjét!');
		}
		else {
			
			$('teljes_ar').set('html',ret.listaar);
			$('ar').set('html',ret.ar);
			$('props-store').value = JSON.encode(ret.props);
			
			if(ret.merethiba && ($('egyedi-hiba')) ) {
				$('egyedi-hiba').set('html',ret.merethiba);
			}
			else if($('egyedi-hiba')) {
				$('egyedi-hiba').set('html','');
			}
			
			vasarolhato = ret.vasarolhato;
			
			if(vasarolhato) {
				$('vasarlas').submit();
			}
		}
	}});
	
	$('din_props').send();
}

function meretek_box(id) {
	new MooDialog.Iframe('/meretek.php?id='+id,{
		size: {
			width: 800,
			height: 600
		},
		scroll: false 
	});	
	
	return false;
}

function keypress_egyedi(event,el) {
	var keyCode = event.keyCode ? event.keyCode : event.which;
	if(keyCode == 13) {
		return false;
	}
	setTimeout('product_ujraszamol();',200);
	setTimeout('fix_int("' + el.id +'")',100);
}

function fix_int(element_id) {
	var el = $(element_id);

	el.value = el.value.replace(",","");
	el.value = el.value.replace(".","");
	var f  = parseInt(el.value);
	if(! isNaN(f)) {
		// el.value = f;
	}
	else {
		el.value = '';
	}
}

function kereses() {
	if($('kereso-input').value != 'Termék Neve...') {
		url = "/kereso/" + Base64.encode($('kereso-input').value);
		document.location.href = url;
	}
}

function leiras(id) {
	$$('#reszletes_leiras_tartalom div').each(function(item) {
		if(item.getParent().id == "reszletes_leiras_tartalom") {
			if(item.id != id) {
				item.setStyle('display','none');
			}
			else {
				item.setStyle('display','block');
			}
		}
	});
	
	$$('#reszletes_leiras_felso div').each(function(item) {
		if(item.id != id + '_felso') {
			item.removeClass("aktiv");
			item.addClass("inaktiv");
		}
		else {
			item.removeClass("inaktiv");
			item.addClass("aktiv");			
		}
	});
	return false;
}

