/*
 *
 * MOTO Design
 *
 * Client: 2011 MOTO Design Inc.
 *
 * Author: Da-Woon Chung
 * Author URL: http://shinycloud.com
 * Copyright: Copyright ⓒ 2011 ShinyCloud
 * 
 * Version: 0.3.05072011
 *
 */

/**************************************************/

/*
 *
 * MOTO Design data transfer script based on ELINN Light 0.2
 *
 */

/**************************************************/

/* load XML */
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","moto.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

var c_xml; // content from xml
var c_category; // content category
var parent_category; // parent category, for project and about-moto

var c_query = new Array; // content query

var checknavi;
var checkpop;
var checkother;

var n_loaded = false;
var n_current = 1; // all div and img id start from 1, not 0 // so when calling from array, you have to add 1 to each array number...

var loaded_img = 0;
var loaded_img_check = new Array;
var ci_loaded = false;

var intro_xml;
var current_intro = 0;
var loaded_intro = 0;
var loaded_intro_check = new Array;

var current_about = 0;

var team_xml;
var current_team = 0;

/* main functions begin here */
function getcontent(category, navi, pop, other, p_category) {
	c_category = category;
	parent_category = p_category;
	checknavi = navi;
	checkpop = pop;
	checkother = other;

	c_xml = xmlDoc.getElementsByTagName(category);

	for(var i=0; i<c_xml.length; i++) {
		c_query[i] = new Content(i);
	}	

	if(checkother)
		appendcontent(); // checkother can be removed and appendcontent/writecontent can be written into one function, if contents can be somehow put into xml.
	else
		writecontent();

	if(checknavi)
		setnavi();

	if(checkpop)
		setpop();

	firstload();
}

function writecontent() {
	for(var i=0; i<c_query.length; i++) {
		document.writeln("<img id=\"content-"+(i+1)+"\" class=\"content-img\" />");
		document.getElementById("content-"+(i+1)).src = c_query[i].image;
		document.getElementById("content-"+(i+1)).alt = c_query[i].title;
	}
}
(function($) {
	appendcontent = function() {
		for(var i=0; i<c_query.length; i++) {
			if(c_query[i].image != "-") {
				var imagecode = "<img id=\"c_img-"+(i+1)+"\" class=\"content-img\" />";
				$("div#content-"+(i+1)).append(imagecode);
				document.getElementById("c_img-"+(i+1)).src = c_query[i].image;
				document.getElementById("c_img-"+(i+1)).alt = c_query[i].title;
			}
			
		}
	}
})(jQuery);

(function($) {
	firstload = function() {
		for(var i=1; i<=c_query.length; i++) {
			$("#content-"+i).hide();
		}
		if(checknavi)
			$("#navi").hide();
		if(checkpop)
			$("#product-pop").hide();
		checkimage();
	}
})(jQuery);

function checkimage() {
	var img_que = document.getElementById("main-content").getElementsByTagName("img");

	if(!ci_loaded) {
		document.getElementById("l_progress").innerHTML = "loading images: <span id=\"loaded\">0</span> / " + img_que.length;
		ci_loaded = true;
	}

	for(var i=0; i<img_que.length; i++) {
		if(!loaded_img_check[i]) {
			if(img_que[i].complete) {
				loaded_img_check[i] = true;
				loaded_img++;
				document.getElementById("loaded").innerHTML = loaded_img;
			}
		}
	}
	if(loaded_img == img_que.length)
		loaded();
	else
		setTimeout("checkimage()", 100);
}
(function($) {
	loaded = function() {
		$("div.loader").hide();
		$("#content-"+n_current).fadeIn(300, function() {
			if(checknavi)
				$("#navi").show("slide", {direction: "down"}, 500, function() {
					if(checkpop)
//						$("#product-pop").fadeIn(1000);
						$("#product-pop").show("slide", {direction: "down"}, 700);
				});
		});
		
	}
})(jQuery);

function setcurrent(i) {
	n_current = i;
}

/* functions for navigation */
function setnavi() {
	if(!n_loaded) {
		if(parent_category)
			document.getElementById("navi").className += " " + parent_category;
		else
			document.getElementById("navi").className += " " + c_category;
		n_loaded = true;
	}

	if(n_current == 1)
		document.getElementById("navi").innerHTML = "<div id=\"next\"><a href=\"javascript:n_next()\">next &gt;</a></div>";
	else if(n_current == c_query.length)
		document.getElementById("navi").innerHTML = "<div id=\"prev\"><a href=\"javascript:n_prev()\">&lt; prev</a></div>";
	else
		document.getElementById("navi").innerHTML = "<div id=\"prev\"><a href=\"javascript:n_prev()\">&lt; prev</a></div>" + "\n" + "<div id=\"next\"><a href=\"javascript:n_next()\">next &gt;</a></div>";
}
(function($) {
	n_next = function() {
		if(n_current < c_query.length) {
			$("#content-"+n_current++).hide("slide", {direction: "left"}, 500);
			if(checkpop)
				setpop();
			$("#content-"+n_current).show("slide", {direction: "right"}, 500);

			if(n_current==2 || n_current==c_query.length) // a littlebit of math here...
				setnavi();
		}
	}
})(jQuery);
(function($) {
	n_prev = function() {
		if(n_current > 1) {
			$("#content-"+n_current--).hide("slide", {direction: "right"}, 500);
			if(checkpop)
				setpop();
			$("#content-"+n_current).show("slide", {direction: "left"}, 500);

			if(n_current==1 || n_current==c_query.length-1) // a littlebit of math here...
				setnavi();
		}
	}
})(jQuery);

/* function for product info pop box */
function setpop() {
	document.getElementById("title").innerHTML = c_query[n_current-1].title;
	document.getElementById("date").innerHTML =  c_query[n_current-1].date;
	document.getElementById("client").innerHTML = "client. " + c_query[n_current-1].client;
	document.getElementById("country").innerHTML = c_query[n_current-1].country;
}

/* functions for intro page */
(function($) { // these functions can be integrated with functions above, but it require many 'if' cases anyway, so it's better to seperate... I think...
	getintro = function() {
		intro_xml = xmlDoc.getElementsByTagName("intro");
		for(var i=0; i<intro_xml.length; i++) {
			var intro_src = intro_xml[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
			var intro_title = intro_xml[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
			var intro_description = intro_xml[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;

			document.writeln("<div id=\"intro-"+(i+1)+"\" class=\"intro-div\"></div>");

			$("div#intro-"+(i+1)).append("<img id=\"intro-img-"+(i+1)+"\" class=\"intro-img\">");

			document.getElementById("intro-img-"+(i+1)).src = intro_src;

			if(intro_title != "-") {
				$("div#intro-"+(i+1)).append("<h3 id=\"intro-title-"+(i+1)+"\" class=\"intro-title\"></h3>");
				document.getElementById("intro-title-"+(i+1)).innerHTML = intro_title;
			}

			if(intro_description != "-") {
				$("div#intro-"+(i+1)).append("<p id=\"intro-description-"+(i+1)+"\" class=\"intro-description\"></p>");
				document.getElementById("intro-description-"+(i+1)).innerHTML = intro_description;
			}
		}
		checkintro();
	}
})(jQuery);
function checkintro() {
	var intro_que = document.getElementById("intro-slide").getElementsByTagName("img");

	for(var i=0; i<intro_que.length; i++) {
		if(!loaded_intro_check[i]) {
			if(intro_que[i].complete) {
				loaded_intro_check[i] = true;
				loaded_intro++;
			}
		}
	}
	if(loaded_intro == intro_que.length)
		introloaded();
	else
		setTimeout("checkintro()", 100);
}
(function($) {
	introloaded = function() {
		for(var i=1; i<=intro_xml.length; i++) {
			$("div#intro-"+i).hide();
		}

		$("div#loader-intro").hide();
		introslide();
	}
})(jQuery);
(function($) {
	introslide = function() {
		if(current_intro == intro_xml.length) {
			$("div#intro-"+current_intro).fadeOut(300, function() {
				current_intro = 1;
				$("div#intro-"+current_intro).fadeIn(300);
			});
		}
		else if(current_intro == 0) {
			current_intro++;
			$("div#intro-"+current_intro).fadeIn(300);
		}
		else {
			$("div#intro-"+current_intro++).fadeOut(300, function() {
				$("div#intro-"+current_intro).fadeIn(300);
			});
		}
		setTimeout("introslide()", 5000);
	}
})(jQuery);

function loadlatest(a,b,c,d) { // looks messy and inefficient, but it's better than going through all the xml data everytime. Just remember the product number!
	var latest_xml = xmlDoc.getElementsByTagName("latest");

	for(i=1; i<=4; i++) {
		document.writeln("<div class=\"latest-item\" id=\"latest-"+i+"\">");
		document.writeln("<a id=\"latest-link-"+i+"\"><img class=\"latest-thumbnail\" id=\"latest-thumbnail-"+i+"\" /></a>");
		document.writeln("<div class=\"latest-box\">");
		document.writeln("<p class=\"latest-year\" id=\"latest-year-"+i+"\"></p>");
		document.writeln("<p class=\"latest-client\" id=\"latest-client-"+i+"\"></p>");
		document.writeln("<p class=\"latest-title\" id=\"latest-title-"+i+"\"></p>");
		document.writeln("</div>");
		document.writeln("</div>");
	}

	document.getElementById("latest-link-1").href = "/?c=project&s=latest&n="+a;
	document.getElementById("latest-link-2").href = "/?c=project&s=latest&n="+b;
	document.getElementById("latest-link-3").href = "/?c=project&s=latest&n="+c;
	document.getElementById("latest-link-4").href = "/?c=project&s=latest&n="+d;

	document.getElementById("latest-thumbnail-1").src = latest_xml[a-1].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
	document.getElementById("latest-thumbnail-2").src = latest_xml[b-1].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
	document.getElementById("latest-thumbnail-3").src = latest_xml[c-1].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
	document.getElementById("latest-thumbnail-4").src = latest_xml[d-1].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;

	document.getElementById("latest-year-1").innerHTML = latest_xml[a-1].getElementsByTagName("date")[0].childNodes[0].nodeValue;
	document.getElementById("latest-year-2").innerHTML = latest_xml[b-1].getElementsByTagName("date")[0].childNodes[0].nodeValue;
	document.getElementById("latest-year-3").innerHTML = latest_xml[c-1].getElementsByTagName("date")[0].childNodes[0].nodeValue;
	document.getElementById("latest-year-4").innerHTML = latest_xml[d-1].getElementsByTagName("date")[0].childNodes[0].nodeValue;

	document.getElementById("latest-client-1").innerHTML = latest_xml[a-1].getElementsByTagName("client")[0].childNodes[0].nodeValue;
	document.getElementById("latest-client-2").innerHTML = latest_xml[b-1].getElementsByTagName("client")[0].childNodes[0].nodeValue;
	document.getElementById("latest-client-3").innerHTML = latest_xml[c-1].getElementsByTagName("client")[0].childNodes[0].nodeValue;
	document.getElementById("latest-client-4").innerHTML = latest_xml[d-1].getElementsByTagName("client")[0].childNodes[0].nodeValue;

	document.getElementById("latest-title-1").innerHTML = latest_xml[a-1].getElementsByTagName("title")[0].childNodes[0].nodeValue;
	document.getElementById("latest-title-2").innerHTML = latest_xml[b-1].getElementsByTagName("title")[0].childNodes[0].nodeValue;
	document.getElementById("latest-title-3").innerHTML = latest_xml[c-1].getElementsByTagName("title")[0].childNodes[0].nodeValue;
	document.getElementById("latest-title-4").innerHTML = latest_xml[d-1].getElementsByTagName("title")[0].childNodes[0].nodeValue;
}

(function($) {
	feedload = function() {
		$("#feed-source").hide();
		$("#loader-feed").hide();
		$("#feed-source").fadeIn(300);
	}
})(jQuery);

/* about slide */
(function($) {
	aboutslide = function() {
		if (current_about == 0) {
			current_about++;
		}
		else if(current_about == c_xml.length) {
			$("img#content-"+current_about).fadeOut(300, function() {
				current_about = 1;
				$("img#content-"+current_about).fadeIn(300);
			});
		}
		else {
			$("img#content-"+current_about++).fadeOut(300, function() {
				$("img#content-"+current_about).fadeIn(300);
			});
		}
		setTimeout("aboutslide()", 3000);
	}
})(jQuery);

/* load client list */
function getclient() {
	var client_xml = xmlDoc.getElementsByTagName("iclient");
	for(i=0; i<client_xml.length; i++)
		document.writeln("<li>"+client_xml[i].childNodes[0].nodeValue+"</li>");
}

/* functions for team slide */
function getteam() {
	team_xml = xmlDoc.getElementsByTagName("team");
	for(i=0; i<team_xml.length; i++) {
		document.writeln("<img id=\"team-slide-"+(i+1)+"\" class=\"team-right\" />");
		document.getElementById("team-slide-"+(i+1)).src = team_xml[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
	}
}
(function($) {
	teamslide = function() {
		if (current_team == 0) {
			current_team++;
		}
		else if(current_team == team_xml.length) {
			$("img#team-slide-"+current_team).fadeOut(300, function() {
				current_team = 1;
				$("img#team-slide-"+current_team).fadeIn(300);
			});
		}
		else {
			$("img#team-slide-"+current_team++).fadeOut(300, function() {
				$("img#team-slide-"+current_team).fadeIn(300);
			});
		}
		setTimeout("teamslide()", 3000);
	}
})(jQuery);

/* Content class */
function Content(i) {
	switch(parent_category) {
		case "project":
			this.image = c_xml[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
			this.title = c_xml[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
			this.date = c_xml[i].getElementsByTagName("date")[0].childNodes[0].nodeValue;
			this.client = c_xml[i].getElementsByTagName("client")[0].childNodes[0].nodeValue;
			this.country = c_xml[i].getElementsByTagName("country")[0].childNodes[0].nodeValue;
			break;
		default:
			this.image = c_xml[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
			break;
	}
}

