var openedMenu = '';
var openedWork = '';
var openedPage = '';
var bodyHeight = 0;
var documentHeight = jQuery(window).height();

function closeIntro() {
	jQuery.post("/Resources/Php/close-intro.php","",{},"json");
	jQuery("#page-intro").css("display","none");
	jQuery("#page-container").fadeIn(600);
}

function showSecondaryMenu(selectedMenu, resetPage) {
	if(resetPage) {
		jQuery("#page-content-right").animate({width : "0px", height : "0px"},400,function() {
			jQuery(this).css("display","none");
		});
		
		jQuery("#page-content-left").slideUp(400);
	}
		
	if(openedMenu != selectedMenu) {
		if(openedMenu != '')
			jQuery("#" + openedMenu).slideUp(400,function() {	
				jQuery("#" + selectedMenu).slideDown(400, function() {
					openedMenu = selectedMenu;									   
				});											
			});
		else
			jQuery("#" + selectedMenu).slideDown(400, function() {
				openedMenu = selectedMenu;									   
			});	
	} else
		jQuery("#" + selectedMenu).slideUp(400, function() {
			openedMenu = '';									   
		});
}

function changePage(variables) {
	
	jQuery("#loading").css("display","block");
	
	var loadedPage = variables['Subpage'] != undefined ? '/Resources/UI/' + variables['Page'] + '/' + variables['Subpage'] + '.php' : '/Resources/UI/' + variables['Page'] + '.php';
	
	jQuery.post(loadedPage, variables,
		function(data) {
			if(data.content != undefined) {
				jQuery("#page-content-right").animate({width : "0px", height : "0px"},400,function() {
					jQuery(this).css("display","none");
					if(openedWork != '')
						openedWork = '';
				});
				jQuery("#page-content-left").slideUp(400,function() {
					jQuery(this).html(data.content).slideDown(400, function() {
						jQuery("#loading").css("display","none");
						
						bodyHeight = jQuery("body").height();
						if(bodyHeight < documentHeight)
							bodyHeight = documentHeight;
						
						jQuery(".work > a").each(function() {
							jQuery(this).click(function() {
								jQuery(".work > a").removeClass("selected");
								jQuery(this).addClass("selected");
							});
						});
					});												  
				});
				
				if(variables['Page'] == 'contact') {
					if(openedMenu != '') {
						showSecondaryMenu(openedMenu, false);
						openedMenu = '';
					}
					jQuery("#page-content-right").html("<h1>map localization</h1><div style=\"height:450px; width:450px; border:solid 3px #CCCCCC;\" id=\"google-map\"></div>");
					
					getMap("google-map");
					jQuery("#page-content-right").animate({width : "460px", height : "460px"}, 400);
				}
			}
		},"json"
	);
}

function showWork(variables) {
	jQuery(this.window).scrollTo(0, 800);
	
	if(openedWork != variables['WorkID'])
		jQuery("#loading").css("display","block");
	
	jQuery.post('/Resources/UI/works.php', variables,
		function(data) {
			if(data.content != undefined) {
				if(openedWork != '' && openedWork != variables['WorkID']) {
					jQuery("#work-details-" + openedWork).slideUp(400);
				}
				if(openedWork != variables['WorkID']) {
					openedWork = variables['WorkID'];
					
					if(jQuery("#work-details-" + variables['WorkID']).css("display") == "none")
						jQuery("#work-details-" + variables['WorkID']).slideDown(400);
					
					jQuery("#page-content-right").animate({height : "0px",width : "0px"},400,function() {
						jQuery(this).html(data.content).css("height","100%").animate({width : "460px"},400, function() {
							
							jQuery(".work-right").slideDown(400, function() {
																		  
								jQuery("#loading").css("display","none");
							
								jQuery(".lightbox").fancybox({
									'zoomSpeedIn'		:	500,
									'zoomSpeedOut'		:	500,
									'transitionIn'      :   'elastic',
									'transitionOut'     :   'elastic',
									'centerOnScroll'    :   true
								});
							});
						});													 
					});
				}
			}
		},"json"
	);
}

function submitContact() {
	jQuery("#loading").css("display","block");
	
	if(jQuery("#name").val() == '' || jQuery("#subject").val() == '' || jQuery("#email").val() == '' || jQuery("#message").val() == '') {
		jQuery("#messages").html("<font color=\"#FF0000\">Ati omis completarea unuia sau mai multor campuri ! Toate campurile sunt obligatorii !</font>").slideDown(400);
		jQuery("#loading").css("display","none");
	} else {
		jQuery.post("/Resources/Php/contact.php", {Name : jQuery("#name").val(), Subject : jQuery("#subject").val(),Email : jQuery("#email").val(),Message : jQuery("#message").val()},
			function(data) {
				if(data.content != undefined) {
					jQuery("#messages").slideUp(400, function() {
						jQuery(this).html(data.content)										  
					}).slideDown(400);
					if(data.sent == 1) {
						jQuery("#name").attr("value","");
						jQuery("#subject").attr("value","");
						jQuery("#email").attr("value","");
						jQuery("#message").attr("value","");
					}
				}
				
				jQuery("#loading").css("display","none");
			},"json"
		);
	}
	
}

function getMap(id) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById(id));
		map.setCenter(new GLatLng(44.194561,28.643298), 13);
		map.setZoom(16);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		map.enableScrollWheelZoom()

		var websight = new GLatLng(44.194561,28.643298);
		var websightmarker =new GMarker(websight); 
		map.addOverlay(websightmarker);
		
		map.openInfoWindowHtml(websight,"<div style=\"height:88px; width:200px; display:block; background:url(/Resources/Images/gwebsight.gif) no-repeat center; padding-top:20px;\"><b>Webisght Media</b><br />Strada Ion Ratiu, Nr. 109, Constanta</div>");

	}
}


jQuery(document).ready(function() {
	var wh = jQuery(window).height();

	var marginTop = parseInt((wh - 510) / 2);
	
	jQuery("#intro").css("margin-top",marginTop + 'px');
});
