var PictureList = new Array();

PictureList['index'] = new Array();
PictureList['index']['picture'] = '/ajax/index_picture.html';
PictureList['index']['frames'] = 3;
PictureList['index']['start'] = 2;

PictureList['homes'] = new Array();
PictureList['homes']['picture'] = '/ajax/homes_picture.html';
PictureList['homes']['frames'] = 6;
PictureList['homes']['start'] = 1;


$(document).ready(function(){
	
	if(!$("#imagebox")){return false;}
	
	var Page = $("#imagebox").attr('page');
	if(!Page){return false;}
	
	var BodyWidth = $(window).width();
	var BodyHeight = $(".content").height();
	
	var lTop = 370;
	var lLeft = BodyWidth / 2 - 25;
	
	
	$("#loading").css({
		'position':'absolute', 
		'z-index':'99', 
		'top': lTop + 'px', 
		'left': lLeft + 'px', 
		'width':'50px', 
		'height':'50px', 
		'background':'url(\'/images/loading3.gif\') no-repeat left top'
	}).show();
	
	
	$("#picture img").bindImageLoad(function(){
	    $("#loading").hide();
		CreateAninatePicture();	
	});	
	
	
	
});

$(window).resize(function(){
	CreateAninatePicture();
});


function CreateAninatePicture(){
	var BodyWidth = $(window).width();
	var BodyHeight = $(".content").height();
	
	var Page = $("#imagebox").attr('page');
	if(!Page){return false;}
	
	$("#imagebox").css({
		'width': BodyWidth + 'px', 
		'height':BodyHeight + 'px', 
		'overflow':'hidden', 
		'position':'relative', 
		'z-index':'1'
	});
	
	var PictureWidth = $("#picture > img").width();
	
	var Frames = 1;
	if(PictureList[Page]['frames']){
		Frames = PictureList[Page]['frames'];
	}
	
	var StartFrame = 1;
	if(PictureList[Page]['start']){
		StartFrame = PictureList[Page]['start'];
	}
	
	var CurrentFrame = StartFrame;
	
	var StartLeft = 0;
	
	var FrameWidth = parseInt(PictureWidth / Frames);
	
	var FramesLeft = new Array();
	
	for(i = 0; i < Frames; i++){
		FramesLeft[i + 1] = FrameWidth * i;	
	}
	
	FramesLeft[FramesLeft.length - 1] = PictureWidth - BodyWidth;
	
	
	StartLeft = FramesLeft[StartFrame];
	
	
	$("#picture").css({
		'position':'relative', 
		'left': -StartLeft + 'px', 
		'z-index':'2'
	});
	
	// Buttons 
	
	var ButtonTop = BodyHeight / 2 - 36 / 2;
	var ButtonRightLeft = (BodyWidth - 100) - (36 / 2);
	
	$("#left").css({
		'position':'absolute', 
		'z-index':'99', 
		'width':'36px', 
		'height':'36px', 
		'background':'url(\'/images/left.png\') no-repeat left top', 
		'top': ButtonTop + 'px', 
		'left':'100px',
		'cursor':'pointer'
		
	}).click(function(event){
		if(this == event.target){
			$("#left").show();
			$("#right").show();
			
			//var Next = CurrentFrame < Frames ? CurrentFrame + 1 : Frames;
			var Next = CurrentFrame > 1 ? CurrentFrame - 1 : 1;
			var Left = FramesLeft[Next];
			CurrentFrame = Next;
			$("#picture").animate( {left: -Left + 'px'}, 1000);
			
			if(CurrentFrame == 1){
				$("#left").hide();
			}

		}
		return false;	
	});
	
	if(CurrentFrame > 1){
		$("#left").show();
	}
	
	$("#right").css({
		'position':'absolute', 
		'z-index':'99', 
		'width':'36px', 
		'height':'36px', 
		'background':'url(\'/images/right.png\') no-repeat left top', 
		'top': ButtonTop + 'px', 
		'left': ButtonRightLeft + 'px', 
		'cursor':'pointer'
		
	}).click(function(event){
		if(this == event.target){
			$("#left").show();
			$("#right").show();
			//var Next = CurrentFrame > 1 ? CurrentFrame - 1 : 1;
			var Next = CurrentFrame < Frames ? CurrentFrame + 1 : Frames;
			var Left = FramesLeft[Next];
			CurrentFrame = Next;
			$("#picture").animate( {left: -Left + 'px'}, 1000);
			
			if(CurrentFrame == Frames){
				$("#right").hide();
			}
			

		}
		return false;
	});
	
	if(CurrentFrame < Frames){
		$("#right").show();
	}
	
	
	
	// 
  
	var IsLinkBox = $(".home_link").html();
	if(IsLinkBox){
		$("#picture > img")/*.click(function(event){
			if(this == event.target){
				//alert(CurrentFrame);
				var LinkBox = $("#home_link_" + CurrentFrame);
				if(LinkBox){
					var HomeLink = LinkBox.html();
					if(HomeLink.length > 0){
						//alert(HomeLink);
						location.href = '/' + HomeLink;
					}
				}
			}
			
		})*/.css({'cursor':'pointer'});
	}
}


