function forEach(object, callback){
	var i, l = object.length, x;
	if(l === undefined){
		for(x in object){
			if(callback.call(object[x], x, object[x]) === false){
				break;
			}
		}
	}
	else{
		for(i = 0, x = object[0]; i < l && callback.call(x, i, x) !== false; x = object[++i]){
		}
	}
}
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

/**
 * kolorowanie wierszy <ul>
 */
var istStripeUl = {
	init: function() {
		var parent = document.getElementsByTagName("ul");
		for(var i=0, j=parent.length; i<j; i++) {
			if (parent[i].className.match("stripeMe")) {
				var el = parent[i].getElementsByTagName("li");
				for(var k=0, l=el.length; k<l; k++) {
					if(k%2) addClass(el[k],'even');
					else addClass(el[k],'odd');
				}
			}
		}
	}
}

/**
 * galeria w produkcie
 */
var istGallery = {
	thumbs: 0,
	init: function(x){
		istGallery.thumbs = x;
    x = document.getElementById('iStoreProductGalleryThumbs');
    if(x){  
    	document.getElementById('iStoreProductGalleryControls').style.display = 'block';
    	removeClass(document.getElementById('iStoreProductGalleryThumbs'), 'gallScroll');
			if(document.getElementById('iStoreProductGalleryPhoto')){
				forEach(x.getElementsByTagName('a'), function(){
					this.target = '';
					this.onclick = function(){
					  
					  hmm = document.getElementById('iStoreProductGalleryPhoto').getElementsByTagName('a')[0];
					  hmm.href = this.getElementsByTagName('img')[0].src.replace(/thumbnail/, 'big');
					  
						var x = new Image(), y = document.getElementById('iStoreProductGalleryPhoto').getElementsByTagName('img')[0];
						x.src = this.getElementsByTagName('img')[0].src.replace(/thumbnail/, 'midi'); /* uwaga: w razie potrzeby zmienic ! */
						y.src = x.src;
						this.blur();
						return false;
					};
				});
			}
			x = x.getElementsByTagName('li');
      
			if(x.length > istGallery.thumbs){
				document.getElementById('iStoreProductGalleryControls').style.display = 'block';
				document.getElementById('iStoreProductGalleryControlPrev').style.display = 'none';
        document.getElementById('iStoreProductGalleryControlPrev').getElementsByTagName('a')[0].onclick = function(){
					istGallery.prev();
					this.blur();
					return false;
				};
				document.getElementById('iStoreProductGalleryControlNext').getElementsByTagName('a')[0].onclick = function(){
					istGallery.next();
					this.blur();
					return false;
				};
				forEach(x, function(i){
					this.style.display = (i >= istGallery.thumbs) ? 'none' : 'block';
				});
			}
      else {
				document.getElementById('iStoreProductGalleryControls').style.display = 'none';
      }
		}
	},
	controls: function(x){ 
		document.getElementById('iStoreProductGalleryControlPrev').style.display = (x[0].style.display == 'none') ? 'block' : 'none';
		document.getElementById('iStoreProductGalleryControlNext').style.display = (x[x.length-1].style.display == 'none') ? 'block' : 'none';
	},
	prev: function(){

		var i, x = document.getElementById('iStoreProductGalleryThumbs').getElementsByTagName('li');
		if(x[0].style.display == 'none'){
			for(i = x.length - 1; i >= 0; i--){
				if(x[i].style.display != 'none'){
					x[i].style.display = 'none';
					break;
				}
			}
			for(i--; i >= 0; i--){
				if(x[i].style.display == 'none'){
					x[i].style.display = 'block';
					break;
				}
			}
		}
		istGallery.controls(x);
	},
	next: function(){
		var i, x = document.getElementById('iStoreProductGalleryThumbs').getElementsByTagName('li');
		if(x[x.length-1].style.display == 'none'){
			for(i = 0; i < x.length; i++){
				if(x[i].style.display != 'none'){
					x[i].style.display = 'none';
					break;
				}
			}
			for(i++; i < x.length; i++){
				if(x[i].style.display == 'none'){
					x[i].style.display = 'block';
					break;
				}
			}
		}
		istGallery.controls(x);
	}
};

/**
 * ocena produktu - gwiazdki
 */
var istStars = {
	init: function(){
		var x = document.getElementById('addOpinion');
		if(x){
			forEach(x.getElementsByTagName('select'), function(){
				var html = '<ul class="rating" onmouseover="this.className = \'rating rating-active\';" onmouseout="this.className = \'rating rating-inactive\';">';
				forEach(this.getElementsByTagName('option'), function(){
					html += '<li class="star-' + this.value + '"><a href="#" title="' + this.text + '" onclick="return istStars.select(this, ' + this.value + ');">' + this.text + '</a></li>';
				});
				html += '</ul>';
				this.style.display = 'none';
				this.parentNode.getElementsByTagName('label')[0].style.cursor = 'text';
				this.parentNode.innerHTML += html;
			});
		}
	},
	select: function(el, val){
		var x = el.parentNode.parentNode;
		forEach(x.getElementsByTagName('a'), function(){
			this.className = '';
		});
		x.className = 'rating rating-inactive';
		el.className = 'selected';
		forEach(x.parentNode.getElementsByTagName('select')[0].getElementsByTagName('option'), function(){
			this.selected = (this.value == val);
		});
		el.blur();
		return false;
	}
};

/**
 * rozwijanie menu jezykow i walut w ie6 (hover bug)
 */
/*@cc_on @*/
/*@if (@_jscript_version < 5.8)
if (navigator.userAgent.toLowerCase().indexOf("msie 6.0") != -1) {
	var lB = document.getElementById("langBox"),
		lS = document.getElementById("langSelect"),
		cB = document.getElementById("currBox"),
		cS = document.getElementById("currSelect");
	lB.onmouseenter = function () { lS.style.display = "block" };
	lB.onmouseleave = function () { lS.style.display = "none" };
	cB.onmouseenter = function () { cS.style.display = "block" };
	cB.onmouseleave = function () { cS.style.display = "none" };
}
/*@end @*/

/**
 * zaawansowana wyszukiwarka
 */
var istAdvSearch = {
	status: '',
	searchForm: '',
	searchLink: '',
	init: function () {
		var box = document.getElementById("istTopSearch").getElementsByTagName("div")[0];
		var el = box;
		var boxPos = {x: 0, y: 0};
		while (el) {
			boxPos.x += el.offsetLeft;
			boxPos.y += el.offsetTop;
			el = el.offsetParent;
		}
		var x = document.getElementById("istAdvSearch");
		
		x.style.top = boxPos.y + 41  + "px";
		x.style.left = boxPos.x + "px";
		x.style.width = box.offsetWidth + "px";

		// zapełnianie warstwy
		this.searchForm = document.getElementById("istAdvSearchContent");
		if (this.searchForm) {
			//x.appendChild(this.searchForm);
			this.status = "hidden";
		}

		var span = document.createElement("span");
		span.className = "istBot";
		x.appendChild(span);

		this.searchLink = document.createElement("a");
		this.searchLink.innerHTML = "wyszukiwarka zaawansowana";
		var that = this;
		this.searchLink.onclick = function () { that.changeView() };

		var linkwrap = document.createElement("div");
		linkwrap.id = "istAdvSearchLink";
		linkwrap.appendChild(this.searchLink);

		var span = document.createElement("span");
		span.className = "istBot";
		span.appendChild(document.createElement("span"));
		linkwrap.appendChild(span);
    
		x.appendChild(linkwrap);
	},
	changeView: function () {
		if (this.status === "hidden") {
			this.searchForm.style.display = "block";
			this.searchLink.innerHTML = "ukryj opcje zaawansowane";
			this.searchLink.className = "istHide";
			this.status = "visible";
		} else if ( this.status === "visible") {
			this.searchForm.style.display = "none";
			this.searchLink.innerHTML = "wyszukiwarka zaawansowana";
			this.searchLink.className = "istShow";
			this.status = "hidden";
		}
		return false;
	}
}

/**
 *	Boks z parametrami produktów, przed przejściem do koszyka
 */
var istParamCart = {
	box: 0,
	root: '',
	init: function () {
		var list = document.getElementById("istItemsIcon") || document.getElementById("istItemsList");
		if (list !== null) {
			var that = this;
			forEach(list.getElementsByTagName('a'), function () {
				if ( hasClass(this, 'withParam') ) {
					this.onclick = function () {
						that.showBox(this);
						return false;
					}
				}
			});
		}
	},
	showBox: function (el) {
		for ( var x = el.parentNode.parentNode.lastChild; x != null; x = x.previousSibling ) {
			if (x.nodeType == 1) {
				if (y = x.getElementsByTagName("form")[0]) {
					this.prepareBox(x, y);
				}
				break;
			}
		}
	},
	prepareBox: function (el, cont) {
		if (!this.box) {
			this.box = document.createElement("div");
			this.box.id = "istPopup";
			document.body.appendChild(this.box);
			var that = this, close = document.createElement("span");
			close.id = "closePopup";
			close.innerHTML = "X";
			close.onclick = function () { that.closeBox() };
			document.body.onkeydown = function (e) {
				if (window.event) {
					keynum = e.keyCode;
				} else if (e.which) {
					keynum = e.which;
				}
				if (keynum == 27) { that.closeBox() };
			};
			this.box.appendChild(close);
		} else {
			this.box.style.display = "block";
		}
		this.root = el;
		this.box.appendChild(cont);
		this.box.style.left = ((window.innerWidth || document.documentElement.clientWidth) - this.box.offsetWidth) / 2 + (window.pageXOffset || document.documentElement.scrollLeft) + "px";
		this.box.style.top = ((window.innerHeight || document.documentElement.clientHeight) - this.box.offsetHeight) / 2 + (window.pageYOffset || document.documentElement.scrollTop) + "px";
	},
	closeBox: function () {
		this.box.style.display = "none";
		this.root.appendChild(this.box.getElementsByTagName("form")[0]);
		document.body.onkeydown = "";
	}
};

var productId;
var langId;

function deleteFromNotepad(id) {
  productId = id;
  var ajax = new istAjax();
  ajax.setCallBackMethod('changeNotepadLinkToAdd');
  ajax.invoke('/notepad/delete?id='+id);
  return false;
}

function changeNotepadLinkToAdd(status) {
  if (status == 1) {
	  elem = document.getElementById('notepad');
	  elem.className = 'iStoreNotepadAddToList';
	  elem.onclick = function() {
	    return addToNotepad(productId, langId);
	  };
	  elem.innerHTML = "Dodaj do listy zakupów";
  }
}

function changeNotepadLinkToRemove(status) {
  if (status == 1) {
	  elem = document.getElementById('notepad');
	  elem.className = 'iStoreNotepadDeleteFromList';
	  elem.onclick = function() {
	    return deleteFromNotepad(productId, langId);
	  };
	  elem.innerHTML = "Usuń z listy zakupów";
	}
}

function addToNotepad(id) {
  productId = id;
  var ajax = new istAjax();
  ajax.setCallBackMethod('changeNotepadLinkToRemove');
  
  var props = "";
  $$('select').each(function(element, index){
    if (String(element.name).test('Prop')) {
      props = props+element.get('id')+"-"+element.getSelected()[0].get('value')+"|";
    }
  });
  
  ajax.invoke('/notepad/add?id='+id+'&props='+props);
  return false;
}

/**
 * Funkcja ustawia dlugosci kolumn w skelpie
 * pilnuje, zeby glowny content nie byl krotszy
 * od lewej kolumny
 * 
 * Funkcja wykorzystuje MooTools!
 * 
 * @author: Kuba Zgolinski
 */
function setColumnWidths() {
	// dla stron statycznych
	leftColWidth = $('istContentWrap').getHeight();
	mainColWidth = $$('#istWrap #istContentWrap .istContent #istStatic .istBoxWrap').getHeight();
	if (leftColWidth > mainColWidth)
		$$('#istWrap #istContentWrap .istContent #istStatic .istBoxWrap').setStyle('height', (parseInt(leftColWidth)-15));
}

/**
 * funkcje wywolywane onload
 */
istStripeUl.init();
istGallery.init(2);
istStars.init();
//istAdvSearch.init();
istParamCart.init();
//setColumnWidths();



