$(document).ready(function(){
	leftNavEfx ();
	setOverflow ();
	navKockakEfx ();
	if (typeof shopId != "undefined"){
		initAjaxXml ();
		comingSoon ();
	}
	showHideNL ();
	subscribeAjax ();
	unsubscribeAjax ();
	window.onresize = function () {
		setOverflow ();
	}
	$('#scrolltotop').click (function() {
		$.scrollTo( 0, 800, {queue:true} );
		return false;
	});
});
$('#storeCont').html('');
// Ajax XML
function parseXml(xml) {
	$('#terkepbtn').click(function() {
		$(xml).find("Store").each(function() {
			if ($(this).attr("name") == shopId) {
				var gmapinc = $(this).find("Map").text();
				// gmapinc = xml: "Map"
				$('#storeCont').html('<iframe width="459" height="296" frameborder="0" scrolling="no" src="'+gmapinc+'"></iframe>');
			}
		});
		return false;
	});
	function getInfo () {
		$(xml).find("Store").each(function() {
			if ($(this).attr("name") == shopId) {
				var shopInfo = $(this).find("Info").text();
				// shopInfo = xml: "Info"
				$('#storeCont').html('<p>'+shopInfo.replace(/\n/g, "<br />")+'</p>');
			}
		});
	}
	getInfo ();
	$('#shopbtn').click(function() {
		getInfo ();
		return false;
	});
}
function showLoading(place) {
	if (place == '#response' && $('#response:hidden')) {
		$('#response').fadeIn();
		$(place).html('<img id="loading" src="/images/ajax-loader.gif" width="32" height="32" />');
	}
	if (place == '#respnl' && $('#respnl:hidden')) {
		$('#respnl').fadeIn();
		$(place).html('<img id="loading" src="/images/ajax-loader-blue.gif" width="32" height="32" />');
	} else {
		$(place).html('<img id="loading" src="/images/ajax-loader.gif" width="32" height="32" />');
	}
}
initAjaxXml = function () {
	  $.ajax({
		type: "GET",
		url: "/xml/shops.xml",
		dataType: "xml",
		beforeSend: showLoading('#storeCont'),
		success: parseXml
	  });	
}
// accordion left side navigation
leftNavEfx = function () {
	// termekek click
	$('#termekbtn').click(function() {
	   return false;
	});
	// hover accordion
	$('#boltbtn, #boltmenu').hover(
		function() {
			$('#boltmenu').stop().animate({"height": "160", "padding-bottom": "20", "margin-bottom": "-20"}, "slow");
		},
		function() {
			$('#boltmenu').stop().animate({"height": "0", "padding-bottom": "0", "margin-bottom": "20"}, "slow");
		});
	$('#termekbtn, #termekmenu').hover(
		function() {
			$('#termekmenu').stop().animate({"height": "200", "padding-bottom": "20", "margin-bottom": "-20"}, "slow");
		},
		function() {
			$('#termekmenu').stop().animate({"height": "0", "padding-bottom": "0", "margin-bottom": "20"}, "slow");
		});
}
// hoverFade efx on "kocka"
navKockakEfx  = function () {
	// set the right image
	if ($('body').hasClass("top10")){
		var kocka = "kocka_hover_topten.png";
		
		// top1 hover
		$('<div class="holder-top1"><img class="hoverimg" src="/images/top1_hover.png" width="209" height="234" /></div>').insertAfter("#top1");
		$('<div class="holder"><img class="hoverimg" src="/images/'+kocka+'" width="162" height="162" /></div>').insertAfter(".menuimg");
	} else if ($('body').hasClass("home")){
		var kocka = "kocka_hover_border.png";
	} else if ($('body').hasClass("termekek")){
		var kocka = "kocka_hover_border_kek.png";
	} else {
		var kocka = "kocka_hover_line.png";
	}
	// insert all hover images
	$('<div class="holder"><img class="hoverimg" src="/images/'+kocka+'" width="153" height="153" /></div>').insertAfter(".menuimg");
	//test for MSIE x.x;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
		// capture x.x portion and store as a number
		var ieversion = new Number(RegExp.$1);
		// disable fadeing in ie
		if (ieversion >= 6) 
			$('.hoverimg').attr("src","/images/kocka_clear.png");
			$('.hoverimg').hover(
				function() {
					$(this).attr("src","/images/"+kocka);
				},
				function() {
					$(this).attr("src","/images/kocka_clear.png");
				});
			if ($('body').hasClass("top10")){
				$('.hoverimg:first').hover(
				function() {
					$(this).attr("src","/images/top1_hover.png");
				},
				function() {
					$(this).attr("src","/images/kocka_clear.png");
				});
			}
		// apply fadeing in all other browsers
		} else {
			$('.hoverimg').readyState == 'complete';
			$('.hoverimg').animate({"opacity": "0"}, 0);
			$('.hoverimg').hover(
				function() {
					$(this).stop().animate({"opacity": "0.8"}, "medium");
				},
				function() {
					$(this).stop().animate({"opacity": "0"}, "medium");
				});
			$('.hoverimg').click(
				function() {
					$(this).stop().animate({"opacity": "0"}, "medium");
				});
		}
}
comingSoon = function () {
	$('<div class="holder"><img class="hamarosan" src="/images/hamarosan.png" width="153" height="153" /></div>').insertAfter(".cs");
}

// hack overflowing design elements
setOverflow = function () {
	//myBody = document.getElementById('fw');	
	myBody = document.getElementsByTagName('html')[0];
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
		}
	}
	if (winW < 960) {
		myBody.style.overflowX="auto";
	} else {
		myBody.style.overflowX="hidden";
	}
}

function showHideNL () {
	$('#newsletterForm').hide();
	$('#response').hide();
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) 
		var ieversion = new Number(RegExp.$1);
		if (ieversion >= 6) {
			$('#hirlevelbtn, #hirlevelKockaBtn').click (
			function() {
				clearForm();
				$("#newsletterForm").show();
				$("#home #content").css({'min-height' : '1000px'});
				return false;
			});
		} else {
			$('#hirlevelbtn, #hirlevelKockaBtn').click (
			function() {
				clearForm();
				$("#newsletterForm").toggleFade({ speedIn : "slow" });
				$("#home #content").css({'min-height' : '1000px'});
				return false;
			});
		}
	
}
function validateEmail() {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	$("#email").val($.trim($("#email").val()));
	if(filter.test($("#email").val())) {
		$("#email").removeClass("error");
		email = $("#email").val();
		return true;
	} else {
		$("#email").addClass("error");
		$('#response').text('Hibás e-mail cím.');
		$('#response').fadeIn();
		return false;
	}
}
function validateName(){
	$('#name').val($.trim($('#name').val()));
	if($('#name').val().length < 4) {
		$('#name').addClass("error");
		$('#response').text('A névnek minimum 4 karakter hosszúnak kell lennie.');
		$('#response').fadeIn();
		return false;
	} else {
		$('#name').removeClass("error");
		name = $("#name").val();
		return true;
	}
}
function subscribeAjax () {
	$("#formHirlevel").submit(function() {
		validateEmail();
		validateName();
		if(validateName() && validateEmail()) {
			$.ajax({
				type: "POST",
				url: "/hirlevel.php",
				data: 'name='+ name + '&email=' + email,
				beforeSend:	showLoading('#response'),
				success: function(html){
					$('#response').html(html);
					if ($('#response:hidden')) {
						$('#response').fadeIn();
					}
				}
			 });
			 return false;
		} else {
			return false;
		}
	});
}

function clearForm () {
	$('#newsletterForm').fadeOut(function() {
		$('form')[0].reset();
		$('#response').html('');
		$('#name, #email').removeClass("error");
		$("#home #content").css({'min-height' : '800px'});
  	});
}

function validateEmailLeiratkozas () {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	$("#emailnl").val($.trim($("#emailnl").val()));
	if(filter.test($("#emailnl").val())) {
		$("#emailnl").removeClass("error");
		email = $("#emailnl").val();
		return true;
	} else {
		$('#respnl').fadeIn();
		$("#emailnl").addClass("error");
		$('#respnl').html('<h3 style="color:darkred">HIBA: A megadott e-mail cím nem megfelelő.</h3>');
		return false;
	}
}
function unsubscribeAjax () {
	$('#respnl').hide();
	$('#respnl').html('');
	$("#formUns").submit(function() {
		validateEmailLeiratkozas();
		if(validateEmailLeiratkozas()) {
			$.ajax({
				type: "POST",
				url: "/hirlevel/leiratkozas.php",
				data: 'email='+ email,
				beforeSend:	showLoading('#respnl'),
				success: function(html){
					$('#respnl').html(html);
					if ($('#respnl:hidden')) {
						$('#respnl').fadeIn();
					}
				}
			 });
			 return false;
		} else {
			return false;
		}
	});
}

/* jQuery Plugin Toggle Fade v1.0
 * Requires jQuery 1.2.3 (Not tested with earlier versions).
 * Copyright (c) 2008 Gregorio Magini [gmagini at gmail dot com]
 * 
 *	@param: Object Array. Arguments need to be in object notation.
 *	Returns: jQuery.
 *	Options:
 *		speedIn: Sets the speed of the fadeIn effect. Default: "normal".
 *		speedOut: Sets the speed of the fadeOut effect. Default: same as speedIn.
 */

(function($) {
$.fn.toggleFade = function(settings) {
	settings = jQuery.extend( {
		speedIn: "normal",
		speedOut: settings.speedIn
	},settings);
	return this.each(function() {
		var isHidden = jQuery(this).is(":hidden");
		jQuery(this)[ jQuery(this).is(":hidden") ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
	});
};
})(jQuery);
