// JavaScript Document

var cHttp;

function initContentAjax(){
	cHttp = getCHttpRequest();
}

function getCHttpRequest(){
	try{
		req = new XMLHttpRequest();
	} catch(err1){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(err2){
			try{
				req = new ActiveXObject("Microsoft.XMLHttp");
			} catch(err3){
				req = false;
			}
		}
	}
	return req;
}

/****generic methods****/
function loadingContent(){
	var dim = getBrowserSize();
	document.getElementById("content").innerHTML = "";
	var dimLeft = (dim[0]-30)/2;
	var dimTop = (dim[1]-30)/2;
	$('#preloader').css('left', dimLeft)
		.css('top', dimTop)
		.fadeIn(100);
}

function fillContent(e){
	closePortfolioOverlay();
	document.getElementById("content").innerHTML = e;
	$('#content').stop().css('left','-750px').animate({'left' : '0px'}, 200);
	$('#preloader').fadeOut(250);
	goToTop();
	$(".sayHi").text(sayHi());
}

/**** content request****/
function sendContentRequest(page){
	var pageDetail = page.split("/");
	var section = pageDetail[0];
	var id = pageDetail[1];
	
	var randomNum = Math.round(Math.random()*1000000000);
	var myurl = 'service/getContent.php?section=' + section + '&id=' + id + '&rand=' + randomNum;
	cHttp.open("GET", myurl, true);
	
	switch(section){
		case "resume":
			cHttp.onreadystatechange = receiveResumeHandler;
			break;
		case "work":
			if(id == undefined || id == ''){
				window.location = '#/home';
			}
			else{
				cHttp.onreadystatechange = receiveStaticPageHandler;
			}
			break;
		default:
			cHttp.onreadystatechange = receiveStaticPageHandler;
			break;
	}
	
	cHttp.send(null);
}

function receiveStaticPageHandler(){
	if(cHttp.readyState == 4){
		if(cHttp.status == 200){
			fillContent(cHttp.responseText);
			enableArrows();
			enableImageZoom();
			enableControls();
		}
		else{
			alert("Sorry, we have encountered an error while loading this page, please refresh and try again.");
		}
	}
	else{
		loadingContent();
	}
}

function receiveResumeHandler(){
	receiveStaticPageHandler();
	enablepdfDlButton();
}

/**** home page preview project ****/
function sendPreviewRequest(prevId){
	var randomNum = Math.round(Math.random()*1000000000);
	var myurl = 'service/getPreview.php?prevId=' + prevId + '&rand=' + randomNum;
	cHttp.open("GET", myurl, true);
	cHttp.onreadystatechange = receivePreviewReply;
	cHttp.send(null);
}

function receivePreviewReply(){
	if(cHttp.readyState == 4){
		if(cHttp.status == 200){
			parsePreviewReply(cHttp.responseText);
		}
		else{
			//alert("An error has occurred while loading this page.");
		}
	}
	else{
		loadingContent();
	}
}

function parsePreviewReply(reply){
  var hello = sayHi();
	var introText = "<h2>" + hello + "!</h2><h1>Welcome to my home in the cloud, <br/>here is what I&rsquo;ve been up to:</h1>";
	var buttonCode = "<div id='what_else' alt='See what else I am working on...' title='See what else I am working on...'>" + 
						"<img src='assets/images/ui/what_else_button/up.gif' id='what_else_up' alt='What else am I working on?'/>" + 
						"<img src='assets/images/ui/what_else_button/over.gif' id='what_else_over' alt='What else am I working on?'/>" + 
					"</div>";
	var project = "";
					
	//previous request did not succeed, AVG js bug
	if(reply.length == 0){
		introText = "<h2>" + hello + "!</h2><h1>Welcome to my home in the cloud, <br/>this is a design portfolio of <span class='highlight'>Paul Yuan</span></h1>";
		buttonCode = ""
	}
	else{
		var replyArray = reply.split('|');
		currentPreviewProject = replyArray[0];
		var title = "<h1>" + replyArray[1] + "</h1>";
		var link = replyArray[2];
		var image = "<img class='preview' src='" + replyArray[4] + "' alt='" + replyArray[5] + "' title='" + replyArray[5] + "'/>";
		var description = replyArray[3];
		
		project = "<div class='project'>" + 
							title + 
							link + 
							image + 
							description +
						"</div>";
	}
	var newContent = introText + project + buttonCode + "<div class='bottomPad'></div>";
	fillContent(newContent);
	enableWhatElseButton();
}

function enableWhatElseButton(){
	$('#what_else').hover(function(){
		$(this).children('#what_else_over').stop().animate({'top' : '0px', 'opacity' : 1}, 150);
		$(this).children('#what_else_up').stop().animate({'top' : '23px'}, 75);
		}, function(){
			$(this).children('#what_else_over').stop().animate({"top" : '23px', 'opacity' : 1}, 75);
			$(this).children('#what_else_up').stop().animate({'top' : '0px'}, 150);
	});
	$('#what_else').click(function(){
		sendPreviewRequest(currentPreviewProject);
		return false;
	});
}

function enablepdfDlButton(){
	$('#pdf_dl').hover(function(){
		$(this).children('#pdf_dl_over').stop().animate({'top' : '0px', 'opacity' : 1}, 150);
		$(this).children('#pdf_dl_up').stop().animate({'top' : '51px'}, 75);
		}, function(){
			$(this).children('#pdf_dl_over').stop().animate({"top" : '51px', 'opacity' : 1}, 75);
			$(this).children('#pdf_dl_up').stop().animate({'top' : '0px'}, 150);
	});
	
//	$('#pdf_dl').click(function(){
//		window.open('http://paul.maxvisualstudios.com/classic/assets/pdf/resume.pdf');
//        return false;				
//	});
}

function enableArrows(){
	var timeLong = 0; //originally 350
	var timeShort = 0; //originally 250
	
	$('.arrowUp').hover(function(){
		$(this).children('.arrowUp_over').stop().animate({'top' : '0px'}, timeLong);
		$(this).children('.arrowUp_up').stop().animate({'top' : '100px'}, timeShort);
		}, function(){
			$(this).children('.arrowUp_over').stop().animate({"top" : '100px'}, timeShort);
			$(this).children('.arrowUp_up').stop().animate({'top' : '0px'}, timeLong);
	});
	
	$('.arrowUp').click(function(){
		$(this).children('.arrowUp_over').stop().animate({"top" : '100px'}, timeShort);
		$(this).children('.arrowUp_up').stop().animate({'top' : '0px'}, timeLong);		
		return false;
	});
	
	$('.arrowDown').hover(function(){
		$(this).children('.arrowDown_over').stop().animate({'top' : '0px'}, timeLong);
		$(this).children('.arrowDown_up').stop().animate({'top' : '-100px'}, timeShort);
		}, function(){
			$(this).children('.arrowDown_over').stop().animate({"top" : '-100px'}, timeShort);
			$(this).children('.arrowDown_up').stop().animate({'top' : '0px'}, timeLong);
	});
	
	$('.arrowDown').click(function(){
		$(this).children('.arrowDown_over').stop().animate({"top" : '-100px'}, timeShort);
		$(this).children('.arrowDown_up').stop().animate({'top' : '0px'}, timeLong);			
		return false;
	});
	
	$('.projDesc').hover(function(){
		$(this).children('.projDesc_over').stop().animate({'top' : '0px'}, timeLong);
		$(this).children('.projDesc_up').stop().animate({'top' : '-100px'}, timeShort);
		}, function(){
			$(this).children('.projDesc_over').stop().animate({"top" : '-100px'}, timeShort);
			$(this).children('.projDesc_up').stop().animate({'top' : '0px'}, timeLong);
	});
	
	$('.projDesc').click(function(){
		showProjectDescription();
		$(this).children('.projDesc_over').stop().animate({"top" : '-100px'}, timeShort);
		$(this).children('.projDesc_up').stop().animate({'top' : '0px'}, timeLong);			
		return false;
	});
	
	$('#top_down_arrow').click(function(){
		goTo('target_first');		
		return false;
	});
}

function enableSmallX()
{
	$('.smallX').hover(function(){
		$(this).children('.x_over').stop().animate({'top' : '0px'}, 0);
		$(this).children('.x_up').stop().animate({'top' : '-100px'}, 0);
		}, function(){
			$(this).children('.x_over').stop().animate({"top" : '-100px'}, 0);
			$(this).children('.x_up').stop().animate({'top' : '0px'}, 0);
	});
	
	$('.smallX').click(function(){
		showProjectDescription();
		$(this).children('.x_over').stop().animate({"top" : '-100px'}, 0);
		$(this).children('.x_up').stop().animate({'top' : '0px'}, 0);			
		return false;
	});
}

function enableControls()
{
	if($('#work .work_expand').length){
		toggleDescription(2);
		$('#work .work_expand').click(function(){
			toggleDescription(2);
		});
	}
	
	enableSmallX();
	
	if($(".q_informatics").length){
		$(".q_informatics").mouseover(function(){
			$(this).attr("src", "../assets/images/icons/q2.png");
		}).mouseout(function(){
			$(this).attr("src", "../assets/images/icons/q1.png");
		}).click(function(e){
			showInformatics(e);
		});
	}
}

/*
 * scroll to top and show project description
 */
function showProjectDescription()
{
	goToTop();
	toggleDescription(1);
}

/*
 * 0-close, 1-open, 2-toggle
 */
function toggleDescription(mode)
{
	switch(mode){
		
		case 0:
			if($('#work .work_description').css('display') != 'none'){
				$('#work .work_description').slideUp(250, function(){
					$('#work .work_expand').text("Show Project Description").css('color', '#b8fcff');
					$('#work .work_expand').css("background", "url('assets/images/ui/mini_buttons/down2.png') no-repeat");
					$(this).hide();
				});
			}
			break;
	
		case 1:
			if($('#work .work_description').css('display') == 'none'){
				$('#work .work_description').slideDown(250);
				$('#work .work_expand').text("Hide Project Description").css('color', '#cccccc');
				$('#work .work_expand').css("background", "url('assets/images/ui/mini_buttons/up1.png') no-repeat");
			}
			break;
		
		case 2:
			if($('#work .work_description').length){
				if($('#work .work_description').css('display') == 'none'){
					$('#work .work_description').slideDown(250);
					$('#work .work_expand').text("Hide Project Description").css('color', '#cccccc');
					$('#work .work_expand').css("background", "url('assets/images/ui/mini_buttons/up1.png') no-repeat");
				}
				else{
					$('#work .work_description').slideUp(250, function(){
						$('#work .work_expand').text("Show Project Description").css('color', '#b8fcff');
						$('#work .work_expand').css("background", "url('assets/images/ui/mini_buttons/down2.png') no-repeat");
						$(this).hide();
					});
				}
			}
			break;
			
	}
}

function enableImageZoom()
{
//	$('.work_asset').click(function(){
//		console.log($(this).html());
//	});
}

function sayHi()
{
  var response = "";
  var collection = ["مرحبا (Arabic)", "你好 (Chinese)", "Ahoj (Croatian)", "Salut (French)", 
    "Hallo (German)", "γεια (Greek)", "Hola (Spanish)", "היי (Hebrew)", "हाय (Hindi)", 
    "Hai (Indonesian)", "こんにちは (Japanese)", "Ciao (Italian)", "안녕하세요 (Korean)", 
    "Hej (Swedish)", "สวัสดี (Thai)", "Merhaba (Turkish)", "привет (Russian)"];
  var random = Math.floor(Math.random() * collection.length);
  response = collection[random];
  return response;
}



