var glossaryWidth = 400;
var glossaryHeight = 250;
var helpWidth = 650;
var helpHeight = 540;

var onQuickChoise = false;

$(function() {
	$('a[id^=add-compare-]').click(function() {	
		setCompareFlag(this, ($(this).text() == ADD_TO_COMPARE));
		$.getJSON(
			$(this).attr('href'),
			new Object({isAjax: true}), 
			function(json) { onAddToCompareChanged(json);}
		);	
		return false;
	});

	if(typeof(compareProductsTxt) != "undefined" && compareProductsTxt != '') {
		$(compareProductsTxt).each(function(i) {	
			setCompareFlag(this, true);
		});
	}

	//set effect from select menu value
	$("#quick-choise").click(function() {
		$("#quick-choise-content").toggle('blind', {},300);
		return false;
	});
	
	$("#quick-choise").mouseover(function() {
	
		if($("#quick-choise-content:visible").size() == 0) {
			$("#quick-choise").addClass('quick-choise-hover');
		}
	});
	
	$("#quick-choise").mouseleave(function() {
		if($("#quick-choise-content:visible").size() == 0) {			
			$("#quick-choise").removeClass('quick-choise-hover');
		}
	});
	
	$("#quick-choise-content").mouseleave(function() {
		onQuickChoise = false;
	});
	
	$("#quick-choise-content").mouseover(function() {
		onQuickChoise = true;
	});

	$(document).click(function() {
		if(!onQuickChoise && $("#quick-choise-content:visible").size() == 1) {
			$("#quick-choise").trigger('click');
			$("#quick-choise").removeClass('quick-choise-hover');
		}	
	});
	
	var objects = document.getElementsByTagName("object");
	for (var i=0; i<objects.length; i++) {
	    objects[i].outerHTML = objects[i].outerHTML;
	}

});

function setCompareFlag(obj, isInCompare) {
	className = $(obj).attr('class');
	
	if(className.substr(className.length - 9) == '-disabled') {
		className = className.substring(0, className.length - 9);
	} 
	
	if(isInCompare) {
	 	$(obj).removeClass(className);
	 	$(obj).addClass(className + '-disabled');
	 	$(obj).text(REMOVE_FROM_COMPARE);
 	} else {
 		$(obj).removeClass(className + '-disabled');
 		$(obj).addClass(className);
	 	$(obj).text(ADD_TO_COMPARE);
 	}
}

function onCompareChanged(json, type) {
	// nothing to do
}

function onAddToCompareChanged(json) {
	if(json.result) {		
		$('#compare-products-link').text(PRODUCT_COMPARISON + ': ' + json.count);
		onCompareChanged(new Array(json.rs), json.result == 'add');
		if(json.result == 'remove' && removeCompareItem != null) {
			removeCompareItem(json.rs);
		}
	}
}

function formatMonetaryAmount(nStr, currency) {
	nStr = Math.round(nStr*100)/100;
	nStr += '';
	rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) nStr = nStr.replace(rgx, '$1' + ' ' + '$2');

	suffix = '';

	if(currency == 'Kč') {
		suffix = '';
	} else {	
		if(nStr != 0) {
			nStr = nStr.replace('\.', ',');
			if(nStr.indexOf(',') == nStr.length - 2) {
				nStr += '0';
			}
		} else {
			nStr = '0,00';
		}
	}
	
	
	return nStr + suffix + ' ' + currency;
}

function openGlossary(url) {
	scroll_bars = "yes";
	if((typeof(win)!= "undefined") && (win.closed == false)){
		x1 = win.document.body.clientWidth;
		y1 = win.document.body.clientHeight;
		win.location = url;
		win.resizeBy(glossaryWidth-x1, glossaryHeight-y1);
	}else{
		win = window.open(url,"glosar", "width="+glossaryWidth+",height="+glossaryHeight+",toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars="+scroll_bars);
	}
	win.focus();
	return true;
}

function openHelp(url) {
	scroll_bars = "yes";
	if((typeof(win)!= "undefined") && (win.closed == false)){
		x1 = win.document.body.clientWidth;
		y1 = win.document.body.clientHeight;
		win.location = url;
		win.resizeBy(helpWidth-x1, helpHeight-y1);
	}else{
		win = window.open(url,"glosar", "width="+helpWidth+",height="+helpHeight+",toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars="+scroll_bars);
	}
	win.focus();
	return true;
}

function roundNumber(rnum, rlength) {
	 var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

function trackEvent(category, action, optional_label, optional_value) {
//	alert(action);
	if(pageTracker) {
		pageTracker._trackEvent(category, action, optional_label, optional_value);
	}
}

