
$.fn.gpos = function()
{
  var prm = $.extend({
   left:0,
   top:0,
   right:0,
   bottom:0,
   width:0,
   height:0
   });

  elem = this.get(0);
  prm.width = elem.offsetWidth;
  prm.height = elem.offsetHeight;
  while (elem)
  {
    prm.left += elem.offsetLeft;
    prm.top += elem.offsetTop;
    elem = elem.offsetParent;
  }
  prm.right = prm.left + prm.width;
  prm.bottom = prm.top + prm.height;
  return prm;
};

$.fn.mysubmenu = function(options)
{
  var cfg = $.extend({
   idpref:'stmenu',
   posflag:'tp',
   currClass:'current'
   }, options);

  $(this).hover(
  function()
  {
      $(this).addClass(cfg.currClass);
      var coords = $(this).gpos();
  	  var prm = {
  		left:coords.left,
  		right:coords.right,
  		top:coords.top,
  		bottom:coords.bottom
  	  }
    var childid = cfg.idpref+$(this).attr("rel");
    if($("#"+childid).length)
    {
  	   if($(this).attr(cfg.posflag) == 'v')
  	    {
  	  	  $("#"+childid).css("top", prm.bottom);
  	      $("#"+childid).css("left",prm.left);
  	    }
  	    else if($(this).attr(cfg.posflag) == 'h')
  	    {
  	      $("#"+childid).css("top",prm.top);
  	      $("#"+childid).css("left",prm.right);
  	    }
  	    var coords = $("#"+childid).gpos();
  	    var prm = {
  		  left:coords.left,
  		  right:coords.right,
  		  top:coords.top,
  	 	  bottom:coords.bottom,
  		  id:childid
  	    }
  	    $("#"+childid).show();
    }
  },
  function()
  {
    var childid = cfg.idpref+$(this).attr("rel");
    if($("#"+childid).length)
    {
  	  $("#"+childid).hide();
    }
    $(this).removeClass(cfg.currClass);
  });
};

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
function resizeBody()
{
  $('#cont').css('height', (getClientHeight()-272)+'px');
  $('#conttd').css('height', (getClientHeight()-272-40)+'px');
}

function getElemPosition(id)
{
  var elem = $('#'+id).get(0);
  var l = 0;
  var t = 0;
  var w = elem.offsetWidth;
  var h = elem.offsetHeihgt;
  while (elem)
  {
    l += elem.offsetLeft;
    t += elem.offsetTop;
    elem = elem.offsetParent;
  }
  return {"l":l,"t":t,"w":w,"h":h};
}

var slblock =
{
  showElems:'',
  contElems:'',
  speed:'',
  interval:'',
  intervalShow:'',
  action:'',
  startInd:'',
  ind:0,
  activeShow:false,
  timeout_id:'',

  apply:function()
  {
	if(this.startInd >= 0)
	{
	  this.ind = this.startInd-1;
	}
	$(this.showElems).bind(this.action, function(){
	  var index = $(slblock.showElems).index(this);
	  if(slblock.ind == index)
	  {        window.location.href=slblock.getLink(slblock.ind);
	  }
	  else
	  {
	    slblock.ind = index;
	    slblock.show(index);
	    slblock.setPause();
	  }
	});
	$(this.contElems).bind("click", function(){
	  var index = $(slblock.contElems).index(this);
	  if(slblock.ind == index)
	  {
        window.location.href=slblock.getLink(slblock.ind);
	  }
	});
  },

  getLink:function(ind)
  {  	return $(this.contElems).eq(ind).children("table:first-child").children("tbody").children("tr:first-child").children("td.main").children("div.more").children("a").attr("href");
  },

  show:function(ind)
  {
	var index = ind;
	$(slblock.contElems).eq(index).slideDown(slblock.speed);
	$(this.contElems).each(function(i){
	  if(i != index)
	  {
	    $(this).slideUp(slblock.speed);
	  }
	});
  },

  slide:function()
  {
  	this.ind++;
  	if(this.ind >= $(this.showElems).size())
  	{  	  this.ind = 0;
  	}
  	this.show(this.ind);
  },

  slideShow:function()
  {
  	if(this.activeShow)
  	{
  	  this.slide();
  	  this.timeout_id = setTimeout(function(){slblock.slideShow();},this.interval);
    }
  },

  setPause:function()
  {
  	this.activeShow = false;
  	clearTimeout(this.timeout_id);
  	this.timeout_id = setTimeout(function(){slblock.activeShow=true;slblock.slideShow()},this.intervalShow);
  },

  define:function(sh,cnt,speed,act,interval,intervalShow,ind)
  {
    this.showElems = sh;
    this.contElems = cnt;
    this.speed = speed;
    this.action = act;
    this.interval = parseInt(interval)*1000;
    this.intervalShow = parseInt(intervalShow)*1000;
    this.startInd = parseInt(ind);
    this.apply();
    this.activeShow = true;
    this.slideShow();
  }
};

var desrot = {
images:[],
imelem:'',
melem:'',
melemid:'',
action:'',
iconCount:'',
timeoutId:'',
activeShow:true,
duration:400,
blankGif:'',

showIcon:function(index, icon)
{
  $(this.imelem).eq(icon).children("img:first").fadeOut(this.duration);
  setTimeout(function(){
    	var im = $(desrot.imelem).eq(icon).children("img:first").get(0);
    	im.src = desrot.images[index].src;
  		$(desrot.imelem).eq(icon).children("img:first").fadeIn(desrot.duration);
  		$(desrot.imelem).eq(icon).children("img:first").attr('rel',index);
  		$(desrot.imelem).eq(icon).children("div:first").attr('rel','');
    	}, this.duration);

},

show:function(index, icon)
{
  index = index % this.images.length;
  if($(this.imelem).eq(icon).html()=='&nbsp;')
  {
    $(this.imelem).eq(icon).html("<img /><div />");
    $(this.imelem).eq(icon).children("img:first").attr('src',this.blankGif);
  }
  setTimeout(function(){desrot.showIcon(index,icon);}, (1000*icon));

},

hide:function(index)
{
  $(this.melem).eq(index).removeClass(this.currClass);
},

slider:function(ind)
{
  if(this.activeShow)
  {
    var k = ind * this.iconCount;
	for(n=0;n<this.iconCount;n++)
	{
	  this.show(k,n);
	  k++;
	}
	ind++;
	if(ind >= this.images.length)
	{
	  ind=0;
	}
  }
  this.timeoutId = setTimeout(function(){desrot.slider(ind);}, 7000);
},

setPause:function()
{
  this.activeShow = false;
  clearTimeout(this.timeout_id);
},

wcontinue:function()
{
  this.activeShow = true;
},

apply:function()
{
	this.slider(0);
	var move = -15;
	var zoom = 1.2;
	$(this.imelem).hover(function() {

		width = $(this).width() * zoom;
		height = $(this).height() * zoom;
	    var index = $(this).children("img:first").attr('rel');
		$(this).find("img").stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});
		if($(this).children("div:first").attr('rel') != index)
		{
		  $(this).children("div:first").html($('#'+desrot.melemid+index).html());
		  $(this).children("div:first").attr('rel',index);
		}
		$(this).children("div:first").stop(false,true).fadeIn(desrot.duration);
		desrot.setPause();
	},
	function() {

		$(this).find("img").stop(false,true).animate({'width':$(this).width(), 'height':$(this).height(), 'top':'0', 'left':'0'}, {duration:100});
        $(this).children("div:first").stop(false,true).fadeOut(desrot.duration);
        desrot.wcontinue();
	});

	$(this.imelem).bind("click", function(){window.location.href = $(this).find('a').attr('href');});
},

init:function(images, imelem, melems, melemid, action, duration, blankGif)
{
  for(i in images)
  {    this.images[i] = new Image();
    this.images[i].src = images[i];
  }
  this.imelem = imelem;
  this.iconCount = $(this.imelem).length;
  this.melem = melems;
  this.melemid = melemid;
  this.action = action;
  this.duration = duration;
  this.blankGif = blankGif;
  this.apply();
}
};


var gal = {
count: 2,
logo:'',
contid:'',
prefix:'',
current:'',
timeout:200,
sh:'',
sw:'',
nextButton:'',
prevButton:'',

showlogos:function(pos)
{
  var n=0;
  var arr = new Array();
  for(i in this.logo)
  {
    arr[n] = i;
    if(i == pos)
    {
      posit = n;
    }
    n++;
  }
  arr.reverse();
  n = 1;
  var flag = false;
  for(i in arr)
  {    if(pos == arr[i])
    {
      flag = true;
    }
    if(n == this.count)
    {
      if(flag)
      {
        pos = arr[i];
        posit = n-1;
      }
      break;
    }
    n++;
  }
  arr.reverse();

  var prev = arr[(posit-1)];
  var next = arr[(posit+1)];
  if(posit != 0)
  {
    $("#"+this.prefix+"prev").html('<img onclick="gal.showlogos(\''+prev+'\');">');
    $("#"+this.prefix+"prev img:first").attr('border','0').get(0).src = this.prevButton.src;
  }
  else
  {
    $("#"+this.prefix+"prev").empty();
  }

  if(!flag)
  {
    $("#"+this.prefix+"next").html('<img onclick="gal.showlogos(\''+next+'\');">');
    $("#"+this.prefix+"next img:first").attr('border','0').get(0).src = this.nextButton.src;
  }
  else
  {
  	$("#"+this.prefix+"next").empty();
  }

  var n=-1;
  for(i in this.logo)
  {
    if(i == pos)
    {      n++;
    }
    if(n >= 0)
    {
      if(typeof this.logo[i] == 'object')
      {
        $("#"+this.prefix+"images").children("img").eq(n).attr('rel',i).attr('id','').get(0).src = this.logo[i].src;
        if(i == this.current)
        {
          $("#"+this.prefix+"images").children("img").eq(n).attr('id',this.prefix+"curr");
        }
      }
      n++;
      if(n == this.count)
      {
        break;
      }
    }
  }
  $("img."+this.prefix+"imlogo").unbind("click");
  $("img."+this.prefix+"imlogo").bind("click",function(){
  	gal.makeAction(this);
  });
},

makeAction:function(elem)
{
  this.showim($(elem).attr('rel'));
  $("#"+this.prefix+"curr").attr('id','');
  $(elem).attr('id',this.prefix+"curr");
},

showim:function(val)
{
  if(this.current != val)
  {
    if(this.current != '')
    {
  	  $("#"+this.prefix+this.contid+this.current).fadeOut(this.timeout);
    }
    setTimeout(function(){$("#"+gal.prefix+gal.contid+val).fadeIn(gal.timeout)},this.timeout);
    this.current = val;
  }
},

initLogos:function()
{  for(i=0;i<this.count;i++)
  {
	$("#"+this.prefix+"images").append('<img />');
    $("#"+this.prefix+"images img:last").attr('class',this.prefix+"imlogo").attr('border','0').attr('height',this.sh).attr('width',this.sw);
  }
},

define:function(count,logo,contid,prefix,timeout,sw,sh,prevButton,nextButton,curr)
{
  this.count = count;
  this.logo = new Array();
  for(i in logo)
  {
    this.logo[i] = new Image();
    this.logo[i].src = logo[i];
  }
  //this.logo = logo;
  this.contid = contid;
  this.prefix = prefix;
  this.timeout = timeout;
  this.sw = sw;
  this.sh = sh;
  this.prevButton = new Image();
  this.prevButton.src = prevButton;
  this.nextButton = new Image();
  this.nextButton.src = nextButton;
  this.initLogos();
  this.showlogos(curr);
  $("img."+this.prefix+"imlogo").each(function(i){if($(this).attr('rel') == curr){gal.makeAction(this);}});
  $("table.gallery_imitem").bind("click", function(){  	var now = new Date();
  	//var newWin = window.open($(this).find('a').attr('href'), now.getTime());
  	window.location.href=$(this).find('a').attr('href');
  });

}

};

$(document).ready(function () {
$(window).bind("resize", function(){resizeBody();});
});
