var IMAGESLIDESHOW = function() {
	var myImages = "";
	var myImagesId = "";
	var myImagesTitles = "";	
	var secondsBetweenPhotos = 5;
	var divName1 = "loader1";
	var divName2 = "loader2";
	var div_name = "";
	var divTitlesName = "loaderTitle";	
	var divPreloaderName = "preloader";	
	var secondsBetweenLoad = 1;
	var countReservedImg = 2;
	var timer;
	var timerPreLoad;
	var thisImg = myImages.length - 1;
	var nextImg = 0;
	var next = 0;	
	var isSetPrev = 0;
	var isSetAnimRemove = 0;
	var isSetAnimNew = 0;
	var isSetAnimNewShow = 0;	
	var buttonClicked = -1;
	
	function changeTitlePos(divName){
		var containerDivHeight = $("#container").css("height");
		var imgHeight = $("#"+divName+" img").css("height");
		containerDivHeight = containerDivHeight.split("px");
		imgHeight = imgHeight.split("px");
		var newPos = parseInt((containerDivHeight[0] - imgHeight[0])/2);
		$("#loadTitle").css("top", -newPos+"px");
	}
	
	function setButtonsStatus(){
		var imageLength = myImages.length;
		if(parseInt(isSetAnimRemove) == 0 && parseInt(isSetAnimNew) == 0 && parseInt(isSetAnimNewShow) == 0 && imageLength > 1){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	function getNextIndex(arg1){
		if(isSetPrev){
			if (arg1 == 0) {
				return myImages.length - 1;
			} else{
				return arg1 - 1;
			}
		} else{
			if (arg1 + 1 == myImages.length) {
				return 0;
			} else{
				return arg1 + 1;
			}
		}
	}	
	function reverseBoxes(){
		if($("#box1").is(".underBox")){
			$("#box1").removeClass("underBox");
			$("#box2").removeClass("overBox");			
			$("#box1").addClass("overBox");
			$("#box2").addClass("underBox");			
		} else{
			$("#box1").removeClass("overBox");
			$("#box2").removeClass("underBox");			
			$("#box1").addClass("underBox");
			$("#box2").addClass("overBox");
		}
	}
	function renameBubbles(renameType, divName){
		if (divName == null){
			divName = "allBubbles";
		}
		if (renameType == null){
			renameType = 0;
		}

		var $kids = $("#" + divName).children();
		if($kids.length){
			$("#" + divName).children().each(function(){
				if(renameType == 0){
					if($(this).attr("name") == "cmpballoon")
						$(this).attr("name", "cmpballoonhide");
				}		
				else{					
					if($(this).attr("name") == "cmpballoonhide")
						$(this).attr("name", "cmpballoon");									
				}		
			});
		}
	}
	function moveBubbles(imgid, divName){
		var $kids = $("#allBubbles").children();
		if($kids.length){
			$("#allBubbles").children().each(function(){
				var cloneobj = $(this).clone(true);
				if($(cloneobj).attr("linkedimg") == imgid){
					$("#"+ divName).append($(cloneobj));
				}	
			});
		}	
		renameBubbles(1, divName);
		$("#" + divName).find("div[name='cmpballoon']").each(function() {
				$(this).css("opacity", 0);
		});		
		InitSpeechBubbles();
		$("#" + divName).find("div[name='cmpballoon']").each(function() {
				$(this).animate({opacity: 1}, 1000);
		});		
	}
	function reverseBoxLoading(){
		if( parseInt(isSetAnimNewShow) != 0 ){
			$("#boxLoading").removeClass("underBox");
			$("#boxLoading").addClass("overBox");
			$('body').css('cursor', 'progress');		
		} else{
			$("#boxLoading").removeClass("overBox");
			$("#boxLoading").addClass("underBox");
			$('body').css('cursor', 'default')
		}
	}
	function setVars(currImg){
		renameBubbles();
		$('body').css('cursor', 'progress');
		$("#loadTitle").css("color", $(".slideshowmailphoto").css("color"));
		isSetPrev = 0;
		if($(".slideshowdelimiter").length > 0){
			var baseUrl = $(".slideshowbaseurl").val();
			var delimiter = $(".slideshowdelimiter").val();
			if($(".slideshowlist").length > 0){	
				myImagesId = $(".slideshowlist").val().split(delimiter);
				myImages = $(".slideshowlist").val().split(delimiter);
			}
			for(var i = 0; i < myImages.length; i++){
				myImages[i] = baseUrl + myImages[i];
			}	
			if($(".slideshowtitleslist").length > 0){	
				myImagesTitles = $(".slideshowtitleslist").val().split(delimiter);
			}
			if(currImg){
				setBothDivs(currImg);
			}
			else{
				if($(".slideshowcurrentimage").length > 0){	
					for(var i = 0; i < myImages.length; i++){
						if (baseUrl + $(".slideshowcurrentimage").val() == myImages[i]){
							setBothDivs(i);
							break;
						}
					}	
				}
			}
		}
	}	
	function setBothDivs(arg1){
		thisImg = arg1;
		nextImg = getNextIndex(thisImg);
		if($("#" + divName1 + " img").length > 0 && $("#" + divName2 + " img").length > 0){
			if(next == 0){
				$("#"+divName2).html("");
				newImage(myImages[nextImg], divName2, 0, nextImg);					
			} else{
				$("#"+divName1).html("");	
				newImage(myImages[nextImg], divName1,  0, nextImg);	
			}	
		}
		else{
			newImage(myImages[thisImg], divName1,  1, thisImg);				
			newImage(myImages[nextImg], divName2, 0, nextImg);	
		}
	}
	function nextImage () {	
		if(isSetPrev){
			thisImg--;
			if (thisImg == 0)
				thisImg = myImages.length - 1;
		} else{
			thisImg++;
			if (thisImg == myImages.length)
				thisImg = 0;
		}
		return myImages[thisImg];
	}	
	function changeImage () {
		if(parseInt(isSetAnimRemove) == 0 && parseInt(isSetAnimNew) == 0 && parseInt(isSetAnimNewShow) == 0){
			reverseBoxLoading();
			var t = myImages[thisImg];
			var n = nextImage();	
			if (t != n) {	
				if(next == 0){
					removeImage(n, divName1);
				} else{
					removeImage(n, divName2);
				}
			} else {
				changeImage();
			}
		}
		else{
			reverseBoxLoading();
		}
	}	
	function removeImage(src, divName) {
		timerPreLoad = setInterval(checkPreLoad, 500);
		$("#"+divName+"Title").html();
		$("#loadTitle").animate({opacity: 0}, 100);
		$("#" + divName).find("div[name='cmpballoon']").each(function() {
				$(this).animate({opacity: 0}, 1000);
		});
		isSetAnimRemove++;
		$("#"+divName+" img").each(function(){
			if($(this).attr("id") != "balloonimage"){
				$(this).animate({opacity: 0}, 1000, function(){
					nextImg = getNextIndex(thisImg);
					reverseBoxes();			
					if(next == 0){
						next = 1;
						newImage(myImages[nextImg], divName1, 0, nextImg);
					} else{
						next = 0;
						newImage(myImages[nextImg], divName2, 0, nextImg);
					}
					$(this).remove();
					$("#"+divName+" img").html();
					isSetAnimRemove--;	
				});
			}
		});

		isSetAnimNew++;
		if(next == 0){
			$("#"+divName2+" img").each(function(){
				if($(this).attr("id") != "balloonimage"){
					$(this).animate({width: "100%", opacity: 1}, 1000, function(){
						isSetAnimNew--;
						moveBubbles($(this).attr("id"), divName2);
						$("#loadTitle").html($("#"+divName2+"Title").html());
						changeTitlePos(divName2);
						$("#loadTitle").animate({opacity: 1}, 100);	
						if (jQuery.browser.msie){
							this.style.removeAttribute('filter');	//for IE								
						}
					});
				}
			});			
		} else{
			$("#"+divName1+" img").each(function(){
				if($(this).attr("id") != "balloonimage"){
					$(this).animate({width: "100%", opacity: 1}, 1000, function(){
						isSetAnimNew--;
						moveBubbles($(this).attr("id"), divName1);
						$("#loadTitle").html($("#"+divName1+"Title").html());
						changeTitlePos(divName1);						
						$("#loadTitle").animate({opacity: 1}, 100);											
						if (jQuery.browser.msie){
							this.style.removeAttribute('filter');	//for IE								
						}
					});
				}
			});				
		}
	}
	function newImage(src, divName, opacityType, currIndex){
		isSetAnimNewShow++;
		$("#"+divName+"Title").html(myImagesTitles[currIndex]);
		$("#"+divName).html("");
		var largeImage = new Image();
		$(largeImage).load(function(){
			//$(this).hide();
			$("#"+divName).append(this);
			$("#"+divName+" img").css("opacity", opacityType);
			$(this).show(1, function(){
				isSetAnimNewShow--; 	
				reverseBoxLoading(); 
				if(buttonClicked != -1){
					ShowPhoto(buttonClicked);
					buttonClicked = -1;
				}
				setImageAttr(largeImage, myImagesId[currIndex]);
				if(opacityType == 1){//for first image
					moveBubbles(myImagesId[currIndex], divName);
					changeTitlePos(divName);
					$("#loadTitle").html($("#"+divName+"Title").html());
					if (jQuery.browser.msie){
						this.style.removeAttribute('filter');	//for IE
					}
				}
			});	
		});  
		$(largeImage).attr("src", src);

	}
	function setImageAttr(imgObj, imgid){
		$(imgObj).attr("id", imgid);
		$(imgObj).css("position", "relative");
		$(imgObj).css("top", "0px");
		$(imgObj).css("left", "0px");
	}
	function checkForLoaded () {
		clearInterval(timer);
		changeImage();
		timer = setInterval(changeImage, (secondsBetweenPhotos * 1000));
	}
	function StopSlideShow(){
		clearInterval(timer);
		clearInterval(timerPreLoad);
		$(".stopSlideShow").css("display", "none");
		$(".startSlideShow").css("display", "block");
	}
	function StartSlideShow(){
		StopSlideShow();
		isSetPrev = 0;
		buttonClicked = -1;
		
		setVars(thisImg);
		timer = setInterval(checkForLoaded, 500);
		$(".startSlideShow").css("display", "none");
		$(".stopSlideShow").css("display", "block");
	}
	function ShowPhoto(arg1){
		if(setButtonsStatus() == 0){
			reverseBoxLoading();
			StopSlideShow();
			isSetPrev = arg1;		
	
			if(next == 0){
				nextImg = getNextIndex(thisImg);
				$("#"+divName2).html("");
				var largeImage = new Image();
				$(largeImage).attr("src", myImages[nextImg]);
				setImageAttr(largeImage, myImagesId[nextImg]);
				$("#"+divName2).append($(largeImage));
				$("#"+divName2+" img").css("opacity", 0);
				$("#"+divName2+"Title").html(myImagesTitles[nextImg]);
				thisImg = nextImg;
				nextImg = getNextIndex(thisImg);
				removeImage(myImages[nextImg], divName1);	
			} else{
				nextImg = getNextIndex(thisImg);
				$("#"+divName1).html("");
				var largeImage = new Image();
				$(largeImage).attr("src", myImages[nextImg]);
				setImageAttr(largeImage, myImagesId[nextImg]);
				$("#"+divName1).append($(largeImage));
				$("#"+divName1+" img").css("opacity", 0);  
				$("#"+divName1+"Title").html(myImagesTitles[nextImg]);
				thisImg = nextImg;
				nextImg = getNextIndex(thisImg);
				removeImage(myImages[nextImg], divName2);			
			}
			showComments(true);
		}
		else{
			buttonClicked = arg1;
			reverseBoxLoading();
		}
	}
	function setMailPhotoUrl(obj){
		var newid = myImages[thisImg].split("id=");
		var newurl = $(obj).attr("href") + newid[1];
		$(obj).attr("href", newurl);
	}	
	function preLoad(){
		var nextInd = thisImg;
		for(var i = 0; i < countReservedImg; i++){
			nextInd = getNextIndex(nextInd);
			if(myImages[nextInd])
				loadImage(myImages[nextInd]);			
		}
		clearInterval(timerPreLoad);
	}	
	function loadImage(src){
		var largeImage = new Image();
		$(largeImage).load(function(){
			$(this).hide();
			$("#"+divPreloaderName).append(this);                                             
		});  
		$(largeImage).attr("src", src); 	
	}	
	function checkPreLoad() {
		if (document.getElementById(divPreloaderName) != null) {
			clearInterval(timerPreLoad);
			preLoad();
			timerPreLoad = setInterval(preLoad, (secondsBetweenLoad * 1000));
		}
	}
	function updateCommentProperties() {
		if ($(".comment_box_container").length) {
			$(".page_item_id").val(myImagesId[thisImg]);
			COMMENT.ViewComments();
		}
	}
	function showComments(show) {
		if (show) {
			updateCommentProperties();
			$(".userpages_comments_container").show();
		}
		else
			$(".userpages_comments_container").hide();
	}
	return {
		initslideshow: function initslideshow() {
			setVars(0);
			showComments(true);
			$(".stopSlideShow").click(function() {
				StopSlideShow();
				showComments(true);
			});	
			$(".startSlideShow").click(function() {																
				if(setButtonsStatus() == 0)
				{
					StartSlideShow();
					showComments(false);
				}
			});		
			$(".nextPhoto").click(function() {
				ShowPhoto(0);
			});	
			$(".prevPhoto").click(function() {
				ShowPhoto(1);
			});	
			$(".slideshowmailphoto").click(function() {
				setMailPhotoUrl(this);
			});
						
		}		
	}		
}();		

$(document).ready(function() {
	IMAGESLIDESHOW.initslideshow();
});