var limit = 18;
var timing = 400;





function showGallery(){
$("a.pic").each(function(i){
	 if(i < limit){
		$(this).children("img").fadeIn();
		$(this).animate(
		{
			top : (Math.floor((i+1) / 3) * 190),
			left : ((i+1) % 3 * 260)
		},
		timing,
		"linear"
		);	
	} else{
		$(this).children("img").css("display","block");
		$(this).css("left",((i+1) % 3 * 260)+"px").css("top",(Math.floor((i+1) / 3) * 190)+"px");
	}	 
$(".galDiv").css("height",(Math.floor($("a.pic").length / 3) * 195) + 80 + "px");
});


$('a.pic').fancyzoom(); 

}
function hideGallery(){};

function showGalleries(){
	$(".galDiv").remove();
	$("#galsDiv").show(300);
};
function hideGalleries(){
	$("#galsDiv").hide();
};


function initGalleries(){
	
	 $("a.gal").each(function(i){
		 if(i < limit){
			$(this).children("img").fadeIn();
			$(this).animate(
			{
				top : (Math.floor(i / 3) * 190),
				left : (i % 3 * 260)
			},
			timing,
			"linear"
			);	
		} else{
			$(this).children("img").css("display","block");
			$(this).css("left",(i % 3 * 260)+"px").css("top",(Math.floor(i / 3) * 190)+"px");
		}	 
	 });
	 if( !$("#noJS")[0] ){
		 $("a.gal").click(function(){
		 	$.get(
		 		$(this).attr("href"),
		 		function(data){
		 			$("#galsDiv").after(data);
		 			hideGalleries();
		 			showGallery();
		 		}
		 	)
		 	return false;
		 })
	}
}

var panning = false;
var maxSpeed = 15;
var delta = -1;
var panThr;
var pwidth = -1;
var pw2 = -1;

var isOpened = false;

function pan(){
	if(panning){
		var l = parseInt($("#pano").css("backgroundPosition").split(" ")[0]);
		$("#pano").css("backgroundPosition", (l + delta) +"px 0px");
	}
}

function setupPan(){
	pwidth = parseInt($("#pano").width());
	pw2 = Math.round(pwidth/2);
}

$(document).ready(function(){
	
	$("#blbl").click(function(){
	   if( !$(this).hasClass("q") ){
	       $(this).val("").addClass("q");
	       }
	   })
	
	$("#subs").click(function(){

           $.getJSON(
                "http://latomas.hu/new/hirj.php?jsoncallback=?",
                {"s":1,"habakukk":$("#blbl").val()},
        		function(data){
        				if(data.status == true){
	               $("#blbl").val("");
	               $("#hirlevel").hide();
	               alert("Köszönjük jelentkezésed!");        				    
        				    }
        		  });
	   })

	$("#unsubs").click(function(){
           $.getJSON(
                "http://latomas.hu/new/hirj.php?jsoncallback=?",
                {"u":1,"habakukk":$("#blbl").val()},
        		function(data){
        				if(data.status == true){
	               $("#blbl").val("");
	               $("#hirlevel").hide();
	               alert("Sikeresen lejelentkezél a hírlevelünkről!");        				    
        				    }
        		  });        
        
	   })

	
	$("#hlt").click(function(){
		$("#hirlevel").toggle();
		return false;
	})
	
	$.fn.fancyzoom.defaultsOptions.imgDir='new/ressources/';
	
	if( $(".gal")[0] ){
		initGalleries();
	}
	if( $("#noJS")[0] ){
		showGallery();
	}

		/* msg */
        if( $("#msgform")[0] ){
            $("#msgform").submit(function(){$("#msgform").hide();$("#thankyou").show();return true});
            $("#msgsend").click(function(){
                $("#msgform").hide();$("#thankyou").show();
                return true;
                });            
            }

	
	/* pano */
	if( $("#pano")[0] ){
		setupPan();
		panThr = setInterval(pan,60);
		
		$("#pano").mouseover(function(e){
			panning = true;
			delta = Math.floor(maxSpeed* (pw2 - e.layerX)/pw2 );
		});
		
		$("#pano").mousemove(function(e){
			delta = Math.floor(maxSpeed* (pw2 - e.layerX)/pw2 );
		})
		
		$("#pano").mouseout(function(){
			panning = false;
		})
		
		
		//opens
		var dm = new Date();
		
		var dd = dm.getDay()-1;
		if(dd == -1) dd = 6;
		if(window.shopName){
			var oos = opens[ window.shopName ];
			var os = opens[ window.shopName ][dd];
		} else {
			var oos = opens[(window.location+"").split("/")[4].replace(/\d/g,"")];	
			var os = opens[(window.location+"").split("/")[4].replace(/\d/g,"")][dd];	
		}
		/*
		for(var i = 0; i < oos.length; i++){
			if(oos[i] == false){
				var c = "zárva"
			} else{
				var c = oos[i][0] + "-" + oos[i][1];	
			}
			$( ".d" + (dd+1) ).children(".hours").html(c);
		}
		*/

		if(os === false) {isOpened = false;} else {
    		os[0] = os[0].split(":");
    		os[1] = os[1].split(":");
    		
    		os[0] = parseInt(os[0][0]*60) + parseInt(os[0][1]);
    		os[1] = parseInt(os[1][0]*60) + parseInt(os[1][1]);
    				
    		
    		var m = (60* dm.getHours()) + dm.getMinutes();
    		if(m > os[0] && m < os[1]) isOpened = true;
    		
    	}	
    		$( ".d" + (dd+1) ).css("background","black");
    		if(isOpened){
    		$( ".d" + (dd+1) ).append("<div id='open' />")
    		}
		

        
        

	}
})

var hirekText = "";


$(window).load(function(){
    if( $("#tumblr-hirek")[0] ){
        var h = tumblr_api_read["posts"], n = h.length;
        var ht = [];
        for(var i = 0; i < n; i++){
            var t = "";
            if(ht.length < 5){
                if(h[i]["type"] == "photo"){
                    t = $(h[i]["photo-caption"]).text();
                    if(t.length > 0)
                    ht.push(t + "<br />");
                    }
                if(h[i]["type"] == "link"){
                    t = $(h[i]["link-text"]).text();
                    if(t.length > 0)                    
                    ht.push(t + "<br />");
                    }                
                if(h[i]["type"] == "regular"){
                    t = $(h[i]["regular-title"]).text();
                    if(t.length > 0)                    
                    ht.push(t + "<br />");
                    }                
                }
            }
        hirekText = ht.join("\n");
        $("#ht-tu").html(hirekText);
                
        }
        
        
    })

