$(document).ready(function(){
 // videoTab();
  restoreLastAdded();
  checkBut();
  atachBut();
  checkgallery();
  linkboxatach();

});

var array_loader = new  Array();

var IE;
var tempX = 0;
var tempY = 0;
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = getMouseXY;

function restoreLastAdded(){

	$('#lastadded').css("background","none").fadeIn(600).html($("#loaderlastadded").html());
}
function linkboxatach(){
	$(".linkbox").mouseover(function(){
		//var tooltip = $("#tooltipla");
		var data = $(this);
		var tooltip = $("#tooltipla");

	//	console.log(data.attr("href"));
		$("#tooltipla a").attr("href",data.attr("href")).attr("title",data.attr("title"));
		$("#tooltipla a#link1la").html(data.attr("title"));
		//$("#tooltipla a#link1la img").attr("src",data.children("img")[0].attr("src"));
		$("#tooltipla #pretla").html(data.attr("rel")+"<br/>");
		//.attr("href",data.attr("href"));
		var mainchild = data.children(".linkboxcontent");
		var child = mainchild.children(".imagineamea");
		$("#tooltipla a#link2la img").attr("src",child.attr("src"));
		$("#tooltipla").css("display","block");
	})
		.mouseout(function(){
		$("#tooltipla").css("display","none");

	})
	.click(function(){
		location.href=$(this).attr("href");
	})
}
function checkgallery(){

	$(".selcat").click(function(){
		//alert("buzz");
		var id = $(this).attr("id");
		var myid = id.substr(6, id.length);
		if (array_loader[myid][0] == 0)
		{
			restore_imgsrc(myid,0);
		}
		$(".selcat").removeClass("activ");
		//$(".blackbox").css({display:"none"});
		//$("#blackbox"+ id.substr(6, id.length)).css({"display":"block"});
		$(".viss").animate({ top: (-id.substr(6, id.length)*160) }, "slow" );
		$(this).addClass("activ");
		return false;

	});

}
function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}

	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}

	if ($("#tooltipla").css("display") != "none")
	{
		$("#tooltipla").css("left",(tempX+8)+"px");
		$("#tooltipla").css("top",(tempY+8)+"px");
	}
	//document.getElementById("gamesxxx-"+CustomGamesId).style.left  = (tempX+20)+"px";
	//document.getElementById("gamesxxx-"+CustomGamesId).style.top = (tempY+20)+"px";
	return true;
}

function checkBut(){

	$('.screenimg').each(
		function(el){
			var id = $(this).attr("id");
			var myid = id.substr(9, id.length);
			//alert("#galleryimageshow"+myid + " .newpage");
			var number = $("#galleryimageshow"+myid + " .newpage").size();
			//alert(number);
			if (number > 1)
			{
				$("#imgarrows"+myid).css({display:"block"});
				$("#arrr"+myid).attr({rel:"1"});
				$("#arrr"+myid).css({display:"block"});

			}


		});


}

function atachBut(){

	$(".next").each(function(){
		var attr = $(this).attr("rel");
		var id = $(this).attr("id");
		var myid = id.substr(4, id.length);
	//	console.log(myid);
		var number = $("#galleryimageshow"+myid + " .newpage").size();
		array_loader[myid] = new Array();

		for ( var i=0;i<number ; i++)
		{
			if (myid == 0)
			{
				if (i==0)
				{
					array_loader[myid][i] = 1;
				}else
				{
					array_loader[myid][i] = 0;
				}
			}
			else{
				array_loader[myid][i] = 0;
			}

		}
	})
	.click(function(){
		var attr = $(this).attr("rel");
		var id = $(this).attr("id");
		var myid = id.substr(4, id.length);
	//	console.log(myid);
		var number = $("#galleryimageshow"+myid + " .newpage").size();
		var childnr = parseInt(attr) + 1 ;
		if (array_loader[myid][attr] == 0)
		{
			restore_imgsrc(myid,attr);
		}

		if (number > parseInt(attr))
		{
			if (number > (parseInt(attr)+1))
				$(this).attr({rel:(parseInt(attr)+1)});
			else
				$(this).css({display:"none"});
			$("#arrl"+myid).attr({rel:attr});
			$("#arrl"+myid).css({display:"block"});
		}
		else
		{
			$(this).css({display:"none"});
		}
		$("#galleryimageshow"+myid).animate({ left: ((-attr)*400) }, "slow" );
		return false;
	});

	$(".back").click(function(){
		var attr = $(this).attr("rel");
		var id = $(this).attr("id");
		var myid = id.substr(4, id.length);
		//alert(myid);
		//var number = $("#galleryimageshow"+myid + " .newpage").size();
		//console.log(myid);
		attr = parseInt(attr)-1;
		if (attr > 0)
		{
			$("#arrr"+myid).attr({rel:attr+1});
			$("#arrr"+myid).css({display:"block"});
			$("#arrl"+myid).attr({rel:attr});
		}
		else
		{
			//alert("ajung aici");
			$("#arrr"+myid).attr({rel:""+(attr+1)});
			$("#arrr"+myid).css({display:"block"});
			$(this).css({display:"none"});
		}
		$("#galleryimageshow"+myid).animate({ left: ((-attr)*400) }, "slow" );
		return false;
	});


}

function restore_imgsrc( myid, attr) {
	//var childnr = parseInt(attr) + 1 ;
	//console.log("aici");
	//var $ax = document.getElementById("galleryimageshow"+myid).childNodes[attr];

	$("#galleryimageshow"+myid+" .newpage:eq("+attr+") .linkboxcontent").each(function(index){

		$(this).html("<img class='imagineamea' src='"+$(this).attr("rel")+"'/><span class='spnr'>"+(parseInt((parseInt(attr)*10) + parseInt(index) + 1 ))+"</span>");

		/*var img = new Image();
	 $(this).load(function () {
		 //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$(this).parent().append(this);
		$(this).fadeIn();
	 }).error(function () {
	 // notify the user that the image could not be loaded
	 }).attr('src', 'http://farm3.static.flickr.com/2405/2238919394_4c9b5aa921_o.jpg');
	 */

	});


	array_loader[myid][attr] = 1;
	//console.log($ax.children("img"));
	//ax.style.display = "block";
}



