jQuery.fn.aPosition = function(entity) {
  thisLeft = this.offset().left;
  thisTop = this.offset().top;  
  if(entity == undefined)
    thisParent = this.parent();
  else
    thisParent = $(entity);
  if(entity != document) {
    parentLeft = thisParent.offset().left;
    parentTop = thisParent.offset().top;
  } else {
    parentLeft = 0;
    parentTop = 0;
  }//if-else
  return {
    left: thisLeft-parentLeft, 
    top: thisTop-parentTop
  }
}//jquery

$(function(){
  setCatalogIconClick();
  setIconsListing();
  setIconsListingDS(); 
  setCatalogSearchSettingsTitleClick(); 
  setCatalogSearchParamNamesClick();
  setCatalogSortingSettingsTitleClick();
  setCatalogSortingsListClick();
});

function setCatalogSortingsListClick() {
  $("#catalog-sorting-settings select").change(function(){
    location_href = location.href.replace(/&sorting=[^&]*/g,"");
    arr=location_href.split("?");
    question_sign = arr[1]==undefined ? "?" : "";
    if(this.value==0)
      window.open(location_href,"_self");
    else {
      location_href = location.href.replace(/&sorting=[^&]*/g,"");
      window.open(location_href+question_sign+"&sorting="+this.value,"_self");
    }//if-else
  });
}//function

function setCatalogSearchParamNamesClick() {
  
  $("#catalog-search-settings input[type=checkbox] + span").click(function(){
       
    checkbox = $("input[type=checkbox][name="+$(this).attr("id")+"]");
    if(checkbox.attr("checked"))
      checkbox.attr("checked",false);
    else
      checkbox.attr("checked",true);      
    
  });
  
}//function

function setCatalogSearchSettingsTitleClick() {
  
  $("#catalog-search-settings-title").click(function(){
    
    if($("#catalog-search-settings").css("display") == "none") {
      container_top = $("#catalog-search").offset().top;
      container_left = $("#catalog-search").offset().left;
      parent_top = $("#catalog-search").offsetParent().offset().top;
      parent_left = $("#catalog-search").offsetParent().offset().left;
      $("#catalog-search").css("top",container_top-parent_top);
      $("#catalog-search").css("left",container_left-parent_left);
      $("#catalog-search").css("position","absolute");
      $("#catalog-sorting-settings").slideUp("fast");
      $("#catalog-sorting-settings-title span").html("+");
      $("#catalog-search-settings").slideDown();
      $("#catalog-search-settings-title span").html("−");
    
    } else {
      
      $("#catalog-search-settings").slideUp();
      $("#catalog-search-settings-title span").html("+");
    
    }//if-else
    
  });
  
}//function

function setCatalogSortingSettingsTitleClick() {
  
  $("#catalog-sorting-settings-title").click(function(){
    
    if($("#catalog-sorting-settings").css("display") == "none") {
      container_top = $("#catalog-search").offset().top;
      container_left = $("#catalog-search").offset().left;
      parent_top = $("#catalog-search").offsetParent().offset().top;
      parent_left = $("#catalog-search").offsetParent().offset().left;
      $("#catalog-search").css("top",container_top-parent_top);
      $("#catalog-search").css("left",container_left-parent_left);
      $("#catalog-search").css("position","absolute");
      $("#catalog-search-settings").slideUp("fast");
      $("#catalog-search-settings-title span").html("+");
      $("#catalog-sorting-settings").slideDown();
      $("#catalog-sorting-settings-title span").html("−");
    
    } else {
      
      $("#catalog-sorting-settings").slideUp();
      $("#catalog-sorting-settings-title span").html("+");
    
    }//if-else
    
  });
  
}//function

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}//function

function d(id)  {
  return document.getElementById(id);
}//function

function containerHover(container) {

  if(container.hover=="over") {
    container.element.style.background = container.borderColor;
    d(container.name+"_top2").style.background = container.borderColor;
    d(container.name+"_bottom2").style.background = container.borderColor;
    if((!container.widthBorders && container.gradient) || !container.gradient) {
      d(container.name+"_top").style.borderColor = container.borderColor;
      d(container.name+"_bottom").style.borderColor = container.borderColor;   
      d(container.name+"_top2").style.borderColor = container.borderColor;
      d(container.name+"_bottom2").style.borderColor = container.borderColor;    
    }//if
    if(container.isWhite)
      d(container.name+"_content").style.color = "#fff";
  } else {
    if(container.gradient) {
      container.element.style.backgroundImage=container.backgroundImage;
      d(container.name+"_top2").style.background = container.topColor;
      d(container.name+"_bottom2").style.background = container.bottomColor;
    } else {
      container.element.style.background = container.bodyColor;
      d(container.name+"_top2").style.background = container.bodyColor;
      d(container.name+"_bottom2").style.background = container.bodyColor;   
    }//if-else
    if(!container.widthBorders || !container.gradient) {
      d(container.name+"_top").style.borderColor = container.topColor;
      d(container.name+"_bottom").style.borderColor = container.bottomColor;  
      d(container.name+"_top2").style.borderColor = container.topColor;
      d(container.name+"_bottom2").style.borderColor = container.bottomColor;   
    }//if
    if(container.isWhite)
      d(container.name+"_content").style.color = container.borderColor;    
  }//if-else
  
}//function

var SCFO;
var TFO = new Array();
var CFO = new Array();
function disableSelection(target){
  if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return false}
  else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        {target.style.MozUserSelect="none";}
  else {//All other route (ie: Opera)
    for(i=0; ; i++) {
      if(TFO[i] == target)  {
        CFO[i] = target.onmousedown;
        break;
      }//if
      if(TFO[i] == undefined)  {
        CFO[i] = target.onmousedown;
        TFO[i] = target;
        break;
      }//for
    }//for
    target.onmousedown=function(){return false} 
  }//if-else
}//function

function enableSelection(target){
  if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return true}
  else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        target.style.MozUserSelect=""
  else {//All other route (ie: Opera)
        for(i=0; ; i++) {
      if(TFO[i] == target)  {
        target.onmousedown = CFO[i];
        break;
      }//if
      if(TFO[i] == undefined) {
        break;
      }//if      
    }//for
  }//if-else
}//function

function startDisableSelection() {
  disableSelection(document.body);
}//function

function stopDisableSelection() {
  enableSelection(document.body);
}//function

function setCatalogIconClick() {
  $(".catalog-icons div").click(function(){ 
    param = $(this).attr("class").replace("catalog-icon-","");
    main_picture_a_href = $("#main-picture-"+param).css("background-image",$(this).css("background-image").replace("/icon/","/preview/"));
    $("#main-picture-"+param).css("background-image",$(this).css("background-image").replace("/icon/","/preview/"));
    main_picture_a_href = $("#main-picture-"+param).css("background-image").replace("url(","").replace(")","").replace("'","").replace("/preview/","/original/");
    $("#main-picture-a-"+param).attr("href",main_picture_a_href);
  });
}//function

function setIconsListingDS() {
  $("[class*=catalog-icons-list-]").each(function(){
    disableSelection($(this)[0]);
  });
}//function

function setIconsListing() {
  $("[class*=catalog-icons-list-]").each(function(){
    $(this).unbind("click");
  });
  $("[class*=catalog-icons-list-]").click(function(){
    where = $(this).attr("class").replace("catalog-icons-list-","");
    param = $(this).attr("id").replace("catalog-icons-list-"+where+"-","");
    arr = param.split("-");
    
    main_picture_bi = $("#main-picture-"+param).css( "background-image" );
    main_picture_bi_path = main_picture_bi.replace("url(","").replace(")","").replace("'","");
    main_picture_bi_path_arr = main_picture_bi_path.split("/");
    main_picture_file = main_picture_bi_path_arr[main_picture_bi_path_arr.length-1];    
    last_file = "";
    $("#catalog-icons-"+param+" div").each(function(i){
      if(where == "right") {
        if(i==0)
          $(this).remove();
        if(i==2)
          last_file = $(this).attr("id").replace("catalog-icon-","").replace(param+"-","").replace("_",".");      
      }//if
      if(where == "left") {
        if(i==0)
          first_file = $(this).attr("id").replace("catalog-icon-","").replace(param+"-","").replace("_","."); 
        if(i==2)
          $(this).remove();
      }//if
    });
    icons_files_list = $("#icons-files-list-"+param).html();
    icons_files_arr = icons_files_list.split("|");

    for(i=0;i<icons_files_arr.length;i++) {
      if(where == "right") {
        if(icons_files_arr[i] == last_file) {
          if(i == icons_files_arr.length-1)
            next_file = icons_files_arr[0];              
          else
            next_file = icons_files_arr[i+1];
        }//if
        if(icons_files_arr[i] == main_picture_file) {
          if(i == icons_files_arr.length-1)
            new_main_picture_file = icons_files_arr[0];              
          else
            new_main_picture_file = icons_files_arr[i+1];       
        }//if
      }//if
      if(where == "left") {
        if(icons_files_arr[i] == first_file) {
          if(i == 0)
            prev_file = icons_files_arr[icons_files_arr.length-1];
           else
            prev_file = icons_files_arr[i-1];
        }//if
        if(icons_files_arr[i] == main_picture_file) {
          if(i == 0)
            new_main_picture_file = icons_files_arr[icons_files_arr.length-1];
           else
            new_main_picture_file = icons_files_arr[i-1];        
        }//if
      }//if
    }//for
    if(where == "right") {
      next_img_src = "userfiles/catalog/"+arr[0]+"/"+arr[1]+"/icon/"+next_file;
      next_img = "<div class='catalog-icon-"+param+"' id='catalog-icon-"+param+"-"+next_file+"' style='margin:0px 5px;cursor:pointer;cursor:hand;background-image:url("+next_img_src+");background-position:center center;background-repeat:no-repeat;float:left;width:30px;height:30px;'></div>";
      $("#catalog-icons-"+param).append(next_img);
    }//if
    if(where == "left") {
      prev_img_src = "userfiles/catalog/"+arr[0]+"/"+arr[1]+"/icon/"+prev_file;
      prev_img = "<div class='catalog-icon-"+param+"' id='catalog-icon-"+param+"-"+prev_file+"' style='margin:0px 5px;cursor:pointer;cursor:hand;background-image:url("+prev_img_src+");background-position:center center;background-repeat:no-repeat;float:left;width:30px;height:30px;'></div>";
      $("#catalog-icons-"+param).prepend(prev_img);      
    }//if
    $("#main-picture-"+param).css("background-image","url("+"userfiles/catalog/"+arr[0]+"/"+arr[1]+"/preview/"+new_main_picture_file+")");
    main_picture_a_href = $("#main-picture-"+param).css("background-image").replace("url(","").replace(")","").replace("'","").replace("/preview/","/original/");
    $("#main-picture-a-"+param).attr("href",main_picture_a_href);    
    setCatalogIconClick();
  });
}//function

