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");
}


function initGallery(){

  if(e("image-container-div")){  
    var str_main_table = "";
    str_main_table+="<table cellpadding=\"0\" cellspacing=\"0\"><tr>\r\n"
    for (var i = 0 ; i < arr_gallery_images_big.length ; i++){
		str_main_table+="<td style=\"background-image:url(images/gallery-blank-large.gif);\">";
    	// fix to to link to the large file (without the zip. prefix)
    	//str_main_table+="<div class=\"gallery-image-cell\" style=\"background-image:url("+arr_gallery_images_big[i]+");\" valign=\"top\" ><a rel=\"gallery[mysetLge]\" class=\"lightview\" id=\"sliderLge" + i + "\" href=\""+arr_gallery_images_big[i].replace("zip.", "premium.")+"\" target=\"_blank\" ><img src=\"images/x.png\" width=\"475\" height=\"330\" alt=\"\" /></a></div>\r\n";
    	//str_main_table+="</td>";
		
		// fix to to link to the large file (without the zip. prefix)
        var re = /campaigntrack/i;

        if (arr_gallery_images_big[i].match(re)) {
        str_main_table+="<div class=\"gallery-image-cell\" style=\"background-image:url("+arr_gallery_images_big[i]+");\" valign=\"top\" ><a rel=\"gallery[mysetLge]\" class=\"lightview\" id=\"sliderLge" + i + "\" href=\""+arr_gallery_images_big[i].replace("zip.", "premium.")+"\" target=\"_blank\" ><img src=\"images/x.png\" width=\"420\" height=\"330\" alt=\"\" /></a></div>\r\n";
        } else {
       	str_main_table+="<div class=\"gallery-image-cell\" style=\"background-image:url("+arr_gallery_images_big[i]+");\" valign=\"top\" ><a rel=\"gallery[mysetLge]\" class=\"lightview\" id=\"sliderLge" + i + "\" href=\""+arr_gallery_images_big[i].replace("zip.", "")+"\" target=\"_blank\" ><img src=\"images/x.png\" width=\"420\" height=\"330\" alt=\"\" /></a></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"
   
   	str_thumbAreaWidth = "87";
   
   	if (arr_gallery_images_small.length < int_thumbs_visible) {
			str_thumbAreaWidth = "87";
		}
	
		var loopCount = arr_gallery_images_small.length;
		if (loopCount < 5) loopCount = 5;
		for (var i = 0; i < loopCount; i++){
			str_small_table+="<td style=\"background-image:url('images/gallery-blank.gif');\">";
			if (i >= arr_gallery_images_small.length) {
				str_small_table+="<img src=\"images/x.png\" width=\"" + str_thumbAreaWidth + "\" height=\"80\" alt=\"\" />";
			} else {
		    	//str_small_table+="<div style=\"background-image:url('"+arr_gallery_images_small[i]+"');background-position: center; background-repeat: no-repeat;\"><a href=\"#\"  onClick=\"gotoMainGalleryPic("+i+");return false;\" ><img src=\"images/x.png\" width=\"" + str_thumbAreaWidth + "\" height=\"80\"  ></a></div>";
		    	str_small_table+="<div><a href=\"javascript:gotoMainGalleryPic("+i+");\"><img src=\"" + arr_gallery_images_small[i] + "\" width=\"" + str_thumbAreaWidth + "\" height=\"80\" ></a></div>";
			}
			str_small_table+="</td>";
			if (i !=  loopCount - 1){
				str_small_table+="<td width=\"10\" ><img alt=\"\" src=\"images/x.png\" width=\"10\" 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 * -97;
  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 * -475;
  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 )/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);
    //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 = 5;
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();
//===================================

