var scrol; // api
var imgs,divs, imgimg; // elements
var scroltimer;
var coworker;

var isiPad = navigator.userAgent.match(/iPad/i) != null;

var supportsOrientationChange = "onorientationchange" in window
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
$(window).bind(orientationEvent, function() {
	initBackground();
	if(scrol) {
		scrol.seekTo(scrol.getIndex(), 0);
	}
});
$(window).load(function() {
	initBackground();
	imgs.css("visibility", "visible");
	if(scrol) {
		scrol.seekTo(scrol.getIndex(), 0);
	}
});
$(function() {
	imgs = $("#images");
	divs = $("#images .items div");
	imgimg = $("#images .items div img");
	coworker = $("ul.coworker li a");
	
	//initBackground();
	if(divs.length > 1) {
		imgs.scrollable({circular:true, onSeek:checkColor, speed:600});
		scrol = imgs.data("scrollable");
		
		if(coworker.length > 0) {
			coworker.click(function() {
				$("ul.coworker li").removeClass("active");
				$(this).parent("li").addClass("active");
				ii = $(this).parent("li").index();
				scrol.seekTo(ii);
				return false;
			});
		}
		
		$("#prevslide").click(function() {
			scrol.prev();
		});
		$("#nextslide").click(function() {
			scrol.next();
		});
		
		$("#prevslide").show();
		$("#nextslide").show();
	} else {
		if(divs.eq(0).attr("color")) {
			color = divs.eq(0).attr("color");
			$(".mainmenu li.active a").animate({backgroundColor:color}, 500);
		}
		if(divs.eq(0).attr("title")) {
			t = divs.eq(0).attr("title");
			$(".title h1").text(t);	
		}
		
		$("#prevslide").hide();
		$("#nextslide").hide();
	}
});
function nextImage() {
	scrol.next();
}
function checkColor() {
	
	if(coworker.length > 0) {
	} else {
		clearTimeout(scroltimer);
		scroltimer = setTimeout("nextImage()", 800000000);
	}
	
	if(scrol.getItems().eq(scrol.getIndex()).attr("title")) {
		t = scrol.getItems().eq(scrol.getIndex()).attr("title");
	} else {
		t = $(".title h1").attr("base");
	}
	$(".title h1").text(t);	
	
	color = scrol.getItems().eq(scrol.getIndex()).attr("color");
	if(color) {
	} else {
		color = "#7DA9DA";
	}
	$(".mainmenu li.active a").animate({backgroundColor:color}, 500);
	//alert(scrol.getIndex());
}
function initBackground() {
	// reset 4 circular
	divs = $("#images .items div");
	imgimg = $("#images .items div img");
	
	ww = $(window).width();
	wh = $(window).height();
	
	//console.log('window width:' + ww);
	//console.log('window height:' + wh);
	
	iw = 1600;
	ih = 1200;
	
	imgs.css("width", ww);
	imgs.css("height", wh);
	
	divs.css("width", ww);
	divs.css("height", wh);
	
	imgimg.css("width", ww);
	imgimg.css("height", wh);
	
	tmpl = 0;
	tmpt = 0;
	
	
	if((ww/wh) > (iw/ih)) {
		tmpw = ww;
		tmph = tmpw*ih/iw;
		// top = 0
		// bottom = 0 - (tmph - wh);
		// center = 0 - ((tmph - wh) / 2);
		tmpt = 0;
	} else {
		tmph = wh;
		tmpw = tmph* (iw/ih);
		tmpl = 0 - ((tmpw - ww) / 2);
	}
	
	imgimg.css("width", tmpw);
	imgimg.css("height", tmph);
	imgimg.css("left", tmpl);
	imgimg.css("top", tmpt);
}
