
var autoID      = 0 ;
var hideTimeout = 800 ;
var showTimeout = 800 ;
var offsetLeft  = -55 ;
var offsetTop   = -55 ;
var targetPopup = 'content_popup' ;
var targetFix   = 'content_popup_iefix';
var d = document;
var ieFix_enabled = d.all && !window.opera ? true : false ;
var activate_popup=function(element) {
  set_enabled( element, 'enabled') ;
  id = $(element).id || set_new_id(element);
  var timer = setTimeout( "show_popup('" + id +  "')" , showTimeout ) ;
};

var disable_popup = function(element){
  set_enabled( element, 'disabled') ;
  id = element.id || set_new_id(element);  
  setTimeout( "hide_popup('" + id +  "')" , hideTimeout ) ;
};
 
var set_enabled=function( element, value ){
  if ( $(element).attributes.popup_enabled == undefined ){
    $(element).setAttribute('popup_enabled', value);
  } else {
    $(element).attributes.popup_enabled.nodeValue = value;
  }
};

var set_new_id=function(element){
  if ( element.attributes.id == undefined || element.attributes.id.nodeValue == ""){
    element.setAttribute('id', 'autoID'+ (autoID++) );
  }
  return element.attributes.id.nodeValue ;
};

var get_enabled=function(element){
  if ($(element) == null ) return false ;
  if ( $(element).attributes.popup_enabled == undefined ){
    return false;
  } else {
    return $(element).attributes.popup_enabled.nodeValue;
  }
};

var get_item_id=function(element){
  if ( $(element).attributes.itemid == undefined ){
    return false;
  } else {
    return $(element).attributes.itemid.nodeValue;
  }
};

var show_popup=function(element){
  element = $(element)
  if ( get_enabled(element) == "enabled" ){
    pop_constructor(element);
  }
};

var hide_popup=function(element){
  if ($(element) == null ) return ;
  element = $(element)
  if ( get_enabled(element) != "enabled" ){
    Element.hide(targetPopup);
    if( $(targetFix) != null )Element.hide(targetFix);
  }
};

var pop_constructor=function(element){
  $(targetPopup).onmouseover = function(){ set_enabled( element.id, 'enabled'); return false;}  
  $(targetPopup).onmouseout  = function(){ disable_popup(element); return false;}
  popupCloseFunction = function(){ set_enabled( element, 'disabled'); Element.hide(targetPopup); return false;};
  $('popup_closeButton').onclick = popupCloseFunction;

  var id = get_item_id(element) || false ;
  if ( id == false ) return false ;
  content = Items[id];
  var height = Element.getDimensions(element).height;
  var width  = Element.getDimensions(element).width;    
  var left = Position.cumulativeOffset(element)[0];
  var top  = Position.cumulativeOffset(element)[1];
  
  var popup_height = Element.getDimensions(targetPopup).height;
  // Base popup position
  $(targetPopup).style.top = top + height/2 + offsetTop + "px";
  $(targetPopup).style.left = left + width/2 + offsetLeft + "px" ;

  if ( Logged == true ){
    Element.show('popup_item_favorites');
    if (content.is_favorite_product == true ) {
      Element.update('popup_link_to_favorites', 'Remove from favorites');
      $('popup_link_to_favorites').href = '/mysilk/favorites/remove/' + id ;       
    }else{
      Element.update('popup_link_to_favorites', 'Save to Favorites');
      $('popup_link_to_favorites').href = '/mysilk/favorites/add/' + id ; 
    }
  }else{
    Element.hide('popup_item_favorites');  
  }
  content.is_in_wacthlist = false ;
  links = $$('#watchlist a');
  links.each( function(link){
    if (link.href.indexOf('/store/item/' + id)>0 ) content.is_in_wacthlist = true ;
  })
  
  if ( content.is_in_wacthlist == true ){
    Element.hide('popup_item_watchlist');  
  }else{
    Element.show('popup_item_watchlist');    
    $('popup_link_to_watchlist').onclick = function(){ 
        new Ajax.Request('/store/ajax_add_to_watchlist/' + id, {asynchronous:true, evalScripts:true, onComplete: Element.hide('popup_item_watchlist') }); return false;
    }
  }

  if ( content.power_buy == true ){
    Element.show('popup_item_power_buy');  
  }else{
    Element.hide('popup_item_power_buy');    
  }
  
  if (content.multipricing){
    Element.show('popup_item_multipricing'); 
  } else {
    Element.hide('popup_item_multipricing');   
  } 

	Element.show('popup_view_feedback');
  Element.update('popup_item_name', content.name);
  Element.update('popup_item_price', content.price);
  Element.update('popup_item_store_name', content.storename);
  //$('popup_item_image').src = "/images/themes/blue/default/product-no_130x100.png";
  //$('popup_item_image').src = content.image;
  // REMOVE IMG
  Element.remove('popup_item_image');
  //
  Element.update('popup_item_description', content.description);
  $('item_instock_quantity').value = content.instock_quantity;
  $('buynow').value = 0 ;  
  $('add_to_cart').action = '/cart/add/' + id ; 
  $('popup_link_to_rate_product').href = '/store/item/' + id ;  
  $('popup_link_to_details').href = '/store/item/' + id ;   
  $('image_link_to_details').href = '/store/item/' + id ;
  

  //ADD IMG
  imgObj = $('image_link_to_details').appendChild(d.createElement("img"));
	imgObj.id = "popup_item_image";
  $('popup_item_image').alt = content.name;
  $('popup_item_image').src = content.image;
  Element.addClassName(imgObj, "wide");
  //ADD VIDEO button
  if (content.video != 0){
    $('popup_item_video_button').firstChild.onclick = function(){ button_off(this); popupCloseFunction(); new Ajax.Request('/store/ajax_pop_video_preview/' + content.video, {asynchronous:true, evalScripts:true}); return false;}
    Element.show('popup_item_video_button');
  }else{
    Element.hide('popup_item_video_button');
  }
  //
  
  $('popup_link_to_view_feedback').href='/store/feedback/'+content.storeid;
  $('link_to_store').href = '/store/index/' + content.storeid ;  
    
  $('popup_link_to_buynow').onclick = function(){ new Ajax.Request('/cart/ajax_check_item/' + id + '?buynow=1&qty='+document.getElementById('quantity').value, {asynchronous:true, evalScripts:true}); return false;}
  $('popup_link_to_addcart').onclick = function(){ new Ajax.Request('/cart/ajax_check_item/' + id + '?buynow=0&qty='+document.getElementById('quantity').value, {asynchronous:true, evalScripts:true}); return false;}
  $('quantity').value = 1 ;
  $('add_to_cart').onsubmit = function(){ if ($('buynow').value == 0 ) {
      new Ajax.Request('/cart/ajax_check_item/' + id + '?buynow=1&qty='+document.getElementById('quantity').value, {asynchronous:true, evalScripts:true}); 
    }else{
      new Ajax.Request('/cart/ajax_check_item/' + id + '?buynow=0&qty='+document.getElementById('quantity').value, {asynchronous:true, evalScripts:true});
    }
    return false;
  }

  if ( content.expired == true ) {
    $('popup_link_to_buynow').onclick = function(){ expired_action(content.name); return false ;}
    $('popup_link_to_addcart').onclick = function(){ expired_action(content.name); return false ;}
    $('add_to_cart').onsubmit = function(){ expired_action(content.name); return false ;} 
    if ( content.is_in_wacthlist == false ){ $('popup_link_to_watchlist').onclick = function(){ expired_action(content.name); return false ;} }
    $('popup_link_to_details').onclick = function(){ expired_action(content.name);}
  }
  if( ieFix_enabled && $(targetFix) == null ){
    new Insertion.After($(targetPopup), 
       '<iframe id="' + targetFix + '" '+
       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0)" ' +
       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
  }
  if( $(targetFix) != null ) $(targetFix).style.zIndex = 1;
  $(targetPopup).style.zIndex = 2;
  Element.show(targetPopup);
  if( $(targetFix) != null ) Element.show(targetFix);
  shiftElement(targetPopup);
  if( $(targetFix) != null ) Element.setStyle(targetFix, {width:"1px", height:"1px"});
  if(d.all && !window.opera && ieFix_enabled){
    //MSIE
    Position.clone(targetPopup, targetFix);
  }
};

var expired_action=function(title){
  alert("Listing of \"" + title+ "\" has expired")
};

var link_to_item = function(id){
  new Ajax.Request('/store/item/' + id + '?ajax=true', {
    asynchronous:true, evalScripts:true, onComplete:function(request){StateManager.setState('store/item/' + id);
      $('worker').style.visibility = 'hidden';}, onLoading:function(request){$('worker').style.visibility = 'visible';
    }}); 
}

/* Slider & Popups for AJAX-pagination */
// Get Items through codestring in container
function getPopItems(id){
  var defaultContainerId = "item_scroller_container";
  var depotElementClass = "js_pop_code";
  var id = id || defaultContainerId;
  var code = $$("#" + id + " ." + depotElementClass)[0].innerHTML;
  eval(code);
}