var selector = "img[class=slide]";
var move = false;
var curr = count = 0;
var timer;

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){
	var yScroll, xScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

function defPosition(event) {
      var x = y = 0;
      if (document.attachEvent != null) { 
            x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
      } else if (!document.attachEvent && document.addEventListener) { // Gecko
            x = event.clientX + window.scrollX;
            y = event.clientY + window.scrollY;
      } else {
            // Do nothing
      }
      return {x:x, y:y};
}

function closeBox()
{
	$(".darkness, .whiteness").animate({"opacity":0},300,"linear",function(){$(".darkness, .whiteness").css({"display":"none"});});
}

function mousemove(e)
{
	var pos = defPosition(e);
	var scroll = getPageScroll();
	if (move) {
		$(".whiteness").css({"top":(scroll[1]+pos.y-31)+"px"});
		$(".whiteness").css({"left":(scroll[0]+pos.x-parseInt($(".whiteness").css("width"))/2-13)+"px"});
	};
}

function defCurrent(obj)
{
	var i = 0;
	$(selector).each(function(){
		i++;
		if ($(this).attr("rel")==$(obj).attr("rel"))
			curr = i;
	});
	count = i;
}

function toRight()
{
	var i = 0;
	var changed = false;
	$(selector).each(function(){
		i++;
		if (i==(curr+1) && !changed) {
			curr++;
			changed = true;
			$(".loading").attr("src",$(this).attr("rel"));
			if (i==count) {
				clearInterval(timer);
				$("#play-button").attr("src","img/play.jpg");
			};
		};
	});
}

$(document).ready(function(){
	$(selector).click(function(){
		defCurrent(this);
		var size = getPageSize();
		var scroll = getPageScroll();
		$(".loading").attr("src",$(this).attr("rel"));
		$(".darkness").css({"display":"block","width":size[0]+"px","height":size[1]+"px","opacity":0.6});
		$(".whiteness").css({"display":"block","opacity":0});
		$(".whiteness").css("left",scroll[0]+(size[2]-parseInt($(".whiteness").css("width")))/2);
		$(".whiteness").css("top",scroll[1]+(size[3]-parseInt($(".whiteness").css("height")))/2);
		$(".whiteness").animate({"opacity":1},700);
	});
	$(".whiteness").mouseenter(function(){
		$(".guide").css({"display":"block","opacity":0});
		$(".guide").animate({"opacity":0.8},300);
	});
	$(".whiteness").mouseleave(function(){
		$(".guide").css("display","none");
	});
	$(".darkness, #close").click(function(){
		closeBox();
	});
	$("#drag-button").mousedown(function(){
		move = true;
	});
	$(".darkness, .whiteness, .loading, .guide, #drag-button").mouseup(function(){
		move = false;
	});	
	$("#pboom").mousedown(function(){
		toRight();
	});
	$("#mboom").mousedown(function(){
		var i = 0;
		$(selector).each(function(){
			i++;
			if (i==(curr-1)) {
				curr--;
				$(".loading").attr("src",$(this).attr("rel"));
			};
		});
	});
	$("#play-button").mousedown(function(){
		if ($("#play-button").attr("src")=="img/play.jpg") {
			timer = setInterval("toRight();",3000);
			$("#play-button").attr("src","img/stop.jpg");
		} else {
			clearInterval(timer);
			$("#play-button").attr("src","img/play.jpg");
		}
	});
	$("#resize-button").mousedown(function(){
		var img = new Image();
		img.src = $(".loading").attr("src");
		var size = getPageSize();
		var scroll = getPageScroll();
		$(".whiteness").css({"width":img.width+10+"px","height":img.height+10+"px"});
		$(".loading").css({"width":img.width+"px","height":img.height+"px"});
		$(".whiteness").css("left",scroll[0]+(size[2]-parseInt($(".whiteness").css("width")))/2);
		$(".whiteness").css("top",scroll[1]+(size[3]-parseInt($(".whiteness").css("height")))/2);
	});
});
