var killme;
     
function mydelete(link){
    var div = link.parentNode;
    killme = function() {
        Element.remove(div);    
    }
    input = div.getElementsByTagName('input')[0];
    Element.remove(input);    
    Effect.Fade(div);
    setTimeout("killme()",1000);
    return false;
}        

function deleteFromServer(id,link){
	new Ajax.Updater(id,link,{});
	return false;
}

function getFromServer(link){
	new Ajax.Request(link,{});
	return false;
}

function saveToServer(data){
	new Ajax.Request('/admin/server/articles/galery/save',{ parameters: data });
	return false;
}

var RegistrationCheckboxWatcher = Class.create();


RegistrationCheckboxWatcher.prototype = {
   initialize: function() {
     $('user_mailing').onclick = this.click_mailing.bind(this);
     $('user_profiled_mailing').onclick = this.click_mailing.bind(this);
     $('user_discounts').onclick = this.click_mailing.bind(this);
     $('user_no_mailing').onclick = this.click_no_mailing.bind(this);
   },

   click_mailing: function() {
     $('user_no_mailing').checked = false;
     return true;
   },

   click_no_mailing: function() {
      $('user_mailing').checked = false;
      $('user_profiled_mailing').checked = false;
      $('user_discounts').onclick = false;
      return true;
   }
}


var register = {};

var SubmitWatcher = Class.create();

SubmitWatcher.prototype = {

   initialize: function(id, gfxname) {
      this.submit = $(id);
      this.gfxname = gfxname;
      this.submitted = false;
      this.submit.onclick = this.click.bind(this);
      this.submit.onmouseout = this.mout.bind(this);
      this.submit.onmouseover = this.mover.bind(this);
      //za?aduj obrazki wcze?niej
      this.imageb = new Image();
      this.imageb.src = '/images/' + this.gfxname + "_b.gif";
      this.imagec = new Image();
      this.imagec.src = '/images/' + this.gfxname + "_c.gif";
   },

   mover: function() {
      if (!this.submitted) {
        this.submit.src = '/images/' + this.gfxname + "_b.gif";
      }
   },
   mout: function() {
      if (!this.submitted) {
        this.submit.src = '/images/' + this.gfxname + '_a.gif';
      }
   },
   click: function() {
      this.submit.src = '/images/' + this.gfxname + '_c.gif';
      this.submitted = true;
   }
};

var AddToCartWatcher = Class.create();

AddToCartWatcher.prototype = {
   initialize: function(product_id, gfxname) {
      this.imgid = 'd' + product_id;
      this.linkid = 'l' + product_id;
      this.product_id = product_id;
      this.gfxname = gfxname;
      this.element = $(this.imgid);
      if (cart_items.indexOf(product_id) > -1) {
        this.set_in_cart();
      } else {
        this.imageb = new Image();
        this.imageb.src = '/images/' + this.gfxname + '_b.gif';
        this.imagec = new Image();
        this.imagec.src = '/images/' + this.gfxname + '_c.gif';
        this.imaged = new Image();
        this.imaged.src = '/images/' + this.gfxname + '_d.gif';
        this.in_cart = false;
      }

      this.element.onmouseover = this.mouseover.bind(this);
      this.element.onmouseout = this.mouseout.bind(this);
      this.element.onclick = this.click.bind(this);
      register[this.imgid] = this;
   },

   mouseover: function() {
      if (!this.in_cart) {
         this.element.src = '/images/' + this.gfxname + '_b.gif';
      }
   },

   mouseout: function() {
      if (!this.in_cart) {
         this.element.src = '/images/' + this.gfxname + '_a.gif';
      }
   },

   set_in_cart: function() {
      this.element.src = '/images/' + this.gfxname + '_d.gif';
      this.in_cart = true;
   },

   loaded: function() {
      this.set_in_cart();
      if (register['add_to_cart']) {
         register['add_to_cart'].set_in_cart();
      }
      $(this.linkid).href = '/torba/' + this.product_id;
   },

   show_saving: function() {
      this.element.src = '/images/' + this.gfxname + '_c.gif';
      this.in_cart = true;
   },

   click: function(evt) {
      if (this.in_cart) {
         window.location.href = '/torba/' + this.product_id;
      } else {
         this.show_saving();
         if (register['add_to_cart']) {
            register['add_to_cart'].show_saving();
         }
         new Ajax.Request($(this.linkid).href, {asynchronous:true, evalScripts:true, onLoaded: this.loaded.bind(this)}); 
      }
      return false;
   }
}

var AddToCartFormWatcher = Class.create();

AddToCartFormWatcher.prototype = {
   initialize: function(product_id) {
      this.product_id = product_id;
      this.element = $('add_to_cart');
      this.form = $('add_to_cart_form');
      register['add_to_cart'] = this;
      if (cart_items.indexOf(product_id) > -1) {
         this.set_in_cart();
      } else {              
        this.imageb = new Image();
        this.imageb.src = '/images/do_torby_big_b.gif';
        this.imagec = new Image();
        this.imagec.src = '/images/do_torby_big_c.gif';
        this.imaged = new Image();
        this.imaged.src = '/images/do_torby_big_d.gif';
        this.in_cart = false;
      }
      this.element.onmouseover = this.mouseover.bind(this);
      this.element.onmouseout = this.mouseout.bind(this);
      this.form.onsubmit = this.submit.bind(this);
   },

   mouseover: function() {
      if (!this.in_cart) {
         this.element.src = '/images/do_torby_big_b.gif';
      }
   },

   mouseout: function() {
      if (!this.in_cart) {
         this.element.src = '/images/do_torby_big_a.gif';
      }
   },

   set_in_cart: function() {
      this.element.src = '/images/do_torby_big_d.gif';
      this.in_cart = true;
   },

   loaded: function() {
      this.set_in_cart();
      if (register['d' + this.product_id]) {
         register['d' + this.product_id].loaded();
      }
   },

   show_saving: function() {
      this.element.src = '/images/do_torby_big_c.gif';
      this.in_cart = true;
   },

   submit: function(evt) {
      if (this.in_cart) {
         window.location.href = '/torba/' + this.product_id;
      } else {
         this.show_saving();
         if (register['d' + this.product_id]) {
            register['d' + this.product_id].show_saving();
         }
         new Ajax.Request(this.form.action, {asynchronous:true, evalScripts:true, onLoaded: this.loaded.bind(this),
                                             parameters:Form.serialize(this.form)}); 
      }
      return false;
   }
}

function redirect_to_q(qid) {
  return function() {
     window.location.href= '/szukaj/q/' + encodeURIComponent($(qid).value);
     return false;
  }
}

function preloadImage(url) {
  var img = new Image();
  img.src = url;
}

var ratings_txt = ['do bani', 'mo??e by??', 'niez??e', 'ca??kiem dobre', 'wow! boskie!'];

function setRating(id, vote) {
  $(id).value = vote;
  width = parseInt(vote) * 20;
  $('current_rating_' + id).style.width = width + "px";
  $('rating_txt_' + id).innerHTML = "Wybra??e?? ocen?? " + vote + " ze skali 1-5";
  return false;
}

function getCookies() {
  var cookies = {}
  document.cookie.split(";").each(
     function(elem) {
       elems = elem.split("=");
       cookies[elems[0].strip()] = elems[1];
     })
  return cookies;
}

function toggleSheet(id) {
    var ul = $(id);         
    var cat = $(id + '_cat');

    if (ul.style.display == "none") {
       cat.className = 'rolldown';
       //ul.style.display = "block";
       Effect.BlindDown(ul);
       document.cookie = id+'=show; path=/';
    } else {
       cat.className = 'rollup';
       Effect.BlindUp(ul);
       //ul.style.display = "none";
       document.cookie = id+'=hide; path=/';
    }
}

function redirect_to(url) {
  return function() {
    window.location.href = url;
    return false;
  }
}

/*
 * Thickbox - One box to rule them all.
 * Oryginalny kod jest autorstwa: Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 */

showImage = function(a, width) {
  var t = a.title;
  thickboxShowImage(t, a.rel, width);
  a.blur();
  return false;
};

showHTML = function(a, width, height) {
  var t = a.title || a.innerHTML || a.href;
  thickboxShowHtml(t, a.href, width, height);
  a.blur();
  return false;
};

var showMessageTimeoutId; //musimy by?? w stanie wy????czy?? ustawione w przysz??o??ci wy????czenie okienka, je??li 
                          //u??ytkownik postanowi?? wy????czy?? je r??cznie

showMessage = function(msg, width) {
  thickboxRemove(); //na wszelki wypadek
  if (!$('info-box')) {
    new Insertion.Bottom("body", "<div id='info-box' style='display: none;'></div>");
  } else {
    Element.hide('info-box');
  }
  Event.observe("info-box", "click", destroyMessage);
  var arrayPageSize = getPageSize();
  var thickboxTop = getPageScroll() + (arrayPageSize[3] / 6);
  var thickboxLeft = (arrayPageSize[0] - width) / 2;
  Element.setStyle("info-box", { top: thickboxTop + "px",
                                 left: thickboxLeft + "px",
                                 width: width + "px" });
  $('info-box').innerHTML = "<img src='/images/close_dlg.gif' alt='zamknij'/><div id='info-box-msg'>" + msg + "</div>";
  Element.show("info-box");
  new Effect.Highlight("info-box");
  showMessageTimeoutId = window.setTimeout("destroyMessage()", 10000);
}

function destroyMessage() {
  window.clearTimeout(showMessageTimeoutId);
  if ($('info-box')) {
    new Effect.Fade('info-box', { afterFinish: function() {if ($('info-box')) { Element.remove('info-box');}}});
  }
}

function thickboxPrepare() {
  setSelectBoxesVisibility("hidden");
  new Insertion.Bottom("body", "<div id='thickbox_overlay' style='display: none'></div>" + 
                               "<div id='thickbox_window' style='display: none;'></div>");
  setOverlayHeight();
  Event.observe("thickbox_overlay", "click", thickboxRemove);
  new Insertion.Bottom("body", "<div id='thickbox_load' style='display: none'>" + 
                               "<img src='/images/circle_animation.gif' /></div>");
  var arrayPageSize = getPageSize();
  var top = getPageScroll() + ((arrayPageSize[3] - 100) / 2);
  var left = (arrayPageSize[0] - 100) / 2;
  Element.setStyle("thickbox_load", { top: top + "px",
                                      left: left + "px"});
                                                                                                                                                
  Event.observe("thickbox_load", "click", thickboxRemove);
  Element.setStyle("thickbox_window", { width: arrayPageSize[1] + "px" });
  new Effect.Appear('thickbox_overlay', { duration: 0.2, from: 0.0, to: 0.6 });
  new Effect.Appear('thickbox_load', { duration: 0.2, from: 0.0, to: 1.0 });

}

function thickboxShowImage(caption, url, width) {
    thickboxPrepare();
    var imgPreloader = new Image();
    imgPreloader.onload = function() {
      TB_WIDTH = imgPreloader.width + 30;
      TB_HEIGHT = imgPreloader.height + 60;
      new Insertion.Bottom("thickbox_window", "<div id='thickbox_closeWindowImg'>" +
       //"<img src='/images/arrow_prev.gif' alt='poprzedni obrazek' style='margin: 0;border: 0;'/>" +
         "<img src='/images/close.gif' alt='zamknij' style='margin: 0;border: 0;'/>" +
       //"<img src='/images/arrow_next.gif' alt='nast??pny obrazek' style='margin: 0;border: 0;'/>" +
         "</div>" +
         "&nbsp;<img id='thickbox_Image' src='" + url + "' width='" + 
          imgPreloader.width + "' height='" + imgPreloader.height + 
         "' alt='" + caption + "'/>" + "<div id='thickbox_caption'>" + 
          caption + "</div><div id='thickbox_closeWindow'>" + 
         "<a href='#' id='thickbox_closeWindowButton'>zamknij</a></div>"); 

      Event.observe("thickbox_closeWindowButton", "click", thickboxRemove);
      Event.observe("thickbox_closeWindowImg", "click", thickboxRemove);
      Event.observe("thickbox_Image", "click", thickboxRemove);

      thickboxPosition(TB_WIDTH, TB_HEIGHT);
    }
    imgPreloader.src = url;
}

function thickboxShowHtml(url, width, height, text) {
    //thickboxPrepare(); - to wywo??uj?? jeszcze przed new Ajax.Request.
    TB_WIDTH = width + 30;
    TB_HEIGHT = height + 40;
    ajaxContentW = TB_WIDTH - 30;
    ajaxContentH = TB_HEIGHT - 45;
    if (!$('thickbox_ajaxContent')) {
      new Insertion.Bottom("thickbox_window", "<div id='thickbox_closeAjaxWindow'>" +
                        "<a href='#' onclick='thickboxRemove(); return false;' " +
                        "    id='thickbox_closeWindowButton'>zamknij okno</a></div>" + 
                        "<div id='thickbox_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
    }
    $('thickbox_ajaxContent').innerHTML = text;
    thickboxPosition(TB_WIDTH, TB_HEIGHT);
}

//helper functions below
function thickboxRemove() {
    //Je??li znajdujemy si?? jeszcze w fazie ??adowania, to wy????cz wszystko od razu.
    if ($('thickbox_load')) {
       Element.remove('thickbox_load');
       Element.remove("thickbox_window");
       Element.remove("thickbox_overlay");
       setSelectBoxesVisibility("visible");
    } else if ($('thickbox_window')) {
       Effect.Fade("thickbox_window", { duration: 0.8, afterFinish: function() {
        Element.remove("thickbox_window");
        Element.remove("thickbox_overlay");
        setSelectBoxesVisibility("visible");
      }});
    }
    return false;
}

function showArticle(url) {
  if (!$('thickbox_overlay')) {
    thickboxPrepare(); 
  }
  if ($('thickbox_ajaxContent')) {
    $('thickbox_ajaxContent').innerHTML = '<p align="center"><img src="/images/loader.gif" alt="??aduj??..."/></p>';
  }

  new Ajax.Request(url, {asynchronous:true, evalScripts:true}); 
  return false;
}

function thickboxPosition(width, height) {
    var arrayPageSize = getPageSize();
    var thickboxTop = getPageScroll() + (arrayPageSize[3] / 15);
    var thickboxLeft = (arrayPageSize[0] - width) / 2;
    Element.setStyle("thickbox_window", { top: thickboxTop + "px", 
                                          left: thickboxLeft + "px",
                                          width: width + "px",
                                          height: height + "px"});
    Element.remove("thickbox_load");
    //Element.show('thickbox_window');
    Effect.Appear("thickbox_window");
}

function setOverlayHeight() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
                                                                                                                             
  if (window.innerHeight && window.scrollMaxY) {
     yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
     yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
     yScroll = document.body.offsetHeight;
  }
                                                                                                                             
  $('thickbox_overlay').style.height = yScroll +"px";
}


//
// Code from lightbox 2.02
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
  var yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }
  return yScroll;
}

// -----------------------------------------------------------------------------------


// Code by Lokesh Dhakar - http://www.huddletogether.com
// 3/31/06
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------


function setSelectBoxesVisibility(visibility){
  $A(document.getElementsByTagName("select")).each(function(select) {
    select.style.visibility = visibility;
  });
}
                                                                                                                             


