function isIE() {
	return (document.all && document.getElementById && !window.opera) ? true : false;
}

function hoverOn(el) {
	el.className += " hover";
}
function hoverOff(el) {
	el.className = el.className.replace(" hover", "");
}
function hover(el) {
	if ( isIE() ) {
		el.onmouseover = function() { hoverOn(this); }
		el.onmouseout  = function() { hoverOff(this); }
	}
}

// aktivace rozbalovaciho menu pro IE
$(document).ready(function() {
	if ( isIE() ) {
		$('#nav > ul.navItems').each(function() {
			$('li', this).each(function() {
				hover(this);
			});
		});
	}
});

// aktivace hover efektu pro pridani do kosiku
$(document).ready(function() {
	if ( isIE() ) {
		$("button.cart-add", $("#content")).each(function() {
			hover(this);
		});
		$("button.cart-add-mini", $("#content")).each(function() {
			hover(this);
		});
	}
});


// automaticky vyskakovaci okna
var defaultPopupOptions = {
	width:		600,
	height:		440,
	toolbar:	0,
	scrollbars:	0,
	resizable:	1,
	parKey:		'popup',
	parVal:		1
}
$(document).ready(function() {
	$('a[@rel~=popup]').each(function() {
		var t = $(this);
		var rel = $.trim(t.attr('rel')).split(' ');
		for ( i in rel ) {
			if ( rel[i].substring(0, 5) == 'popup' ) {
				var popupOptions = defaultPopupOptions;
				var config = rel[i].substring(5);
				if ( config ) {
					var leftBracket = config.indexOf('[');
					var rightBracket = config.indexOf(']');
					config = config.substring(leftBracket+1, rightBracket-leftBracket);
					config = $.each(config.split(','), function() { return $.trim(this); });
					for ( j in config ) {
						var tmp = config[j].split('=');
						if ( tmp.length > 1 ) {
							popupOptions.tmp[0] = tmp[1];
						} else {
							switch ( parseInt(j) ) {
								case 0: popupOptions.width = tmp[0]; break;
								case 1: popupOptions.height = tmp[0]; break;
							}
						}
					}
				}
				// aktivace popup okna
				if ( popupOptions.parKey != null ) {
					this.href += (this.href.indexOf("?") < 0 ? "?" : "&") + popupOptions.parKey + (popupOptions.parVal != null ? '='+popupOptions.parVal : '');
				}
				t.click(function() {
					var left = Math.max(0, Math.round((screen.availWidth - popupOptions.width)/2));
					var top = Math.max(0, Math.round((screen.availHeight-85 - popupOptions.height)/2));
					return !window.open(this.href, null, 'width='+popupOptions.width+',height='+popupOptions.height+',left='+left+',top='+top+',toolbar='+popupOptions.toolbar+',scrollbars='+popupOptions.scrollbars+',resizable='+popupOptions.resizable);
				});
				break;
			}
		}
	});
});


// globalni nastaveni pro ajax
$(document).ready(function() {
	$('<div id="loading">Loading...</div>').appendTo('body').hide();
	$("#loading").ajaxStart(function() {
		$(this).show();
	});
	$("#loading").ajaxComplete(function() {
		$(this).hide();
	});
});

// porovnani produktu - "singleton" instance
var compare = new function Compare() {
	this.addUrl		= null;
	this.removeUrl	= null;
	this.resetUrl	= null;

	this.inClassName	= 'disabled';
	this.notInClassName	= false;

	this.compareBox	= null;

	this.getCompareBox = function() {
		if ( this.compareBox == null ) {
			this.compareBox = $("#compare-box");
			if ( this.compareBox.size() != 1 ) {
				this.compareBox = false;
			}
		}
		return this.compareBox;
	}

	this.isCompareEnable = function() {
		return this.getCompareBox() != false;
	}

	this.inCompare = function(pId, el) {
		if ( this.inClassName ) {
			return $(el).is("."+this.inClassName);
		} else if ( this.notInClassName ) {
			return $(el).not("."+this.notInClassName);
		} else {
			return null;
		}
	}

	this.setInCompare = function(el, inCompare) {
		el = $(el);
		if ( inCompare ) {
			if ( this.inClassName ) {
				el.addClass(this.inClassName);
			}
			if ( this.notInClassName ) {
				el.removeClass(this.notInClassName);
			}
		} else {
			if ( this.inClassName ) {
				el.removeClass(this.inClassName);
			}
			if ( this.notInClassName ) {
				el.addClass(this.notInClassName);
			}
		}
	}

	this.refreshCompareBox = function(data, action) {
		compareBox = this.getCompareBox();
		if ( compareBox ) {
			$('.count', compareBox).each(function() {
				$(this).html(data.count);
			});
			reset = $('.reset', compareBox);
			if ( data.count > 0 ) {
				compareBox.show();
				reset.show();
			} else {
				compareBox.hide();
				reset.hide();
			}
		}
	}

	this.callAddOrRemove = function(pId, el) {
		if ( ! el || ! this.inCompare(pId, el) ) {
			this.add(pId, function(success) {
				if ( success ) {
					compare.setInCompare(el, true);
				} else {
					window.location = el.href;
				}
			});
		} else {
			this.remove(pId, function(success) {
				if ( success ) {
					compare.setInCompare(el, false);
				}
			});
		}
		return true;
	}

	this.callReset = function(el) {
		this.reset(function(success) {
			if ( ! success ) {
				window.location = el.href;
			}
		});
		return true;
	}

	this.add = function(pId, completeCallback) {
		if ( ! this.isCompareEnable || ! this.addUrl )
			return true;

		var url = this.addUrl.replace("%s", pId);
		var success = function(data) {
			if ( data.success ) {
				compare.sendMessage(data.message.replace("%s", data.product['name']), 'success');
				compare.refreshCompareBox(data, 'add');
			} else {
				compare.sendMessage(data.message, 'warning');
			}
			completeCallback(true);
		}
		var error = function(request, error, exception) {
			compare.handleError(request, error, exception);
			completeCallback(false);
		}

		this.call(url, success, error);
	}

	this.remove = function(pId, completeCallback) {
		if ( ! this.isCompareEnable || ! this.removeUrl )
			return true;

		var url = this.removeUrl.replace("%s", pId);
		var success = function(data) {
			if ( data.success ) {
				compare.sendMessage(data.message.replace("%s", data.product['name']), 'success');
				compare.refreshCompareBox(data, 'remove');
			} else {
				compare.sendMessage(data.message, 'warning');
			}
			completeCallback(true);
		}
		var error = function(request, error, exception) {
			compare.handleError(request, error, exception);
			completeCallback(false);
		}

		this.call(url, success, error);
	}

	this.reset = function(completeCallback) {
		if ( ! this.isCompareEnable || ! this.resetUrl )
			return true;

		var url = this.resetUrl;
		var success = function(data) {
			if ( data.success ) {
				compare.sendMessage(data.message, 'success');
				compare.refreshCompareBox(data, 'reset');
				var elsInCompare = $('.porovnani > a', '#content');
				elsInCompare.each(function() {
					compare.setInCompare(this, false);
				})
			} else {
				compare.sendMessage(data.message, 'warning');
			}
			completeCallback(true);
		}
		var error = function(request, error, exception) {
			compare.handleError(request, error, exception);
			completeCallback(false);
		}

		this.call(url, success, error);
	}

	this.call = function(url, successCallback, errorCallback) {
		return $.ajax({
			type: "GET",
			async: true,
			url: url,
			dataType: 'json',
			success: successCallback,
			error: errorCallback
		});
	}

	this.sendMessage = function(msg, type) {
		if ( type == 'success' ) {
			alert(msg);
		} else {
			alert(msg);
		}
	}

	this.handleError = function(request, error, exception) {
		//this.sendMessage(error + "\n" + exception, 'error');
	}

}
