var active_photo = null;
var gal_visible = false;

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 
window.onload = externalLinks;

function resize(id, H){
	if(document.getElementById(id)) {
		document.getElementById(id).style.height=H+'px';
	}
	if(document.getElementById('main'))
		document.getElementById('main').style.bottom=H+'px';
}

function getItem(id) {
	var itm = false;
	if(document.getElementById)
	itm = document.getElementById(id);
	else if(document.all)
	itm = document.all[id];
	else if(document.layers)
	itm = document.layers[id];
	
	return itm;
}

function toggleItem(id) {
	itm = getItem(id);
	
	if(!itm)
	return false;
	
	if(itm.style.display == 'none')
	itm.style.display = '';
	else
	itm.style.display = 'none';
	
	return false;
}

function popup(file,w,h) {
	
	param = "width="+w+", height="+h+", scrollbars=no";
	window.open(file, "gmap", param);
	
}

function getActiveWorkIndex(){
	
	for(i=0; i<works_id.length; i++){
		if(works_id[i]==work_prev_id) return i;
	}
						 
};

function nextWork(t){
	
	len = works_id.length;

	if(len == (getActiveWorkIndex()+1) ) i = 0;
	else i = getActiveWorkIndex()+1;
	
	id = works_id[i];
	openWork(id,t);
	work_prev_id = id;
						 
};

function prevWork(t){
	
	len = works_id.length;

	if(getActiveWorkIndex() == 0 ) i = len-1;
	else  i = getActiveWorkIndex()-1;
	
	id = works_id[i];
	openWork(id,t);
	work_prev_id = id;
						 
};

function echeck(str) {
	var at="@", dot=".", lat=str.indexOf(at), lstr=str.length, ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) {
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}

function validateForm() {
	$("#vardas").css("border-top", "1px solid #B4B4B4");
	$("#vardas").css("border-left", "1px solid #B4B4B4");		
	$("#vardas").css("border-bottom", "1px solid #D9D9D9");
	$("#vardas").css("border-right", "1px solid #D9D9D9");		
	
	$("#elpastas").css("border-top", "1px solid #B4B4B4");
	$("#elpastas").css("border-left", "1px solid #B4B4B4");		
	$("#elpastas").css("border-bottom", "1px solid #D9D9D9");
	$("#elpastas").css("border-right", "1px solid #D9D9D9");		
	
	var vardas 	= $("#vardas").val();
	var email 	= $("#elpastas").val();
	var errors 	= 0;

	if (email == null || email == '')	{
		$("#elpastas").css("border", "1px solid red");
		$("#elpastas").focus();			
		return false;
	}

	if (!echeck(email)) {
		$("#elpastas").css("border", "1px solid red");
		$("#elpastas").focus();			
		return false;
	}
	
	if (vardas == null || vardas == '')	{
		$("#vardas").css("border", "1px solid red");
		$("#vardas").focus();
		return false;
	}
	
	$("#reg_form").hide();
	$("#ad_links").hide();	
}		
	
function isNumeric(form_value) {
	if (form_value.match(/^\d+$/) == null)
		return false;
	else
		return true;
}
	
/* EDIT */

function toggleGallery(gal_id) {
		 if ($("#gallery-"+gal_id).attr("title") == "active") {
			 gal_visible = false;
			 closeImage(gal_id);
			 $("#gallery-"+gal_id).removeAttr("title");
			 $("#gallery-"+gal_id).slideUp("fast");
		 } else {
			 gal_visible = true;
			 $("#gallery-"+gal_id).attr("title", "active");
			 $("#gallery-"+gal_id).slideDown("fast");
			 $.get(BASE_HREF + 'ajax/getPG.php?id='+gal_id, function(data) { 
																				$("#photos-"+gal_id).html(data);
																				var n = Math.ceil(($("#photos-"+gal_id+" > a").length) / 6);
																				if (gal_visible) {
																					$("#gallery-"+gal_id).animate( { height: ((n * 64) + 5) + "px" }, 600 );
																					$("#preloader-cont-"+gal_id).css("display", "none");
																					$("#photos-"+gal_id).fadeIn("fast");
																				}
																		   } );
		 }
}

function openImage(id, gal_id) {
		 if ($("#big-image-container-"+gal_id).attr("title") == "active") {
			$.get(BASE_HREF+'ajax/getImage.php?id='+id, function(data){ 
																 		$("#tempCont").html(data);
																	    $("#bigimage-"+gal_id).html(data);
																	  });
		 } else {
		 	$("#big-image-container-"+gal_id).fadeIn();
			$("#big-image-container-"+gal_id).attr("title", "active");
			$.get(BASE_HREF+'ajax/getImage.php?id='+id, function(data){ 
																 		$("#tempCont").html(data);
																		$("#bigimage-"+gal_id).html(data);
																   	  });
		 }
		 active_photo = id;
};

function closeImage(gal_id){
		 $("#big-image-container-"+gal_id).fadeOut();
		 $("#big-image-container-"+gal_id).removeAttr("title");
}

function nextImage(gal_id) {
		 var id = $(".gal-id-"+gal_id).filter("[title="+active_photo+"]").next().attr("title");
		 if ($(".gal-id-"+gal_id).filter("[title="+active_photo+"]").next().length == 0) {
			 id = $(".gal-id-"+gal_id).attr("title");
		 } 
		 openImage(id, gal_id);
};

function prevImage(gal_id) {
		 var id = $(".gal-id-"+gal_id).filter("[title="+active_photo+"]").prev().attr("title");
		 if ($(".gal-id-"+gal_id).filter("[title="+active_photo+"]").prev().length == 0) {
			 id = $(".gal-id-"+gal_id).filter(":last").attr("title");
		 } 
		 openImage(id, gal_id);
};