jQuery.fn.dshadow = function ( w, h){

  var divs = 5; 
  for (var i=0, j=divs ; i < divs; i++, j--) {
    this.append($("<div></div>").css({'background':'#6d6d6d' , 'width':w, 'height':h,
    'position':'absolute', 'top':i+'px', 'left':i+'px', 'z-index':j, 
    'opacity':j/10, "-ms-filter":'"progid:DXImageTransform.Microsoft.Alpha(Opacity='+ j*10 +')"',
    'filter': 'alpha(opacity='+j*10+')'
    }));
  };
  return this;
};


function preLoadImages(list){
    $(list).each(function(idx, url){
      var img= new Image();
      ul.append('<li id="'+ idx +'"></li>');
      $(img).load(function(){
        ul.children('li#'+idx).empty().append($('<img id="'+ idx +'" src="'+ url + '">').css({'position':'absolute', 
                   'top':'-1px', 'left': '-1px','z-index':'95'}));
      }).attr('src', list[idx]);
    });
  
    //büyükler
    $(list).each(function(idx, url){
      img = new Image();
      $(img).attr('src', url);
    });
}

function findPos(obj) {   
  var curleft = curtop = 0; 
  if (obj.offsetParent) { 
    do {      
      curleft += obj.offsetLeft;      
      curtop += obj.offsetTop; 
    } while (obj = obj.offsetParent); 
  return [curleft,curtop]; } 
};  


function showImage(idx) {
  // görsel detaylarını üst alanda güncelliyoruz
  $('div#en').html('');
  $('div#tr').html('');
  $('div#imgfield').html('').unbind(); // delete event handlers - zoom stuff
  
  // sıradaki görsel 

  var item = images[idx];
  $('div#en').html("<span>"+ item.title_en +"</span> <br>" + 
      "<span>" + item.details_en +"</span>");

  $('div#tr').html("<span >"+ item.title_tr +"</span> <br>" + 
      "<span>" + item.details_tr +"</span>");

  if (item.sold){
    $('div.sold').css({'display':'block'});
    $('div.buy').css({'display':'none'});
  } else {
    $('div.sold').css({'display':'none'});
    $('div.buy').css({'display':'block'});
    
  }
  $('div.prstr span').text(item.price);
  $('div.buy a').attr({'href':'/buy/'+ item.slug +'/'})

  var posx = 600 - parseInt(item.width)/2;
  var posy = 300 - parseInt(item.height)/2;

  //temp 
  //if (parseInt(item.height) == 0) {
     posy = 170;
  //};

  $('div#imgfield').css({'width': item.width, 'height':item.height, 
        'left': posx, 'top':posy}).append(ul.children('#'+idx).children('img').clone());
  $('div#imgfield').dshadow(item.width, item.height);
  var wasrunning = 0;
  $('div#imgfield').hover(function(){
         if (!stopped){
            clearInterval(cycleInterval);
         }
        }, 
        function(){
          if (!stopped){
            cycleInterval = setInterval(function(){displayImage('next')}, 8000);
          }
        }
 );
  
  
  $('div#imgfield img').tooltip({  
      delay: 0, 
      showURL: false, 
      track: true,
      width: parseInt(item.bigwidth),
      height: parseInt(item.bigheight),

      bodyHandler: function() { 
        return $("<img/>").attr("src", item.bigurl); 
      } 
  });
};

function displayImage(id) {
  
  if (id == 'next'){
    current = (current >= images.length - 1) ? 0: current + 1;
    showImage(current);

  } else if (id == 'previous') {
    current = (current <= 0) ? images.length - 1: current - 1;
    showImage(current);

  } else { // stop 
    stopped = 1; 
  };
}

