var cpobj = new cpaint()
cpobj.set_transfer_mode('get')
cpobj.set_response_type('object')

var cp = new cpaint()
cp.set_transfer_mode('get')
cp.set_response_type('text')

var spinner = "indicator.gif"

function get_project(id) {
	header_data = '<div class="load"><img src="images/'+spinner+'" alt="" /> loading data...</div>'
	changeDivContent("title",header_data)
	
	description_data = '<div class="load"><img src="images/'+spinner+'" alt="" /> loading description...</div>'
	changeDivContent("description",description_data)
	
	tumbs_data = '<div class="load"><img src="images/'+spinner+'" alt="" /> loading images...</div>'
	changeDivContent("thumbs",tumbs_data)
	
	changeDivContent("links","")
	
	get_data(id)
	get_thumbs(id)
	
	showFooterData('...')
}

//// update data //////////////////////////////////////////////

function get_data(id) {
	cp.call('gateway.php', 'get_data', update_data, id)
}

function update_data(result) {
	
	var output = result.split("&")
	var project = new Array()
	var links = ""
	
	for (i=0; i<output.length; i++) {
		pair = output[i].split("=")
		project[pair[0]] = pair[1]
	}
	
	
	//// LINKS ////////////////////////////////////
	
	project.link = new Array();
	project.linkset = project.links.split(";");
	for (i=0; i<((project.linkset.length)-1); i++) {
		project.link[i] = project.linkset[i].split("%");
	}
	
	for (i=0; i<project.link.length; i++) {
	
		type = project.link[i][0];
		win = project.link[i][1];
		href = project.link[i][2];
		
		//alert('['+type+']\n['+win+']\n['+href+']');
		
		win = (win!=null) ? win : "scroll";
		w = (screen.availWidth/3)*2;//850;
		h = (screen.availHeight/3)*2;//650;
	
		if ((href) && (type) && (win != "null")) {
			//// create link string ////
			if (type=="video") {
				script = 'openVideo';
				url =  href.replace(/\@/g,"=").replace(/\|/g,"&");
			} else if (href.indexOf("http://")>-1) {
				script = 'openWin';
				url = href;
			} else {
				script = 'openWin';
				url = href;
			}
			
			/*
			thislink = '<a href="javascript:void(0);" onclick="'+script+'(\''+url+'\',\'popupwin\',\''+w+'\',\''+h+'\',\''+win+'\')">';
			thislink += '<img src="images/icon_'+type+'.gif" alt="open '+type+'" title="open '+type+'" />';
			thislink += '</a> ';
			*/
			
			thislink = '<a href="javascript:'
			thislink += script+'(\''+url+'\',\'popupwin\',\''
			thislink += w+'\',\''+h+'\',\''+win+'\')">'
			thislink += '&#187; check the '+type
			thislink += '</a>'
			//alert(thislink);
			
			links += thislink;
		}
	}
	
	showFooterData(project.title)
	
	header_data = "<h2><strong>"+project.title+"</strong> <small>("+project.section+")</small>";
	//header_data += links;
	header_data += "</h2>";
	header_data += "<h3>"+project.subtitle+" </h3>";
	header_data += "<h4>"+project.team+" / "+project.date+"</h4>";
	
	changeDivContent("title",header_data);
	//document.getElementById('title').innerHTML = header_data;
	
	description_data = ''
	description_data += '<p>'+project.brief
	if (project.description != undefined) description_data += " <a id=\"more_link_"+project.id+"\" class=\"plus\" href=\"javascript:showDescription('"+project.id+"');\">more...</a>"
	description_data += '</p>'
	if (project.description != undefined) {
		description_data += "<div id=\"more_text_"+project.id+"\" class=\"more_text\">"+project.description+"</div>";
	}
	
	description_data += "<p><strong>role: "+project.role+"</strong></p>";
	
	changeDivContent("description",description_data);
	
	if (links != '') {
		changeDivContent("links",'<p>related media:<br />'+links+'</p>');
	}
}


//// update thumbs //////////////////////////////////////////////

function get_thumbs(id) {
	cp.call('gateway.php', 'get_thumbs', update_thumbs, id);
}

var resizeThumbsInterval = undefined;
var imageList = [];

function update_thumbs(result) {
	
	changeDivContent("thumbs","");
	
	var n = 0
	var results = result.split("|")
	var id = results.pop()
	var output = ''
	
	for (i=1; i<results.length; i++) {
		idata = results[i].split(';')
		imageList[i] = idata;
		
		output = idata.toString(';')
		
		var my_image = idata[0]
		var my_image_w = idata[1]
		var my_image_h = idata[2]
		var my_image_alt = idata[3]
		var my_image_path = idata[4]
		
		output = "<div id=\"imgcont_"+id+"\" class=\"imgcont\">"
		output += "<a href=\"javascript:showImage('"+my_image+"','"+my_image_w+"','"+my_image_h+"',"+i+",'"+id+"');\">"
		output += "<img class=\"thumb\" src=\""+my_image_path+"\" alt=\""+my_image_alt+"\" />"
		output += "<div>"+my_image_alt+"</div>"
		output +=  "</a></div>"
		
		if (n==1) output += "<hr />"
		n = (n==1) ? 0 : 1
		
		document.getElementById('thumbs').innerHTML += output
		
		output = ''
	}
}

	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	function getPageSize()
	{
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
			
		// all but Explorer Mac
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			
		// Explorer Mac...would also work in Explorer 6 Strict,
		// Mozilla and Safari
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		// all except Explorer
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
			
		// Explorer 6 Strict Mode
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		
		// other Explorers
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height
		// less than height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width
		// less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}