/* Vahdin  valinnat disabloitu. Vahdin checkboxin ruksaaminen avaa vahtivalinnat. */

function toggleCheckboxDependencies(checkboxObj) {
  if (checkboxObj.checked) {
    checkboxObj.value = "y";
    for (i = 1; i < arguments.length; i++) 
    {
	    element = document.getElementById(arguments[i]);
	    if(element){
			/* disabloidaan radiobutton ryhmä loopaten */
			if(element.type=="radio")
			{
				radiobuttons = document.getElementsByName(arguments[i]); /* huomaa ByName, ei ById */
				for(x=0 ; x<radiobuttons.length ; x++)
				{
					radiobuttons[x].disabled = false;
				}
			}
			/* muut elementit disabloidaan normaalisti */
			else
			{
				element.disabled = false;
			}
		}
    }
  }
  else {
    checkboxObj.value = "";
    for (i = 1; i < arguments.length; i++) 
    {
	    element = document.getElementById(arguments[i]);
	    if(element){

			/* disabloidaan radiobutton ryhmä loopaten */
			if(element.type=="radio")
			{
				radiobuttons = document.getElementsByName(arguments[i]); /* huomaa ByName, ei ById */
				for(x=0 ; x<radiobuttons.length ; x++)
				{
					radiobuttons[x].disabled = true;
				}
			}
			/* muut elementit disabloidaan normaalisti */
			else
			{
				element.disabled = true;
			}
		}
    }
  }
}

/* Vapautetaan vahtivalinnat, jos vahti on voimassa. */
function enableElements(elements) {
	for (i = 0; i < arguments.length; i++) {
		element = document.getElementById(arguments[i]);
		if(element){
			/* disabloidaan radiobutton ryhmä loopaten */
			if(element.type=="radio")
			{
				radiobuttons = document.getElementsByName(arguments[i]); /* huomaa ByName, ei ById */
				for(x=0 ; x<radiobuttons.length ; x++)
				{
					radiobuttons[x].disabled = false;
				}
			}
			/* muut elementit disabloidaan normaalisti */
			else
			{
				element.disabled = false;
			}
		}
	}
}

/* Disabloidaan vahtivalinnat, jos vahtia ei ole. */
function disableElements(elements) {
	for (i = 0; i < arguments.length; i++) 
	{
		element = document.getElementById(arguments[i]);
		if(element){
			/* disabloidaan radiobutton ryhmä loopaten */
			if(element.type=="radio")
			{
				radiobuttons = document.getElementsByName(arguments[i]); /* huomaa ByName, ei ById */
				for(x=0 ; x<radiobuttons.length ; x++)
				{
					radiobuttons[x].disabled = true;
				}
			}
			/* muut elementit disabloidaan normaalisti */
			else
			{
				element.disabled = true;
			}
		}
	}
}


/* Viesti välittäjälle toiminnon päälle/pois kytkentä jos vahti luottamuksellinen */
function toggleMessage(element1,element2, element3) {
	if(element1.checked && element3.checked){
		element2.disabled = false;
	}
	else{
		element2.disabled = true;

	}
}

/*  END Viesti välittäjälle toiminnon päälle/pois kytkentä jos vahti luottamuksellinen */


/* Kohdesivulla vaihtaa ison kuvan thumbnailin mukaiseksi, vaihtaa ison kuvan ympärillä olevan linkin
   viittaamaan kuvan originaaliin ja tekee thumbnailiin valkoisen kehyksen sekä vaihtaa kuvatekstin */
function changePic(picId, origPicPath, wwwPicPath, thumbId, imageTitle)
{
  //Jos originaalin osoite on annettu, vaihdetaan se linkin hrefiin
  if(origPicPath) {
    origLinkElement = document.getElementById('origImageLink');
    if(origLinkElement) {
      origLinkElement.href = origPicPath;
    }
  }

  //Vaihdetaan kuva
  picElement = document.getElementById(picId);
  picElement.src=wwwPicPath;
  for( i=0; i<=9; i++) { 
    currentOldImage = document.getElementById("thumb" + i);
    if (currentOldImage) {
      currentOldImage.style.border = "1px solid #333333";
    }
  }

  //Muutetaan aktiivisen kuvan kehys punaiseksi
  thumbImage = document.getElementById(thumbId);
  thumbImage.style.border = "1px solid #ff0000";

  //Vaihdetaan kuvateksti
  title = document.getElementById('imageTitle');
  if(title) {
    text = document.createTextNode(imageTitle);
    while (title.hasChildNodes()) {
      title.removeChild(title.lastChild);
    }
    title.appendChild(text);
  }
}

/* Select-lista navigointina */
function goThere() {
    var list = document.forms["selectNavi"].urlList;
    if (list.options[list.selectedIndex].value != 0) {
    location.href = list.options[list.selectedIndex].value;
    }
}


/* Listasivulla mikäli kohteita on listattuna yli 100 kappalta, ei printtisivun näyttäminen ole mahdollista. */ 
function listPageCheckItemAmount(item_amount, max_row_amount_in_print, list_id)
{
	if(item_amount > max_row_amount_in_print)
		alert("Tulostettavaa sivua ei näytetä jos hakua vastaavia kohteita löytyy yli "+max_row_amount_in_print+" kappaletta.\n Rajaa kohdemäärää tarkentamalla hakuehtojasi.");	
	else
		window.open('itemlist_print.jsp?list_id='+list_id+'&max_row_amount_in_print='+max_row_amount_in_print+'&portal=eo','popUp','resizable=yes,scrollbars=yes,width=850,height=600');
}

/* Listasivulla mikäli kohteita on listattuna yli 100 kappalta, ei printtisivun näyttäminen ole mahdollista. */ 
function favoritesPageCheckItemAmount(item_amount, max_row_amount_in_print, list_id)
{
	if(item_amount > max_row_amount_in_print)
		alert("Tulostettavaa sivua ei näytetä jos hakua vastaavia kohteita löytyy yli "+max_row_amount_in_print+" kappaletta.\n Rajaa kohdemäärää tarkentamalla hakuehtojasi.");	
	else
		window.open('favorites_print.jsp?list_id='+list_id+'&max_row_amount_in_print='+max_row_amount_in_print+'&portal=eo','popUp','resizable=yes,scrollbars=yes,width=850,height=600');
}


/* Kohdesivun uudet kuvien näyttöskriptit alkavat PPI 2.1.2008 */
/* Monesta funktiosta on tehty uudiskohde-esittelysivuja varten oma versio (pääte _v2), koska uudiskohdepuolella näytetään kuvien lisäksi flashia. 
Uudet versiot on tehty, jotta muutokset eivät sotkisi asuntopuolen kuvien näyttämistä. 
Uudiskohdepuolen skripteissä tarvitaan kahta muuttujaa: flash_found ja flash_selected ja jotka alustetaan thumbs.jspf:ssä
*/

function initSlots() {
	for (i = 0; i < slotAmount; i++) {
		if (i < allImages.length) {
			timage = allImages[i];
			addSlot(i,timage);
		}
	}
	 for (i = 0; i < allSlots.length; i++) {
		allSlots[i].updateSrc();
		allSlots[i].deActivate();
	}
	if (allSlots.length > currentSlot) {
		allSlots[currentSlot].activate();
	} else if (allSlots.length > 0) {
		allSlots[0].activate();
	}/* */
}

function initSlots_v2() {
	for (i = 0; i < slotAmount; i++) {
		if (i < allImages.length) {
			timage = allImages[i];
			addSlot(i,timage);
		}
	}
	 for (i = 0; i < allSlots.length; i++) {
		allSlots[i].updateSrc();
		allSlots[i].deActivate();
	}
	
	if (flash_selected != true){
		if (allSlots.length > currentSlot ) {
			allSlots[currentSlot].activate();
		} else if (allSlots.length > 0) {
			allSlots[0].activate();
		}
	}
}


function nextImage() {	
	// if image is last, try to change slot selection
	if (allSlots[allSlots.length - 1].image.isLastImage() == true) {
		if (currentSlot < allSlots.length - 1) {
			allSlots[currentSlot].deActivate();
			currentSlot++;
			allSlots[currentSlot].activate();
		}
	} else {
	// otherwise show next image and update slots
		for (i = 0; i < allSlots.length; i++) {
			imagenum = allSlots[i].image.num;
			allSlots[i].setImage(allImages[imagenum + 1]);
			allSlots[i].updateSrc();
		}		
	}
	checkButtonState();
	showCurrentBigImage();
}

function nextImage_v2() {	

	if (flash_selected == true){
			selectImage(0);
	}

	// if image is last, try to change slot selection
	else if (allSlots[allSlots.length - 1].image.isLastImage() == true) {
		if (currentSlot < allSlots.length - 1) {
			allSlots[currentSlot].deActivate();
			currentSlot++;
			allSlots[currentSlot].activate();
		}
	} else {
	// otherwise show next image and update slots
		for (i = 0; i < allSlots.length; i++) {
			imagenum = allSlots[i].image.num;
			allSlots[i].setImage(allImages[imagenum + 1]);
			allSlots[i].updateSrc();
		}
	}
		
	checkButtonState_v2();
	showCurrentBigImage();
}


function previousImage() {
	// if image is first, try to change slot selection
	if (allSlots[0].image.isFirstImage() == true) {
		if (currentSlot > 0) {
			allSlots[currentSlot].deActivate();
			currentSlot--;
			allSlots[currentSlot].activate();
		}
	} else {
	// otherwise show next image and update slots
		for (i = 0; i < allSlots.length; i++) {
			imagenum = allSlots[i].image.num;
			allSlots[i].setImage(allImages[imagenum - 1]);
			allSlots[i].updateSrc();
		}		
	}
	showCurrentBigImage();
	checkButtonState();
}

function previousImage_v2() {
	// if image is first, try to change slot selection
	if (allSlots[0].image.isFirstImage() == true) {
		if (currentSlot == 0 && flash_found == true) {
			selectFlash();
		}
		if (currentSlot > 0) {
			allSlots[currentSlot].deActivate();
			currentSlot--;
			allSlots[currentSlot].activate();
		}
	} else {
	// otherwise show next image and update slots
		for (i = 0; i < allSlots.length; i++) {
			imagenum = allSlots[i].image.num;
			allSlots[i].setImage(allImages[imagenum - 1]);
			allSlots[i].updateSrc();
		}
	}
	showCurrentBigImage();
	checkButtonState_v2();
}


function showCurrentBigImage() {
	var bigImg = document.getElementById("bigimage");
	bigImg.src = allSlots[currentSlot].image.bigSrc;
}

function showCurrentBigImage_v2() {
	var bigImg = allSlots[currentSlot].image.bigSrc;
	document.getElementById('nb_premium_big_img_base').innerHTML='<a href="javascript:openBigImage()"><img src="'+ bigImg + '" id="bigimage" alt="Iso kuva" title="Näytä kaikki kuvat" /></a>'
}


function checkButtonState() {
		var leftArrowImg = document.getElementById("btnArrowLeft");
		var rightArrowImg = document.getElementById("btnArrowRight");
		if ((allSlots[0].image.isFirstImage() == true) && (currentSlot == 0)) {
			leftArrowImg.src = btn_arrow_left_passive; // "ico/btn_arrow_left_passive.jpg";
		} else {
			leftArrowImg.src = btn_arrow_left_active; // "ico/btn_arrow_left_active.jpg";
		}
		if ((allSlots[allSlots.length - 1].image.isLastImage() == true)  && (currentSlot == allSlots.length - 1)) {
			rightArrowImg.src = btn_arrow_right_passive; // "ico/btn_arrow_right_passive.jpg";
		} else {
			rightArrowImg.src = btn_arrow_right_active; // "ico/btn_arrow_right_active.jpg";
		}
}

function checkButtonState_v2() {
		var leftArrowImg = document.getElementById("btnArrowLeft");
		var rightArrowImg = document.getElementById("btnArrowRight");
		if ((allSlots[0].image.isFirstImage() == true) && (currentSlot == 0)) {
			leftArrowImg.src = btn_arrow_left_passive; // "ico/btn_arrow_left_passive.jpg";
			if (flash_found == true){
				leftArrowImg.src = btn_arrow_left_active;
			}
		} else {
			leftArrowImg.src = btn_arrow_left_active; // "ico/btn_arrow_left_active.jpg";
		}
		if ((allSlots[allSlots.length - 1].image.isLastImage() == true)  && (currentSlot == allSlots.length - 1)) {
			rightArrowImg.src = btn_arrow_right_passive; // "ico/btn_arrow_right_passive.jpg";
			if (flash_selected == true){ // korjaa tilanteen jossa ollaan viimeisessä kuvassa ja valitaan klikkaamalla flash, currentSlotiksi jää viimeinen kuva
				rightArrowImg.src = btn_arrow_right_active;
			}
		} else {
			rightArrowImg.src = btn_arrow_right_active; // "ico/btn_arrow_right_active.jpg";
		}
		if (flash_selected == true){
			leftArrowImg.src = btn_arrow_left_passive;
	}
}


function setSlot(num) {
	allSlots[currentSlot].deActivate();
	currentSlot = num;
	allSlots[currentSlot].activate();
	showCurrentBigImage();
	checkButtonState();
}

function setSlot_v2(num) {
	allSlots[currentSlot].deActivate();
	currentSlot = num;
	allSlots[currentSlot].activate();
	showCurrentBigImage_v2();
	checkButtonState();
}


function openBigImage() {
	openHref = bigPicHref + allSlots[currentSlot].image.num;
	window.open(openHref, 'suurennetut_kuvat', 'scrollbars=yes,width=650,height=790,resizable=yes');
}

/* lisätty 03/08 uudiskohde-esittelyt-osion asemapiirroksen ja yleispohjan avaamista varten */
function openBigImageWithNum(num) {
	openHref = bigPicHref + num;
	window.open(openHref, 'suurennetut_kuvat', 'scrollbars=yes,width=650,height=790,resizable=yes');
}


function addImage(num,thumbSrc,bigSrc,width,height) {
	allImages.push(new objImage(num,thumbSrc,bigSrc,width,height));
}

/* Class representing information of a single image, which can be placed to a slot */
function objImage(num,thumbSrc,bigSrc,width,height) {
	this.num = num;
	this.thumbSrc = thumbSrc;
	this.bigSrc = bigSrc;
	this.width = width;
	this.height = height;
}

objImage.prototype.isFirstImage = function() {
	isFirst = false;
	if (this.num == 0) {
		isFirst = true;
	}
	return isFirst;
}

objImage.prototype.isLastImage = function() {
	isLast = false;
	if (allImages.length - 1 == this.num) {
		isLast = true;
	}
	return isLast;
}

function addSlot(num,image) {
	allSlots.push(new Slot(num,image));
}

/* Class representing information of a slot, meaning visible placeholder image on page */
function Slot(num,image) {
	this.num = num;
	this.image = image;
}

Slot.prototype.deActivate = function() {
	thisImage = document.getElementById("thumb" + this.num);
    if (thisImage) {
      thisImage.style.border = "1px solid #333333";
    }
}

Slot.prototype.activate = function() {
	thisImage = document.getElementById("thumb" + this.num);
    if (thisImage) {
      thisImage.style.border = "1px solid #FF0000";
    }
}

Slot.prototype.updateSrc = function() {
	thisImage = document.getElementById("thumb" + this.num);
    if (thisImage) {
      thisImage.src = this.image.thumbSrc;
      thisImage.width = this.image.width;
      thisImage.height = this.image.height;
    }
}

Slot.prototype.setImage = function(image) {
	this.image = image;
}

// helper function for debugging
function trace(x) {
	setTimeout(function(){throw x}, 0) 
}


// for testing
function chekstatus() {
	for (i = 0; i < allSlots.length; i++) {
			trace(i + " " + allSlots[i].image.num + " thumbSrc " + allSlots[i].image.thumbSrc + " bigSrc " + allSlots[i].image.bigSrc);			
	}
}


function selectFlash() {
	allSlots[currentSlot].deActivate();
	//activate flash thumbnail image
	flashImage = document.getElementById("flash_esitys");
	if (flashImage) {
      flashImage.style.border = "1px solid #FF0000";
	}
	document.getElementById('nb_premium_big_img_base').innerHTML='<object width="470" height="340"><param name="movie" value="' + flash_url + '" /><embed src="'+ flash_url +'" width="470" height="340"></embed></object>';
	flash_selected=true;
	checkButtonState_v2();
}

/* for realtycomplexes (which include images and flash-shows) */
function selectImage(num) {
	//deactivate flash thumbnail image
	flashImage = document.getElementById("flash_esitys");
	if (flashImage) {
      flashImage.style.border = "1px solid #333333";
	}
	flash_selected=false;
	setSlot_v2(num);
	checkButtonState_v2();
}
/* Kohdesivun uudet kuvien näyttö skriptit loppuvat PPI 2.1.2008 */
