$().ready(function() {           
    
	VideoPlayerInit = function () {
		$(".flash_default").each(function(){
		  var el = $(this);
		  var id = "video_player_"+Math.round(Math.random()*10000);
		  el.after("<div class='videoPlayer'><div id='"+id+"'></div></div>");
		  swfobject.embedSWF(window.url+"public/flash/vplayer.swf", id, "500", "370", "9.0.0","expressInstall.swf", { vurl: $(this).attr("alt") }, {bgcolor: "000000"});
		  el.remove();
		});
	};			

	loading = function (status) {
        if ($.browser.msie) refreshAnim = 0;
    	   
		if (status) {
			$("#loader").css({ opacity: 1, display: "block" }).stop().animate({ opacity: 1 }, 200, "swing");
		  	if (refreshAnim) {
			  $("#mainContent").stop().animate({ opacity: 0 }, 200, "swing");
		  	}
		} else {
		 	$("#loader").stop().animate({ opacity: 0 }, 200, "swing", function () {
				$(this).css({ opacity: 0, display: "none" });
   		 	});
		  	if (refreshAnim) {
			  $("#mainContent").stop().animate({ opacity: 1 }, 200, "swing");
		  	} else {
		  		refreshAnim = 1;
		  	}
		}              	
	};
	
	

	StateManager = EXANIMO.managers.StateManager;
	refreshAnim = 1;
	refreshBasket = 0;


	setRemoteLinks = function () {
		$('a.remote').unbind().each(function (i) {
			$(this).click(function() {		     
				refreshBasket = 0;
				window.formpars = false;
				StateManager.setState($(this).attr('href').replace(window.url,''));
				if (typeof formSend == "function") formSend('true');
				return false;
			});
	    });
	    
        $("#menuItems > *").each(function() {             
            $(this).find("a").bind("click", function() {                 
                $("#menuItems > *").each(function() {
                	
                    var el = $(this).find("a");
                    el.removeClass(el.attr("id") + "a");
                });
                $(this).addClass($(this).attr("id") + "a");
            });        
        });

        
        $("#catMenuUlM > *").each(function() {             
            $(this).find("a").bind("click", function() {                      
                $("#catMenuUlM > *").each(function() {
                    var el = $(this).find("a");
                    el.removeClass(el.attr("id") + "a");
                });
                $(this).addClass($(this).attr("id") + "a");
            });        
        });
        
        
         $("#catMenuUlW > *").each(function() {             
            $(this).find("a").bind("click", function() {                      
                $("#catMenuUlW > *").each(function() {
                    var el = $(this).find("a");
                    el.removeClass(el.attr("id") + "a");
                });
                $(this).addClass($(this).attr("id") + "a");
            });        
        }); 
        
       /* $("#videoLink1").bind("click", function() {
            formSend('false');
        });
        $("#videoLink2").bind("click", function() {
            formSend('false');
        });*/

		$('form.remote').unbind().each(function (i) {
			$(this).submit(function() {
				if ($("#dayCheck").length > 0 && $("#dayCheck").val() == 0) {
					alert(window.daycheckError);
					return false;
				}

				if (($(this).find("input[name='file']").length <= 0) || ($(this).find("input[name='file']").length && !$(this).find("input[name='file']").val())) {

					if ($(this).hasClass("noRefreshAnim")) refreshAnim = 0;
					else refreshAnim = 1;

					if ($(this).hasClass("refreshBasket")) refreshBasket = 1;
					else refreshBasket = 0;

					window.formpars = $(this).prepend("<input type='hidden' name='ajax' value='true' />").serializeArray();
					$(this).children("input[name='ajax']").remove();
					StateManager.setState($(this).attr('action').replace(window.url,''));
					return false;
				}
			});
	    });

	};

	StateManager.onstatechange = function(e) {
	    if (e.id!="") {
	        var base = window.location.toString().split("#")[0];
	        var isStore = (window.location.toString().search("/s#") >= 0) ? true : false;
	    
    		if ((window.url != base) && !window.formpars && !isStore) {
    		
    		    if ((base.search("/termekek") >= 0) || (base.search("/megrendeles") >= 0) ||
                    (base.search("/megrendeles_veglegesitese") >= 0) || (base.search("/megrendelve") >= 0) ||
                    (base.search("/bankkartya") >= 0)) {
                    
                    //window.location = window.url + "s#" + e.id.split("?")[0];
                    window.location = window.url + "s#" + e.id;
                    
                } else {
                
                    //window.location = window.url + "#" + e.id.split("?")[0];
                    window.location = window.url + "#" + e.id;
                }
    		} else {
    			if (!window.lock) {
    			  loading(1);
    				window.lock = 1;

    				var pars = (window.formpars?window.formpars:{ ajax: 'true' });
    				window.formpars = false;

					var mTop = $("#mainContent").offset().top;
					if ($('html').scrollTop() > mTop) $('html, body').animate({ scrollTop: 0 }, 'slow');

    				el = (refreshBasket==0)?$("#mainContent"):$("#basket");

    				$.post(window.url+e.id, pars, function (data){
    					if (refreshBasket==1) {
							$("#basket").remove();
							$("#mainContent").prepend(data);
    					} else {
    						el.html(data);
    					}
    					
    					if (typeof pageTracker._trackPageview == "function") pageTracker._trackPageview("/" + e.id);
    					refreshBasket = 0;
    					setRemoteLinks();
    					window.lock = 0;
    					loading(0);    					
    					VideoPlayerInit();
    				});

    			}
    		}
	    }
	}

	initWbPage = function () {
		window.lock = 0;
		setRemoteLinks();
		StateManager.initialize();
		VideoPlayerInit();
	}

	checkIE6 = function () {
	    return ($.browser.msie && parseInt($.browser.version)<7);
	}

	checkIE = function () {
	    return ($.browser.msie);
	}	

	addTag = function (str) {
		var selection = "";
		var value = $("#commentText").val();
		var range = $("#commentText").getSelection();
		var before = value.substring(0, range.start);
		var after = value.substring(range.end, value.length);

		if ((range.text.length > 0) || (str != "img" && str != "url")) {
			if ((str == "url" || str == "img") && range.text.substr(0,7) != "http://" ) {
				range.text = "http://" + range.text;
			}
			var trailing = "";
			if (range.text[range.length-1] == " ") {
				range.text = range.text.substring(0, range.length-1);
				trailing = " ";
			}
    		var selection = "["+str+"]" + range.text + "[/"+str+"]";
			$("#commentText").val(before + selection + trailing + after);
		} else if (str == "url") {
			if (selection = prompt("Adja meg az urlt!","http://")) {
				$("#commentText").val(before + "[url]" + selection + "[/url] " + after);
			}
		} else if (str == "img") {
			if (selection = prompt("Adja meg az kép url-t!","http://")) {
				$("#commentText").val(before + "[img]" + selection + "[/img] " + after);
			}
		}
	};

	htmlToBBCode = function (s) {
		s = $.trim(s);

		function rep(re, str) {
			s = s.replace(re, str);
		};

		rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
		rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
		rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
		rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
		rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
		rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
		rep(/<font>(.*?)<\/font>/gi,"$1");
		rep(/<img.*?src=\"(.*?)\".*?>/gi,"[img]$1[/img]");
		rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
		rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");
		rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");
		rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");
		rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");
		rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");
		rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");
		rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");
		rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");
		rep(/<\/(strong|b)>/gi,"[/b]");
		rep(/<(strong|b)>/gi,"[b]");
		rep(/<\/(em|i)>/gi,"[/i]");
		rep(/<(em|i)>/gi,"[i]");
		rep(/<\/u>/gi,"[/u]");
		rep(/<u>/gi,"[u]");
		rep(/<br \/>/gi,"\n");
		rep(/<br\/>/gi,"\n");
		rep(/<br>/gi,"\n");
		rep(/<p>/gi,"");
		rep(/<\/p>/gi,"\n");
		rep(/&nbsp;/gi," ");
		rep(/&quot;/gi,"\"");
		rep(/&lt;/gi,"<");
		rep(/&gt;/gi,">");
		rep(/&amp;/gi,"&");
		rep(/&undefined;/gi,"'"); // quickfix
		return s;
	}


	// USERNAME AND PASSWORD FIELD TEXT HIDER
	$(".usernameInput").click(function() {
		if ($(this).val() == "Felhasználónév") {
			$(this).val("");
		}
	});

	$(".usernameInput").blur(function() {
		if ($.trim($(this).val()) == "") {
			$(this).val("Felhasználónév");
		}
	});

	$(".passwordInput").click(function() {
		if ($(this).val() == "jelszo") {
			$(this).val("");
		}
	});

	$(".passwordInput").blur(function() {
		if ($.trim($(this).val()) == "") {
			$(this).val("jelszo");
		}
	});

	// GALLERY INITIALIZATION
	initGallery = function () {
		var contentOffset = $("#mainContent").offset();
		window.contentLeft = contentOffset.left;
		window.contentTop = contentOffset.top;
		window.gLoading = 0;
		gallery_slideshow = 0;
		gallery_nextEl = new Object();
		gallery_slideTimer = new Object();

		$(".gallery_image img").css({ opacity: 1 });
		$(".gallery_image > div").css({ zIndex: 10, display: "block", opacity: 0 });
		$(".gallery_image > a").click(function() {
			return false;
		});

		$(".gallery_image > div").mouseover(function () {
				$(this).stop().animate({ opacity: 0.5 }, 100, "swing", function () {
					$(this).animate({opacity: 0 }, 200);
				});
		});


		$(".gallery_image").click(function () {
		    if (!window.gLoading) {
		      window.gLoading = 1;
		      if ($(this).children("div").length) $(this).children("div").before('<div id="galleryLoader">&nbsp;</div>');
		      else $(this).children("a").before('<div id="galleryLoader" style="left: 80px; top: 80px;">&nbsp;</div>');
			  galleryLoad($(this));
		    }
		});

		resizeImg = function (img, MaxWidth, MaxHeight)	{
            var widthRatio = img.width / MaxWidth;
            var heightRatio = img.height / MaxHeight;
            var ratio = Math.max(widthRatio, heightRatio);
            img.width = Math.floor(img.width / ratio);
           	img.height = Math.floor(img.height / ratio);
            return img;
		}

		galleryShow = function () {
			galleryLoader();
			$("#galleryTable").css({ opacity: 0, display: "block" }).stop().animate({ opacity: 1 });
		}

		galleryClose = function (el) {
			clearTimeout(gallery_slideTimer);
		    $("#galleryPrev").remove();
		    $("#galleryNext").remove();
		    $("#galleryLoader").remove();

		    if (!checkIE()) {
			    $("#galleryClose, #gallerySlideBtn, #gallerySlider").stop().animate({ opacity: 0 }, 50, function() {
	                $("#galleryClose, #gallerySlideBtn, #gallerySlider").remove();
			    });
		    } else {
		    	$("#galleryClose, #gallerySlideBtn, #gallerySlider").remove();
		    }

		    $("#galleryImageContainer").stop().animate({top: "+=100", opacity: 0 }, 250, "swing", function() {
		        $("#galleryImageContainer").remove();
		        if (el == "next" || el == "prev") {
		           galleryLoadPage(el);
		        } else if (el) {
        	       window.gLoading = 1;
                   $(el).children("div").before('<div id="galleryLoader">&nbsp;</div>');
        	       galleryLoad($(el));
		        } else {
		        	setTimeout('$("object").css({ visibility: "visible" })', 400);
		        	gallery_slideshow = 0;
		        	$("#fullPageOverlay").stop().animate({ opacity: 0 }, 200, "swing", function() {
		        		$("#fullPageOverlay").remove();
		        	});
		        }
		    });

		}

		galleryLoadPage = function (str) {
		    var el = (str=="next")?$("#paginationNext"):$("#paginationPrev");
		    var href = el.attr('href').replace(window.url,'');
			if (!window.lock) {
			  	loading(1);
				window.lock = 1;
				$("#mainContent").load(window.url+href+"?ajax=true", function (){
					setRemoteLinks();
					window.lock = 0;
					loading(0);					
					(str=="next")?galleryLoad($(".gallery_image:first")):galleryLoad($(".gallery_image:last"));
				});
			}
		}

		galleryNextSlide = function () {
			if (gallery_slideshow && gallery_nextEl.next(".gallery_image").length)
				galleryClose(gallery_nextEl.next(".gallery_image"));
		}

		galleryStartSlide = function (el, width) {
			gallery_slideshow = 1;
			jQuery.easing.def = "easeOutCubic";
			$("#gallerySlideBtn").addClass("gallerySlideStop");
			$("#gallerySlider").stop().animate({ width: (width-20)+"px" }, 5000);
			gallery_nextEl = el;
			gallery_slideTimer = setTimeout("galleryNextSlide();", 5000);
		}

		galleryStopSlide = function () {
			clearTimeout(gallery_slideTimer);
			gallery_slideshow = 0;
			jQuery.easing.def = "jswing";
			$("#gallerySlideBtn").removeClass("gallerySlideStop");
			$("#gallerySlider").stop().animate({ width: 0 });
		}

		galleryLoad = function (el) {
			$("object").css({ visibility: "hidden" });
			if (!$("#fullPageOverlay").length) {
				$("body").prepend("<div id='fullPageOverlay' style='position: absolute; left: 0; z-index: 10000; top: 0; width: 100%; height: "+$(document).height()+"px; opacity: 0; background-color: black; display: none;'>&nbsp;</div>");
				$("#fullPageOverlay").css({ opacity: 0, display: 'block' }).animate({ opacity: 0.5 }, 200);
				$("#fullPageOverlay").click(function(){galleryClose()});
			}

			offsets = el.offset();

            var left = offsets.left;
            var top = offsets.top;
            var image = window.url + el.children("a").attr("rel") + "_large/" + el.children("a").attr("title");
            var imageOri = window.url + el.children("a").attr("rel") + el.children("a").attr("title");
            var title = el.children("a").children("img").attr("alt");

			img = new Image();

		    img.onload=function (){
		      window.gLoading = 0;
		      $("#galleryLoader").remove();
		      var width = 0;
		      var height = 0;
		      width = img.width;
		      height = img.height;
			  img = resizeImg(img,150,150);

			  if (img.width < 150) {
			  	left += Math.floor((150-img.width)/2);
			  }

    		  if (img.height < 150) {
    		  	top += Math.floor((150-img.height)/2);
    		  }

		      $("body").prepend("<div id='galleryImageContainer' style='position: absolute; display: none; left: "+left+"px; top: "+top+"px;'><img id='galleryImage' style='width: "+img.width+"px; height: "+img.height+"px' src='"+image+"' alt='' /><div id='galleryDesc'>"+(title?title:"")+"<br /><a id='galleryFullImage' href='"+imageOri+"' target='_blank'></a></div></div>");

              $("#galleryImageContainer").css({ display: "block", opacity: 0 });

		      var targetLeft = Math.floor($(window).width()/2) - Math.floor(width/2);
		      var targetTop  = $(window).scrollTop() + Math.floor($(window).height()/2) - Math.floor(height/2);

		      jQuery.easing.def = "easeInOutQuart";

              $("#galleryImageContainer").css({left: targetLeft, top: targetTop-100});
              $("#galleryImage").css({ width: width, height: height });

		      $("#galleryImageContainer").animate({ opacity: 1, top: targetTop },350,"swing", function () {
		      	jQuery.easing.def = "easeOutQuad";

		        $("#galleryDesc").css({ width: width+"px", display: "block", opacity: 0 }).animate({ opacity: 1 });

		        $("body").prepend("<div id='galleryClose' style='left: "+(targetLeft-10)+"px; top: "+(targetTop-10)+"px'></div>");
		        $("body").prepend("<div id='gallerySlideBtn' style='left: "+(targetLeft+4)+"px; top: "+(targetTop-9)+"px'></div>");
		        $("body").prepend("<div id='gallerySlider' style='left: "+(targetLeft+31)+"px; top: "+(targetTop+3)+"px'></div>");
		        $("body").prepend("<div id='galleryPrev'  style='left: "+targetLeft+"px; top: "+targetTop+"px; width: "+Math.floor(width/2)+"px; height: "+height+"px;'>&nbsp;</div>");
		        $("body").prepend("<div id='galleryNext'  style='left: "+(targetLeft+Math.floor(width/2)+5)+"px; top: "+targetTop+"px; width: "+Math.floor(width/2)+"px; height: "+height+"px;'>&nbsp;</div>");

		        $("#galleryPrev, #galleryNext").css({ display: "block", opacity: 0 });

		        $("#gallerySlideBtn").hover(
		        	function(){
		        		$(this).stop().animate({ left: targetLeft + 13 + "px" });
		        	},
		        	function(){
		        		$(this).stop().animate({ left: targetLeft + 3 + "px" });
		        	}
		        );

		        $("#gallerySlideBtn").click(function(){
		        	if (gallery_slideshow) 	galleryStopSlide();
		        	else 					galleryStartSlide(el, width);
		        });

		        if (el.prev(".gallery_image").length || $("#paginationPrev").length ) {
		            $("#galleryPrev").css({ display: "block", opacity: 0, cursor: "pointer" });
    		        $("#galleryPrev").hover(
    		          function(){
    		              $(this).stop().animate({ opacity: 1 });
    		          },
    		          function(){
    		              $(this).stop().animate({ opacity: 0 });
    		          }
    		        );

    		        $("#galleryPrev").click(function(){
    		              if (el.prev(".gallery_image").length) {
    		                  galleryClose(el.prev(".gallery_image"));
    		              } else {
    		                  galleryClose("prev");
    		              }
    		        });
		        }

		        if (el.next(".gallery_image").length || $("#paginationNext").length) {
		            $("#galleryNext").css({ display: "block", opacity: 0, cursor: "pointer" });
    		        $("#galleryNext").hover(
    		          function(){
    		              $(this).stop().animate({ opacity: 1 });
    		          },
    		          function(){
    		              $(this).stop().animate({ opacity: 0 });
    		          }
    		        );

    		        $("#galleryNext").click(function(){
    		              if (el.next(".gallery_image").length) {
    		                  galleryClose(el.next(".gallery_image"));
    		              } else {
    		                  galleryClose("next");
    		              }
    		        });
		        }

				if (!checkIE()) $("#galleryClose, #gallerySlideBtn").css({ display: "block", opacity: 0 }).animate({ opacity: 1 });
				else $("#galleryClose, #gallerySlideBtn, #gallerySlider").css({ display: "block" });

		        $("#galleryClose").click(function(){
		          galleryClose();
		        });

		        if (gallery_slideshow) galleryStartSlide(el, width);

		      });
		    }
		    img.src = image;
		}
	}


	/* USE WITH JS: $("#cctvMenu").html(createMenu('<?=addcslashes(store::get_categories_to_ul(39),"'")?>')); */
	createMenu = function (categories) {
		categories = categories.split("</a>");
		var lastSize = 0;
		var nextSize = 0;
		var productList = "";
		for (var i=0; i<categories.length; i++) {
			var size = categories[i].replace(/(\<a.*)/gi,"").length;
			if (categories[i+1] != undefined) {
				var nextSize = categories[i+1].replace(/(\<a.*)/gi,"").length;
			} else {
				var nextSize = 0;
			}

			var str = categories[i].substr(size)+"</a>";
			if ((i != 0) && (i != categories.length-1)) {
				if (nextSize > size) {
					productList += "<li>"+str.replace("class='remote'", "class='remote termekMenuCat'")+"<ul>\n";
				} else if (size < lastSize) {
					for (var j=0; j<(lastSize-size); j++) {
						productList += "</ul></li>";
					}
					productList += "\n<li>"+str+"</li>\n";
				} else {
					productList += "<li>"+str+"</li>\n";
				}
			}
			var lastSize = size;
		}

		return productList;
	}


	initWbPage();
});


//--------------hors3-------------//

/*function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
	alert(movieName);
}
function formSend(music) {
	getFlashMovie('player').sendTextToFlash(music);
	//alert(teszt);
}*/

$().ready(function() {

	if ($("#headerflash").length)
	{
		getFlashMovie = function(movieName) {
			var isIE = navigator.appName.indexOf("Microsoft") != -1;
			return (isIE) ? window[movieName] : document[movieName];
		}
		formSend = function(music) {
			//alert(music);
		    if (typeof getFlashMovie("headerflash").sendTextToFlash != 'undefined')
				getFlashMovie("headerflash").sendTextToFlash(music);
		}
	}
});
 
 
