// JavaScript Document
function loadPage(site,urlString,list,pgId) {
  url = '';
  urlArray = urlString.split(',');
  for (var i = 0; i < urlArray.length; i++) {
      urlSplit = urlArray[i].split('=');
      if(urlSplit[0] != pgId&&urlSplit[0]!=''){
        url = url + '&'+urlArray[i];
      }   
  } 
  if(list.options[list.selectedIndex].value!='-1'){
    url = url + '&'+pgId+'='+list.options[list.selectedIndex].value;
  }  
  
  location.href=site+url;
}

function addImage(list,field) { 
  if(list.options[list.selectedIndex].value!='-1'){ 
    field.value = field.value + list.options[list.selectedIndex].text + '\n';
  }
}

function resizeScreen() {
  windowWidth = window.innerWidth - 376;
  if(windowWidth < 678){
    windowWidth = 678;    
  }
  col = windowWidth / 226;
  col = Math.round(col-0.5);   
  if(col!=oldCol){   
    //location.href=site+'&col='+col;
    new Ajax.Request("?ctrl=updateCol", {
      method: "post",
      parameters: {'col':col, 'site':site},
      onComplete: window.location.href = site 
    });
  }	 
}

function resizeScreenAd() {
  windowWidth = window.innerWidth - 376;
  if(windowWidth < 678){
    windowWidth = 678;    
  }
  col = (windowWidth - 545) / 113;
  col = Math.round(col-0.5);   
  if(col!=oldCol){   
    //location.href=site+'&col='+col;
    new Ajax.Request("?ctrl=updateColAd", {
      method: "post",
      parameters: {'col':col, 'site':site},
      onComplete: window.location.href = site 
    });
  }	 
}

function imgResize(id) {
	windowHeigth = window.innerHeight;
	windowWidth = window.innerWidth;
	imgWidth = windowWidth;
	imgHeight = Math.round(0.625 * imgWidth);
	if (imgHeight < windowHeigth) {
		imgHeight = windowHeigth;
		imgWidth = Math.round(1.6 * imgHeight);
		offsetHeight = 0;
		offsetWidth = (windowWidth - imgWidth) / 2;
	} else {
		offsetHeight = (windowHeigth -imgHeight) / 2;
		offsetWidth = 0;
	}
	$(id).setStyle({
			width: imgWidth+'px',
			height: imgHeight+'px',
		top: offsetHeight+'px',
		left: offsetWidth+'px'
	});
}

function preloader(imageArray) {
	var i = 0;
	imageObj = new Image();
	var l = imageArray.length - 1;
	for(i=0; i<=l; i++) {
		imageObj.src=imageArray[i];
	}
}


/*function updateProductImage(inIndex) {	
	fadeDuration = 0.2;
	appearDuration = 0.7;
	
	imageName = imageList[inIndex];
	thumbId = "t"+inIndex;
	nextIndex = 1+parseInt(inIndex);
	
	$('image').fade({ duration: fadeDuration, afterFinish: function(){
		$('image').observe("load", function(){
	  		setThumb(thumbId); 
	  		$('caption').update(captionList[inIndex]);
	 		$('caption').innerHTML;
		   	$('image').appear({duration: appearDuration});  
	   	});	
	   	$('image').writeAttribute('src', ''); 
	   	$('image').writeAttribute('src', imageName); 
	   	$('image').setStyle({ width: "auto", height:"auto"});   
     } 
	}); 
   	imageListIndex = inIndex;
   	return false;
}  */

function updateProductImage(inIndex) {	
	fadeDuration = 0.2;
	appearDuration = 0.7;
	imageArray = imageList[inIndex].split("-");
	
	imageId = imageArray[0];
	imageName = imageArray[1];
	imageCaption = imageArray[2];
	thumbId = "t"+inIndex;
	nextIndex = 1+parseInt(inIndex);
	
	site = '?ctrl=articledetail&id='+artId+'&imgid='+imageId;
	
	$('image').fade({ duration: fadeDuration, afterFinish: function(){
	   $('image').observe("load", function(){
	  		setThumb(thumbId); 
		   	$('image').appear({duration: appearDuration});  
	   });	
	   $('image').writeAttribute('src', ''); 
	   $('image').writeAttribute('src', imageName);
     $('caption').update(imageCaption); 
	   if($('orderlink')!==null){
      $('orderlink').writeAttribute('href', '?ctrl=articledetail&act=order&id='+artId+'&imgid='+imageId); 
     }	   
	   $('image').setStyle({ width: "auto", height:"auto"});   
     } 
	}); 
   	imageListIndex = inIndex;
   	return false;
}


function setThumb(inID) {
	var ts = $$('#thumbs a.selected');
	ts[0].removeClassName('selected');
	$(inID).addClassName('selected');
}

/*
function prevImage() {
	if(imageListIndex == 0) {
		imageListIndex = imageList.length-1; }
	else {
		imageListIndex--; }
	updateBigImage(imageListIndex);
	return false;
}*/

function nextImage() {
	if(imageListIndex == imageList.length-1) {
		imageListIndex = 0; }
	else {
		imageListIndex++; }
	updateProductImage(imageListIndex);
	return false;
}


/*
function hideImage(inEntryID) {
	new Effect.Fade(inEntryID, {duration:.4, queue: { position: 'end', scope: inEntryID, limit:2 }});
}

function showImage(inEntryID) {
	setTimeout(function(){new Effect.Appear(inEntryID, {duration:.6, queue: { position: 'end', scope: inEntryID, limit:1 }})},700);
}
*/

