// JavaScript Document
function center(elem) {
    elem.css("position","absolute");//outerWidth
	var center_top = (($(window).height() - elem.outerHeight()) / 2) + $(window).scrollTop();
	var center_left = (($(window).width() - elem.outerWidth()) / 2) + $(window).scrollLeft();
	if(center_top < 40)
		center_top = 40;
	if(center_left < 15)
		center_left = 15;
    elem.css("top", center_top + "px");
    elem.css("left", center_left + "px");
    return true;
}
function center_background()
{
	var centered_height = ( ( ($(window).height()-630) / 2) + $(window).scrollTop() );
	if(centered_height<0)
		centered_height = 0;
	var centered_width = ((($(window).width() - 900) / 2) + $(window).scrollLeft());
	if(centered_width<0)
		centered_width = 0;
	$('body').css("background-position",centered_width+"px "+centered_height+"px");
}
function getValFromURI(querystring,token) {
    var match = RegExp('[?&]' + token + '=([^&]*)').exec(querystring);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var anaf_language = "he";
var show_language = true;
var currenttitle = "פרגולות וסוככים";
var currentCollection = "עיצוב חוץ";
var currentIsGallery = true;
function setNewContent(gallery_name,image_num){
	if(currentIsGallery){
		$('#loading').show();
		$('#loading_bg').show();
	}
	$.get("phpgallery/files/galleryAjaxInline.php",{gallery:gallery_name,index:image_num,collection:currentCollection,language:anaf_language}, function(data){
		$('.content').empty();
		$('.content').html(data);
		// register the click event 
		$('.prv_image,.next_image').click(function(ev) {
			//alert($(this).attr('index'));
			setNewContent(currenttitle,$(this).attr('index'));
		});

		if(currentIsGallery){
			$('#loading').hide();
			$('#loading_bg').hide();
		}
		currentIsGallery = true;

	});
	return true;
}
function setContact()
{
	if(currentIsGallery){
		$('#loading').show();
		$('#loading_bg').show();
	}
	$.get("scripts/contact/contact.php",{language:anaf_language}, function(data){
		$('.content').empty();
		$('.content').html(data);
		$('.contact form').submit(function(ev){
			//alert("submit "+ev.target);
			if(validateForm(anaf_language)==true)
			{// send the email
				showErrors("");
				//alert("name: "+eval("document.contact.sender_name.value"));
				//alert("email: "+eval("document.contact.sender_email.value"));
				//alert("phone: "+eval("document.contact.sender_phone.value"));
				//alert("message: "+eval("document.contact.sender_message.value"));
				$.get("scripts/contact/send.php"
						,{name:eval("document.contact.sender_name.value")
						,email:eval("document.contact.sender_email.value")
						,phone:eval("document.contact.sender_phone.value")
						,message:eval("document.contact.sender_message.value")}, function(data){
							if(anaf_language=="en")
								showThanks("Message sent ok.");
							else
								showThanks("ההודעה נשלחה בהצלחה.");
				});
				
			}
			return false;
		});
		if(currentIsGallery){
			$('#loading').hide();
			$('#loading_bg').hide();
		}
		currentIsGallery = false;
	});
}

function setContentCollection(col_name)
{
	currentCollection = col_name;
	if(currentIsGallery){
		$('#loading').show();
		$('#loading_bg').show();
	}
	$.get("phpgallery/files/collections.php",{collection:currentCollection,language:anaf_language}, function(data){
		$('.content').empty();
		$('.content').html(data);
		
		$('.gallery_link').click(function(ev){
			currenttitle = $(this).attr('name');
			setNewContent($(this).attr('name'),0);
		});
		if(currentIsGallery){
			$('#loading').hide();
			$('#loading_bg').hide();
		}
		currentIsGallery = true;
	});	
	return true;
}
function SetHomeHandlers()
{
	/*
	$('li.home_item a').click(function(ev) {
		//alert($(this).text());
		currentCollection = $(this).attr('col');
		currenttitle = $(this).text();
		setNewContent($(this).text(),0);	
		return false;
	});*/
	$(".home_header").click(function(ev){
		setContentCollection($(this).attr('name'));
		return false;
	});
}
function SetHomepage()
{
	if(currentIsGallery){
		$('#loading').show();
		$('#loading_bg').show();
	}
	$.get("phpgallery/files/home.php",{language:anaf_language}, function(data){
		$('.content').empty();
		$('.content').html(data);
	
		SetHomeHandlers();
		currentIsGallery = true;
		if(currentIsGallery){
			$('#loading').hide();
			$('#loading_bg').hide();
		}
	});
}

function SetMenuHandlers()
{
	$("ul.submenu li a").click(function(ev) {
		//alert($(this).text());
		currentCollection = $(this).attr('col');
		currenttitle = $(this).text();
		setNewContent($(this).text(),0);	
		return false;
	});
	$("ul.nav > li > a").click(function(ev){
		//alert("home_header "+ $(this).text());
		if($(this).text()=="דף הבית" || $(this).text()=="Home"){
			currenttitle = $(this).text();
			SetHomepage();	
			//return false;
		}
		if($(this).text()=="פרופיל חברה" || $(this).text()=="Company Profile"){
			currenttitle = $(this).text();
			if(currentIsGallery){
				$('#loading').show();
				$('#loading_bg').show();
			}
			$.get("scripts/profile.php",{language:anaf_language}, function(data){
				$('.content').empty();
				$('.content').html(data);
			
				$('#goto_contact').click(function(){
					setContact();
					});
				if(currentIsGallery){
					$('#loading').hide();
					$('#loading_bg').hide();
				}
				currentIsGallery = false;
			});
		}
		if($(this).text()=="צור קשר" || $(this).text()=="Contact Us"){
			currenttitle = $(this).text();
			setContact();
			return false;
		}
		
		$(".clicked").removeClass("clicked");//remove from all
		$(this).addClass("clicked");//we are going to close all expanded except this one so mark this as clicked
		// close other expanded except fot this one that is 'clicked'
		$("ul.nav > li > a").each(function(index) {
			// expand menu item?
			if($(this).hasClass("expanded") && !$(this).hasClass("clicked") ){
				$(this).removeClass("expanded");
				$(this).removeClass("clicked");

				// change open/closed icon to closed
				$(this).parent().removeClass("opened_"+anaf_language);
				$(this).parent().addClass("closed_"+anaf_language);

				$(this).next().slideUp();
				//alert($(this).parent()[0].tagName);
			}
		});
		// toggle expand or hide
		if($(this).hasClass("expandable") && !$(this).hasClass("expanded") ){
			//show submenu
			$(this).addClass("expanded");
			$(this).next().slideDown();
			// change open/closed icon to open
			$(this).parent().removeClass("closed_"+anaf_language);
			$(this).parent().addClass("opened_"+anaf_language);
		}
		// open collection page
		if($(this).hasClass("expandable"))
			setContentCollection($(this).text());
		
		// remove flag
		//$(this).removeClass("clicked");
		return false;
	});
	// when clicking the inner item the sidebar click event will chatch it unless we clear the event by returning false
	$("ul.submenu > li > a").click(function(){
		$(".clicked").removeClass("clicked");//remove from all
		$(this).addClass("clicked");//only mark this one as clicked
		return false;
	});
}
$(document).ready(function() {
	// center main page horizontally and vertically
	center($(".container"));
	center($("#intro"));
	// center background image
	center_background();
	//$('body').css("background-position",((($(window).width() - 900) / 2) + $(window).scrollLeft())+"px "+( ( ($(window).height()-630) / 2) + $(window).scrollTop() )+"px");
	// update centered images when browser window is resized
	$(window).resize(function() {
		center($(".container"));
		center($("#intro"));
		center_background();
	});
	// default is hebrew change content if english clicked
	$('.intro_he,.intro_en').click(function(ev){
		if($(this).attr('class')=="intro_en")
			anaf_language="en";
		else
			anaf_language="he";
		currentIsGallery = false;
		SetHomepage();
		$.get("scripts/menu.php",{lang:anaf_language}, function(data){
			$('#menu_content').empty();
			$('#menu_content').html(data);
			SetMenuHandlers();
			if(anaf_language=="en")
				currentCollection="Outdoor Design";
			$('#intro').css('display','none');
		});
		return false;
	});
	$('#logo').click(function(){SetHomepage();});
}); 
$(document).oneTime(500,function(){
	// preload all thumbs first
	$.get("scripts/preloadthumbs.php",function(data){
		//$('#preloadlib').text("done");
		$('#preloadthumbs').html(data);
	});
});
$(document).oneTime(3000,function(){
	// preload all images 
	$.get("scripts/preloadimages.php",function(data){
		//$('#preloadlib').text("done");
		$('#preloadimages').html(data);
	});
});

