function isEmail (s) {
   return String(s).search (/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) != -1;
}

$(document).ready(function(){
	var gal=[];
	var galActive=0;
	$("#gallery-com-navigation a.gallery-term").each(function(i){
		var is=$(this).attr('rel').split(';');
		gal[i]={
			src:is[0]
			,href:is[1]
			,txt:is[2]
			,id:is[4]
		};
	});
	if(!(gal.length>0)){
		$("#gallery-com-image div.narrow-right").addClass("hide-button");
		$("#gallery-com-image div.narrow-left").addClass("hide-button");
	}
	$.fn.changeImage=function(i){
		i=parseInt(i);
		if(!(i<gal.length&&i>=0)){
			return;
		}
		//console.info(i);
		
		galActive=i;
		var alt=$("#gallery-com-image img").attr('alt');
		$("#gallery-com-image img").remove();
		var nImg=document.createElement('img');
		$(nImg).attr({'src': gal[i].src+'?'+Math.random(), 'alt':alt});
		$("#gallery-com-image").append(nImg);
		$(nImg).load(function(){
			var h = parseInt($(this).get(0).offsetHeight);
			$("#gallery-com-image div.narrow-right").css({'height':h});
			$("#gallery-com-image div.narrow-left").css({'height':h});
		});
		
		
		/*$("#gallery-com-image img").attr({
			'src':gal[i].src
		});
		$("#gallery-com-image img").load(function(){
			var h = parseInt($(this).get(0).offsetHeight);
			$("#gallery-com-image div.narrow-right").css({'height':h});
			$("#gallery-com-image div.narrow-left").css({'height':h});
			//alert(h);
			return;
		});*/
		$("#gallery-download-link a").attr({
			'href':gal[i].href
		});
		$("#image-describe").html(gal[i].txt);
		if(gal.length>1){
			if(i<=0){
				$("#gallery-com-image div.narrow-left").addClass("hide-button");
			} else {
				$("#gallery-com-image div.narrow-left").removeClass("hide-button");
			}
			if(i+1<gal.length){
				$("#gallery-com-image div.narrow-right").removeClass("hide-button");
			} else {
				$("#gallery-com-image div.narrow-right").addClass("hide-button");
			}
		}
	}
	
	
	for(var i=0; i<gal.length; i++){
		if(gal[i].id==$("#gallery-com-image img").attr('rel')){
			galActive=i;
			$(this).changeImage(i);
			break;
		}
	}
	$("#gallery-com-image div.narrow-left").click(function(){
			$(this).changeImage(parseInt(galActive)-1);
	});
	$("#gallery-com-image div.narrow-right").click(function(){
			$(this).changeImage(parseInt(galActive)+1);
	});
	$("#gallery-com-navigation a.gallery-term").click(function(){
		var i=$(this).attr('rel').split(';');
		galActive=i[3];
		$(this).changeImage(i[3]);
		return false;
	});
	
	$("#gallery-com-image div.narrow-right").hover(
	  function () {
		$(this).addClass("hover-right");
	  },
	  function () {
		$(this).removeClass("hover-right");
	  }
	);
	$("#gallery-com-image div.narrow-left").hover(
	  function () {
		$(this).addClass("hover-left");
	  },
	  function () {
		$(this).removeClass("hover-left");
	  }
	);

	
	
	
	$("#events-register-form span.ev-link").click(function(){
	   //console.info('clicked');
        var i=$(this).attr('id');
        $("#"+i+"-show").css('display', 'block');
        $("#events-register-form .event-reg[id!="+i+"-show]").remove();
        $("#events-register-form span.ev-link").remove();
		$("#"+i+"-show"+" form").submit(function(){
			var er=true;
			$("input.register_required", this).each(function(){
				var v = $(this).val();
				var f=$(this).attr('name');
				if(v==''){
					$(this).css({backgroundColor: "#dd3f00"});
					$(this).animate({ backgroundColor: "#fff" }, 900);
					er=false;
				}
				if(f=='reg-e-mail'&&!isEmail(v)&&er){
					$(this).css({backgroundColor: "#dd3f00"});
					$(this).animate({ backgroundColor: "#fff" }, 900);
					er=false;
				}
			});
			return er;
		});
        return false;
    });
    
    $("#events-register-form span.ev-link").each(function(){
		var txt=$(this).text();
		$(this).html('<img alt="'+txt+'" src="/design/dinamic_image/menu-2.php?s=8&amp;c=255,255,255&amp;l='+txt+'" />');
		
		//console.info(txt);
    });
    //console.info(otherRefering);
    //otherRefering=otherRefering==undefined ? false : true;
    if(false && (!$.cookie('banner-show')|otherRefering)){
       $("body").append(
        '<div id="banner-background" style="position: absolute; top: 0px; left: 0px; z-index:999998; width:'+$("body").get(0).offsetWidth+'px; height:'+$("#page_wrapper").get(0).offsetHeight+'px;"></div><div id="banner-shower" style="position: absolute; top: 100px; left: 300px; z-index:999999;"><a id="banner-link" href="/?spele"><img width="558" height="291" src="/design/images/zm-banner.png" /></a><a id="close-banner" style="display: block; background: url(\'/design/images/empty.gif\'); position: absolute; right: 0px; bottom: 0px; width: 38px; height: 38px;" href="/?close">&nbsp;</a></div>');
        $("#close-banner").click(function(){
            $.cookie('banner-show', 'closed', { path: '/' });
            $("#banner-shower").remove();
            $("#banner-background").remove();
            return false;
        });
        $("#banner-background").css('background-color', '#0db04b');
        $("#banner-background").css('opacity', 0.3);
        $("#banner-link").click(function(){
            $.cookie('banner-show', 'closed', { path: '/' });
            $("#banner-shower").remove();
            $("#banner-background").remove();
        });
    }
    
});
