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 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 img_surround_height= 330;
var str_thumbAreaWidth = 105;
var bln_main_right_enabled = false;
var bln_main_left_enabled = false;
var bln_thumb_right_enabled = false;
var bln_thumb_left_enabled = false;
var bln_enable_caption = true;
var bln_auto_enable_caption = false;
var int_current_caption_length;
//===================================


function initGallery(){
  if(e("image-container-div")){  
    var str_main_table = "";
    str_main_table+='<table cellpadding="0" cellspacing="0" border="0"><tr>\r\n';
    
	for (var i = 0; i < arr_gallery_images_big.length; i++){
    	str_main_table+='<td height="' + img_surround_height + 'px" valign="top" style=\"overflow: hidden;\">';
    	str_main_table+="<div class=\"gallery-image-cell\" align=\"center\"><div class=\"inner-gallery-image-cell\" style=\"background: url(" + arr_gallery_images_big[i] + ") no-repeat center center;\"><a href=\"" + arr_gallery_images_popup[i] + "\" rel=\"lytebox[gallery]\" title=\"" + arr_gallery_captions[i] + "\"><img src=\"images/x.gif\" width=\"465\" height=\"330\" border=\"0\" onmouseover=\"setCaption();\" onmouseout=\"unsetCaption();\"></a></div></div>\r\n";
    	str_main_table+="</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 = "105";
		}*/
	
		var loopCount = arr_gallery_images_small.length;
		if (loopCount < 5) loopCount = 5;
		for (var i = 0; i < loopCount; i++){
			str_small_table+="<td width=\"" + str_thumbAreaWidth + "\">";
			if (i >= arr_gallery_images_small.length) {
				str_small_table+="<img src=\"images/x.gif\" width=\"" + str_thumbAreaWidth + "\" height=\"100\" alt=\"\" />";
			} else {
		    	
		    	str_small_table+="<div style=\"width:" + str_thumbAreaWidth + "px; height: 70px; overflow: hidden; border: 1px solid #333333;\" align=\"center\"><a href=\"javascript:gotoMainGalleryPic("+i+");\"><img src=\"" + arr_gallery_images_small[i] + "\"   border=\"0\" ></a></div>";
			}
			str_small_table+="</td>";
			if (i !=  loopCount - 1){
				str_small_table+='<td width="15" ><div style="width: 15px; height: 1px;"></div></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(){
	//alert(int_current_thumb_pos);
  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 * -120;
  clearInterval(int_timeout_gallery);
  int_timeout_gallery = setInterval("updatePosition()",10);
}

function goNextMainGalleryPic(){
	
  //alert(int_current_main_pos + " " + arr_gallery_images_big.length);
  
  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 * -465;
  clearInterval(int_timeout_gallery);
  int_timeout_gallery = setInterval("updatePosition()",10);

  setCaptionToBlank();
}

function checkMainGalleryButtons(int_ele){
  if (int_ele == arr_gallery_images_big.length){
    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_enable_caption = false;
  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 )/10

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

    e("image-container-div").style.left = 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) < 2){
      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);
	setCaptionToBlank();
	bln_enable_caption = true;
	bln_auto_enable_caption = true;
    setCaptionToCurrentImage();
	setCaption();
  }

}
function setCaptionToCurrentImage(){
  if (e("gallery-caption")){
    var str = arr_gallery_captions[int_current_main_pos];
	
	int_current_caption_length = str.length;
	
    if (str.length < 1 || str == "undefined"){
      str = "";
    }
	e("gallery-caption").innerHTML = '<div style="padding: 10px;">' + str + '</div>';
  }
}

function setCaptionToBlank(){
  if (e("gallery-caption")){
	e("gallery-caption").style.visibility='hidden';
    e("gallery-caption").innerHTML = "";
  }
}

function setCaption() {
	if ((bln_auto_enable_caption) && (int_current_caption_length > 0)) {
		e('gallery-caption').style.visibility='visible';
		bln_auto_enable_caption = false;
	}
	else {
		if ((bln_enable_caption) && (int_current_caption_length > 0)) {
			if (e('gallery-caption').innerHTML.length > 1) {
				e('gallery-caption').style.visibility='visible';
			}
		}
	}
}

function unsetCaption() {
	if (e('gallery-caption').style.visibility == 'visible') {
		e('gallery-caption').style.visibility='hidden';
	}	
}
