
MAG.onSelectValueChange = function(options){
	if(options === undefined){
		var options = {};
	}

	if(this.value == 0){
		self.location = self.location.pathname;
	}else{
		self.location = (options.url ? options.url : '') + (options.key ? options.key : '') + this.value;
	}
};

$(document).ready(function(){
	teaserSlider($('#why_cadooz p'), $('#why_cadooz_next'), $('#why_cadooz_prev'), 9, 285);
	teaserSlider($('#facts_quotes p'), $('#facts_quotes .arrow_right'), $('#facts_quotes .arrow_left'), 6, 276);
	teaserSlider($('#glosary'), $('#glosary_next'), $('#glosary_prev'), 4, 265);

	function teaserSlider(box, next, prev, max_index, width){
		if(box && box.length){
			var index = 0;

			next.click(function(){
				getTeaser(index + 1);
			});

			prev.click(function(){
				getTeaser(index - 1);
			});

			function getTeaser(get_index){
				if(get_index < 0 || get_index > max_index){
					return;
				}

				if(get_index == max_index){
					next.addClass('disabled');
				}else if(get_index == 0){
					prev.addClass('disabled');
				}else{
					next.removeClass('disabled');
					prev.removeClass('disabled');
				}


				index = get_index;

				box.css('backgroundPosition', (width * -get_index) + 'px 0');
			}
		}
	}

	$('a.iframe').click(function() {
		$('#content_for_layout').html('<iframe style="width: 100%; height: 510px; overflow: hidden;" frameborder="0" name="search_frame" onload="window.top.set_iframe_size();" src="' + this.href + '"></iframe>');
		return false;
	});

	// our products teaser
	$('#product_choose').each(function(){
		this.onchange_listener = MAG.onSelectValueChange;
	});

	new function(){
		var links		= $('#products_images div a');
		var texts		= $('#products_texts p');
		var next		= $('#next_product');
		var prev		= $('#prev_product');
		var index		= 0;
		var last_index	= links.length - 1;

		next.click(function(){
			showProduct(index + 1);
		});

		prev.click(function(){
			showProduct(index - 1);
		});

		function showProduct(get_index){
			if(get_index < 0 || get_index > last_index){
				return;
			}

			if(get_index == last_index){
				next.addClass('disabled');
				prev.removeClass('disabled');
			}else if(get_index == 0){
				prev.addClass('disabled');
				next.removeClass('disabled');
			}else{
				next.removeClass('disabled');
				prev.removeClass('disabled');
			}


			links.each(function(key, ele){
				if(key === get_index){
					$('#product_text').html(texts[key].innerHTML);
					$(ele).css('display', 'inline');
				}else{
					$(ele).css('display', 'none');
				}
			});

			index = get_index;
		}
	}
});

var set_iframe_size = (function() {
	var cnt = 0;

	return function() {
		var iframe = $('#content_for_layout iframe');
		iframe.height(cnt++ ? 710 : 510);	// hardcoded - the only way to avoid xss
	};
})();

function showHideFaq(sender, tag_name) {
	var faq_cnt = sender.parentNode.getElementsByTagName(tag_name || 'ul')[0],
		dspl;

	if (faq_cnt) {
		dspl = faq_cnt.style.display ? '' : 'block';
		faq_cnt.style.display = dspl;
		if (dspl) {
			sender.className += ' active_faq';
		} else {
			sender.className = sender.className.slice(0, -11);
		}
	}
}

