﻿
var imgControl =
{
	//"Public"
	preventInit: false,
	
	//"Private"
	_count: 0,
	_flowcount: 0,
	_bigImg: null,
	_thumbs: null,
	_currentImg: null,
	_currentControl: "#image-control",
	_currentImgLink: null,
	_isStartup: true,
	_isSlideShow: false,
	_leftbtn: ".img-leftbtn",
	_rightbtn: ".img-rightbtn",
	_itemsPerPage: 10,
	_currentPage: 1,

	closeSlideshow: function() {

		//var slideshowDiv = document.getElementById("slideshow-container");
		// var alphaDiv = document.getElementById("slideshow-alpha-box");

		$("#slideshow-container").hide();
		$("#slideshow-alpha-box").hide();

		/* For SWFOBJECT  */
		$(imgControl._currentControl + " object").hide();

		imgControl._currentControl = "#image-control";
		imgControl._isSlideShow = false;

		$(imgControl._currentControl + " object").show();

		imgControl.initializeImgControl();

	},

	openSlideshow: function() {
		/*Check for FilterSearch elements*/
		var containerCheck = document.getElementById("result-container");

		var slideShowDiv = $("#slideshow-container");
		var slideShowHolder = document.getElementById("slideshow-holder");
		if (slideShowHolder.className != "hasChildren") {
			$("#slideshow-holder").append(slideShowDiv);
			$("#slideshow-holder").addClass("hasChildren");
		}


		if (containerCheck != null) {
			if ($.browser.safari)
				slideShowDiv.css("top", document.body.scrollTop + 40 + "px");
			else
				slideShowDiv.css("top", document.documentElement.scrollTop + 40 + "px");

		}


		slideShowDiv.show();
		$("#slideshow-alpha-box").show();

		$(imgControl._currentControl + " object").hide();
		imgControl._currentControl = "#slideshow-container";
		imgControl._isSlideShow = true;
		$(imgControl._currentControl + " object").show();
		// alert($("object").length);
		$(".flash-holder").hide();
		imgControl.initializeImgControl();

	},

	setCurrentControl: function(control) {
		imgControl._currentControl = control;
	},

	clickThumb: function(sender, imgUri, text) {
		if (imgUri != "flv") {
			$(imgControl._currentControl + " .flash-holder").hide();
			$(".disc-alpha").show();
			imgControl._bigImg.src = imgUri;
			imgControl._bigImg.alt = $(sender).children()[0].alt;
		}
		else {
			$(imgControl._currentControl + " .flash-holder").show();
			$(".disc-alpha").hide();

		}

		var disc = $(".disclaimer-alpha-img");
		if (typeof (text) == "undefined" || text == "" || text == null)
			disc.hide();
		else {
			if (text == "default")
				text = "Bildmaterialet är för en typbostad i det aktuella projektet och inte den valda bostaden";
			disc.html("<p>" + text + "</p>");
			disc.show();
		}

		imgControl._count = imgControl._thumbs.index(sender);
		$(".thumb_span").text((imgControl._count + 1) + '');
		$(imgControl._currentImgLink.firstChild).css("border", "1px solid #FFF");
		imgControl._currentImgLink = sender;
		$(sender.firstChild).css("border", "1px solid #000");


	},


	initializeImgControl: function() {
		imgControl._bigImg = $(imgControl._currentControl + " .img-container").children()[0];

		if (imgControl._isStartup) {
			imgControl._thumbs = $(imgControl._currentControl + " .ul_thumbs li").children();
			imgControl._currentImgLink = imgControl._thumbs[0];
			$(imgControl._thumbs[0]).click();

			if ($.browser.msie) {
				$(imgControl._thumbs[0]).attr("id", "clickedButton");
				doFire("clickedButton");
			}

			if (imgControl._thumbs.length > imgControl._itemsPerPage)
				$(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._itemsPerPage - 1) + ")").hide();

			$(".thumb_count_span").text(imgControl._thumbs.length + '');
			$(".thumb_span").text(imgControl._currentPage + '');

			imgControl._isStartup = false;
		}
		else {
			var index = imgControl._thumbs.index(imgControl._currentImgLink);
			imgControl._thumbs = $(imgControl._currentControl + " .ul_thumbs li").children();
			$(imgControl._thumbs[index]).click();

			if ($.browser.msie) {
				$(imgControl._thumbs[index]).attr("id", "clickedButton");
				doFire("clickedButton");

			}

			if (imgControl._thumbs.length > imgControl._itemsPerPage) {
				if (imgControl._count < imgControl._itemsPerPage) {
					$(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._itemsPerPage - 1) + ")").hide();
					$(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._itemsPerPage + 1) + ")").show();
				}
				else {
					$(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").hide();
					$(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").show();
				}
			}
		}
	},

	parseString: function(stringToParse) {
		var parsedString = "";

		for (var i = stringToParse.length - 9; i < stringToParse.length; i++)
			parsedString += stringToParse.charAt(i);

		return parsedString;
	},

	initNavButtons: function() {
		$(imgControl._leftbtn).click(function() {

			if (imgControl._count > 0) {
				if (imgControl._thumbs.length > imgControl._itemsPerPage) {
					$(imgControl._currentControl + " .ul_thumbs li:eq(" + (imgControl._count - 1) + ")").show();
					$(imgControl._currentControl + " .ul_thumbs li:eq(" + ((imgControl._count - 1) + imgControl._itemsPerPage) + ")").hide();
				}

				imgControl._count--;

				$(".thumb_span").text((imgControl._count + 1) + '');
				$(imgControl._thumbs[imgControl._count]).click();

				if ($.browser.msie) {
					$(imgControl._thumbs[imgControl._count]).attr("id", "clickedButton");
					doFire("clickedButton");
				}
			}
			else {

				imgControl._currentPage = Math.floor((imgControl._thumbs.length + imgControl._itemsPerPage) / imgControl._itemsPerPage);
				imgControl._count = imgControl._thumbs.length - 1;
				$(".thumb_span").text((imgControl._count + 1) + '');

				$(imgControl._thumbs[imgControl._count]).click();

				if ($.browser.msie) {
					$(imgControl._thumbs[imgControl._count]).attr("id", "clickedButton");
					doFire("clickedButton");
				}

				if (imgControl._thumbs.length > imgControl._itemsPerPage) {
					$(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").hide();
					$(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").show();
				}
			}

			return false;
		});

		$(imgControl._rightbtn).click(function() {

			if ((imgControl._count + 1) < imgControl._thumbs.length) {
				if (imgControl._thumbs.length > imgControl._itemsPerPage) {
					if ((imgControl._count + 1) >= imgControl._itemsPerPage) {
						$(imgControl._currentControl + " .ul_thumbs li:eq(" + ((imgControl._count + 1) - imgControl._itemsPerPage) + ")").hide();
						$(imgControl._currentControl + " .ul_thumbs li:eq(" + (imgControl._count + 1) + ")").show();
					}
				}

				imgControl._count++;
				$(".thumb_span").text((imgControl._count + 1) + '');
				$(imgControl._thumbs[imgControl._count]).click();

				if ($.browser.msie) {
					$(imgControl._thumbs[imgControl._count]).attr("id", "clickedButton");
					doFire("clickedButton");
				}
			}
			else if ((imgControl._count + 1) == imgControl._thumbs.length) {
				imgControl._count = 0;
				imgControl._currentPage = 1;
				$(imgControl._thumbs[0]).click();
				$(".thumb_span").text((imgControl._count + 1) + '');

				if ($.browser.msie) {
					$(imgControl._thumbs[imgControl._thumbs[0]]).attr("id", "clickedButton");
					doFire("clickedButton");
				}

				if (imgControl._thumbs.length > imgControl._itemsPerPage) {
					$(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._itemsPerPage - 1) + ")").hide();
					$(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._itemsPerPage) + ")").show();
				}
			}

			return false;
		});
	}
}

$(document).ready(function() {
	if (!imgControl.preventInit) {
		imgControl.initializeImgControl();
		imgControl.initNavButtons();
	}
});