function e (itemid) {
	return document.getElementById(itemid);
}

function onRollOver(obj_img){
	obj_img.src = obj_img.src.split("-off").join("-on");
}

function onRollOut(obj_img){
	obj_img.src = obj_img.src.split("-on").join("-off");
}

var str_thumbAreaWidth = int_thumb_image_width;


function initGallery(){  
  if(e("image-container-div")){
    var str_main_table = "";
    str_main_table+="<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr>"
    for (var i = 0 ; i < arr_gallery_images_big.length ; i++){
		str_main_table+="<td style=\"background-image:url(images/gallery-blank-large.gif);\">";
			str_main_table+="<div class=\"gallery-image-cell\" style=\"background-image:url("+arr_gallery_images_big[i]+");\" valign=\"top\">";
				str_main_table+="<a href=\""+arr_gallery_images_big[i]+"\" target=\"_blank\" >";
					str_main_table+="<img src=\""+href+"images/x.gif\" width=\""+int_large_image_width+"\" height=\""+int_large_image_height+"\" >";
		str_main_table+="</a></div></td>";
    }
    str_main_table+="</tr></table>";
    e("image-container-div").innerHTML = str_main_table;
  }
  if(e("sm-image-container-div")){
    var str_small_table = "";
    str_small_table+="<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr>\r\n"
   
   	
   
   	if (arr_gallery_images_small.length < int_thumbs_visible) {
			str_thumbAreaWidth = int_large_image_width;
		}
	
		var loopCount = arr_gallery_images_small.length;
		if (loopCount < 4) loopCount = 4;
		for (var i = 0; i < loopCount; i++){
			str_small_table+="<td style=\"background-image:url(images/gallery-blank.gif); width: "+int_thumb_image_width+"px;\">";
			if (i >= arr_gallery_images_small.length) {
				str_small_table+="<img src=\""+href+"images/x.gif\" width=\""+int_thumb_image_width+"\" height=\""+int_thumb_image_height+"\"  >";
			} else {
				str_small_table+="<div style=\"background-image:url("+arr_gallery_images_small[i]+"); background-color: #eeeeee; background-position: center; background-repeat: no-repeat;\">";
					str_small_table+="<a href=\"#\"  onClick=\"gotoMainGalleryPic("+i+");return false;\" >";
						str_small_table+="<img src=\""+href+"images/x.gif\" width=\""+int_thumb_image_width+"\" height=\""+int_thumb_image_height+"\"  >";
				str_small_table+="</a></div>";
			}
			str_small_table+="</td>";
			if (i !=  loopCount - 1){
				str_small_table+="<td width=\"3\" ><img src=\""+href+"images/x.gif\" width=\"3\" height=\"1\" ></td>\r\n";
			}
		}
		str_small_table+="</tr></table>";
		e("sm-image-container-div").innerHTML = str_small_table;
  }


  checkMainGalleryButtons(0);
  checkThumbGalleryButtons(0);
  //setCaptionToCurrentImage();

}


function sm_goNextMainGalleryPic(){
  if (int_current_thumb_pos < arr_gallery_images_small.length - int_thumbs_visible ){
    sm_gotoMainGalleryPic(int_current_thumb_pos + 1);
  }
}

function sm_goPreviousMainGalleryPic(){
  if (int_current_thumb_pos >= 1){
    sm_gotoMainGalleryPic(int_current_thumb_pos - 1);
  }
}

function sm_gotoMainGalleryPic(int_ele){

  int_current_thumb_pos = int_ele;

  checkThumbGalleryButtons(int_current_thumb_pos);
  int_thumb_target_x = int_ele * -int_thumb_image_width;
  clearInterval(int_timeout_gallery);
  int_timeout_gallery = setInterval("updatePosition()",10);
}

function goNextMainGalleryPic(){
  if (int_current_main_pos < arr_gallery_images_big.length - 1){
    gotoMainGalleryPic(int_current_main_pos + 1);
  }
  //gotoMainGalleryPic(int_current_main_pos);
}

function goPreviousMainGalleryPic(){
  if (int_current_main_pos >= 1){
    gotoMainGalleryPic(int_current_main_pos - 1);
  }
}


function gotoMainGalleryPic(int_ele){
  if (e("gal-thumb-mask-" + int_current_main_pos)){
    e("gal-thumb-mask-" + int_current_main_pos).className="not-active";
  }
  int_current_main_pos = int_ele;
  if (e("gal-thumb-mask-" + int_current_main_pos)){
    e("gal-thumb-mask-" + int_current_main_pos).className="active";
  }

  checkMainGalleryButtons(int_current_main_pos);

  int_main_target_x = int_ele * - int_main_gallery_width;
  clearInterval(int_timeout_gallery);
  int_timeout_gallery = setInterval("updatePosition()",10);

  //setCaptionToBlank();
}

function checkMainGalleryButtons(int_ele){
  if (int_ele == arr_gallery_images_big.length - 1){
    bln_main_right_enabled = false;
    if (e("btn_main_right")){
      e("btn_main_right").className="disabled-btn";
    }
  } else {
    bln_main_right_enabled = true
    if (e("btn_main_right")){
      e("btn_main_right").className="enabled-btn";
    }
  }

  if (int_ele == 0){
    bln_main_left_enabled = false;
    if (e("btn_main_left")){
      e("btn_main_left").className="disabled-btn";
    }
  } else {
    bln_main_left_enabled = true
    if (e("btn_main_left")){
      e("btn_main_left").className="enabled-btn";
    }
  }
}

function checkThumbGalleryButtons(int_ele){
  if (arr_gallery_images_small.length - int_thumbs_visible <= 0 || int_ele == arr_gallery_images_small.length - int_thumbs_visible ){

    bln_thumb_right_enabled = false;
    if (e("btn_thumb_right")){
      e("btn_thumb_right").className="disabled-btn";
    }
  } else {
    bln_thumb_right_enabled = true
    if (e("btn_thumb_right")){
      e("btn_thumb_right").className="enabled-btn";
    }
  }

  if (int_ele == 0){
    bln_thumb_left_enabled = false;
    if (e("btn_thumb_left")){
      e("btn_thumb_left").className="disabled-btn";
    }
  } else {
    bln_thumb_left_enabled = true
    if (e("btn_thumb_left")){
      e("btn_thumb_left").className="enabled-btn";
    }
  }
}


function updatePosition(){

  var bln_update_main = false;
  if (int_main_target_x != int_current_main_pos_x){

    bln_update_main = true;

    int_current_main_pos_x+= (int_main_target_x - int_current_main_pos_x )/5

    if( Math.abs(int_current_main_pos_x - int_main_target_x) < 10  ){
      int_current_main_pos_x = Math.round(int_main_target_x);
    }

    e("image-container-div").style.marginLeft = String(int_current_main_pos_x) + "px";

  }

  var bln_update_thumb = false;


  if (int_thumb_target_x != int_current_thumb_pos_x){

    bln_update_thumb = true;

    int_current_thumb_pos_x+= (int_thumb_target_x - int_current_thumb_pos_x )/5;

    if (Math.abs(int_current_thumb_pos_x - int_thumb_target_x) < 5){
      int_current_thumb_pos_x = Math.round(int_thumb_target_x);
    }

    e("sm-image-container-div").style.marginLeft = String(int_current_thumb_pos_x) + "px";

  }

  if (!bln_update_main && !bln_update_thumb){
    clearInterval(int_timeout_gallery);
    //setCaptionToCurrentImage();
  }

}
/*function setCaptionToCurrentImage(){

  if (e("gallery-caption")){
    var str = arr_gallery_captions[int_current_main_pos];
    if (str.length < 1 || str == "undefined"){
      str = "&nbsp;"
    }
    e("gallery-caption").innerHTML = str;
  }
}

function setCaptionToBlank(){
  if (e("gallery-caption")){
    e("gallery-caption").innerHTML = "&nbsp;"
  }
}*/

//===================================
var int_current_main_pos = 0;
var int_current_thumb_pos = 0;
var int_current_main_pos_x = 0;
var int_current_thumb_pos_x = 0;
var int_main_target_x = 0;
var int_thumb_target_x = 0;
var int_thumbs_visible = 4;
var int_timeout_gallery = -1;
var bln_main_right_enabled = false;
var bln_main_left_enabled = false;
var bln_thumb_right_enabled = false;
var bln_thumb_left_enabled = false;
// ----------------------------------
initGallery();
//===================================

