var x=$(document);
x.ready(inicializarEventosJQuery);

var jsonProyectosTeleySis = null;
var jsonProyectosWebyDis = null;

function blockScreen(){
	$.blockUI({message:'<br /><img src="layoutpics/busy.gif" /><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:16px;font-weight:bolder;margin-left:10px;">Cargando contenidos...</span><br /><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:10px;">Si el proceso demora demasiado presione F5.</span><br /><br />'}); 
}

function unblockScreen(){
	$.unblockUI();
}

function inicializarEventosJQuery(){
	
	var sendData1 = "tipotrabajo=webydis";

	$.ajax({
  		 type: "POST",
		 dataType:"json",
  		 url: "utils/php/processLoadPortfolio.php",
  		 data: sendData1,
  		 success: dataResponse, 
		 error: errorSendingData
		 });
	
	var sendData2 = "tipotrabajo=teleysis";

	$.ajax({
  		 type: "POST",
		 dataType:"json",
  		 url: "utils/php/processLoadPortfolio.php",
  		 data: sendData2,
  		 success: dataResponse, 
		 error: errorSendingData
		 });

}


function errorSendingData (XMLHttpRequest, textStatus, errorThrown) {
}

function dataResponse (data, textStatus){
	
	if(data!=null && !isNaN(data.resultados) && data.resultados!=null){
		if(data.tipotrabajo=='teleysis'){
			jsonProyectosTeleySis = data;
		}
		if(data.tipotrabajo=='webydis'){
			jsonProyectosWebyDis = data;
		}	
		cargarProyectos(data.tipotrabajo);
	}
	else{
		window.location.href = "index.html";
		$("#portfolioWebYDesign .portfolioGalleryAreaContent").css('visibility','hidden');
 		$("#portfolioTeleYSistemas .portfolioGalleryAreaContent").css('visibility','hidden');
	}
}

function cargarProyectos(tipotrabajo){

	if(tipotrabajo=='webydis'){
			var baseElement = $("#portfolioWebYDesign .portfolioGalleryAreaContent .portfolioElementBox").clone();

			for(var i=0;i<jsonProyectosWebyDis.resultados;i++){
				var currentElement = $(baseElement).clone();
				var myid =  "webydis"+jsonProyectosWebyDis.id[i];
				$(currentElement).attr("id",myid);
				$(currentElement).appendTo("#portfolioWebYDesign .portfolioGalleryAreaContent");
				
				$("#"+myid+" .portfolioElementBoxInfo").html(jsonProyectosWebyDis.trabajo[i]);
				$("#"+myid+" .portfolioElementBoxThumbnail img").attr("src","showPhoto.php?thumb=1&id=" + jsonProyectosWebyDis.id[i]);
				$("#"+myid+" .portfolioElementBoxArea").attr("id", "webydisElem"+jsonProyectosWebyDis.id[i]);

				if(!($.browser.name=="msie")){
					
					$("#"+myid+" .portfolioElementBoxArea").hover (
						function(){
							if($(".portfolioElementBoxShowDetails",this).css("display")=="none")									 
									$(".portfolioElementBoxShowDetails",this).slideDown("slow");																 					},
						function(){
							if($(".portfolioElementBoxShowDetails",this).css("display")=="block")									 
								$(".portfolioElementBoxShowDetails",this).slideUp("normal");																 					}
					);
					
				}

				$("#"+myid+" .portfolioElementBoxArea a").attr("href",	"detalledetrabajo.html?id="+jsonProyectosWebyDis.id[i]);

				$(currentElement).css('display','block');
				
				if(i%2==0)
				$("#portfolioWebYDesign .portfolioGalleryAreaContent").append('<div class="portfolioElementBoxSpacer"></div>');
				
			}
	}
	
	if(tipotrabajo=='teleysis'){
			var baseElement = $("#portfolioTeleYSistemas .portfolioGalleryAreaContent .portfolioElementBox").clone();
			$(baseElement).removeAttr("id");
			for(var i=0;i<jsonProyectosTeleySis.resultados;i++){
				var currentElement = $(baseElement).clone();
				$(currentElement).removeAttr("id");
				$(".portfolioElementBoxInfo",currentElement).removeAttr("id");
				$(".portfolioElementBoxThumbnail",currentElement).removeAttr("id");
				$(".portfolioElementBoxArea",currentElement).removeAttr("id");
				$(".portfolioElementBoxShowDetails",currentElement).removeAttr("id");
				
				$(".portfolioElementBoxInfo",currentElement).html(jsonProyectosTeleySis.trabajo[i]);
				$(".portfolioElementBoxThumbnail img",currentElement).attr("src","showPhoto.php?thumb=1&id=" + jsonProyectosTeleySis.id[i]);
				$(currentElement).attr("id", "teleysis"+jsonProyectosTeleySis.id[i]);
				$(".portfolioElementBoxArea", currentElement).attr("id", "teleysisElem"+jsonProyectosTeleySis.id[i]);
				
				if(!($.browser.name=="msie")){
					$(".portfolioElementBoxArea",currentElement).hover (
						function(){
							if($(".portfolioElementBoxShowDetails",this).css("display")=="none")									 
									$(".portfolioElementBoxShowDetails",this).slideDown("slow");																 					},
						function(){
						if($(".portfolioElementBoxShowDetails",this).css("display")=="block")									 
							$(".portfolioElementBoxShowDetails",this).slideUp("normal");																 					}
					);
				}
				$(".portfolioElementBoxArea a",currentElement).attr("href", "detalledetrabajo.html?id="+jsonProyectosTeleySis.id[i]);
				
				
				
								
				$(currentElement).css('display','block');

				$(currentElement).appendTo("#portfolioTeleYSistemas .portfolioGalleryAreaContent");
		
			}
	}
}