
$(document).ready(function(){
	$('#product_choose, #service_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;
		}
	}

	new function(){
		var news	= $('#news');
		var next	= $('#next_news');
		var prev	= $('#prev_news');
		var cache	= [news];
		var index	= 0;

		// Cache two news.
		getNews(index + 1);
		getNews(index + 2);

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

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

		function cacheNews(html, set_index){
			if(html){
				cache[set_index]	= $(html);
			}else{
				cache[set_index]	= false;
			}
		}

		// Get news from server.
		function getNews(get_index, insert){
			var parameters	= {'method' : 'get_news', 'index' : get_index};
			var url			= MAG.controller_url + MAG.ajax;

			$.get(url, parameters, function(html){
				cacheNews(html, get_index);

				if(insert){
					insertNews(get_index);
				}
			});
		}

		// If news is cached - insert it.
		// else call 'getNews' to get news via ajax
		// and then insert it.
		function findNews(get_index){
			if(get_index < 0){
				return;
			}

			// Get next news and cache it.
			if(cache[get_index + 2] === undefined){
				getNews(get_index + 2, false);
			}

			if(cache[get_index]){
				insertNews(get_index);
			}else if(cache[get_index] === undefined){
				// Get news and insert it.
				getNews(get_index, true);
			}

		}

		// Replace news content if avaliable
		// and deals with buttons.
		function insertNews(set_index){
			var new_element = cache[set_index];

			if(new_element){
				news.replaceWith(new_element);
				Cufon.replace('#news p');

				index	= set_index
				news	= new_element;

				if(cache[set_index + 1]){
					next.removeClass('disabled');
				}else{
					next.addClass('disabled');
				}

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

});

function activePartners(){
	// Using global 'cadooz_partners' from /public/js/partners.js
	var select			= $('#branche_choose');
	//var image			= $('#partners .partner_logo').get(0);
	var image			= $(new Image()).addClass('partner_logo');
	var next			= $('#next_partner');
	var prev			= $('#prev_partner');
	var category_index	= Math.floor(Math.random() * cadooz_partners.length);
	var partner_index	= Math.floor(Math.random() * cadooz_partners[category_index].childs.length);
	var category		= cadooz_partners[category_index];
	var partner			= category.childs[partner_index];

	useCategory(category_index, partner_index);
	$('#partners .image_holder img').after(image);


	$(select).get(0).onchange_listener = changeCategory;

	next.click(function(){showPartner(partner_index + 1);});
	prev.click(function(){showPartner(partner_index - 1);});

	function changeCategory(select_box){
		useCategory(select_box.value, 0);
	}

	function useCategory(c_index, p_index){
		if(!cadooz_partners[c_index]){
			return;
		}

		category	= cadooz_partners[c_index];
		partner		= category.childs[p_index ? p_index : 0];

		showPartner(p_index ? p_index : 0);
	}

	function showPartner(p_index){
		var max_index = category.childs.length > 0 ? category.childs.length - 1 : 0;

		if(p_index < 0 || p_index > max_index){
			return;
		}

		if(p_index == max_index){
			next.addClass('disabled');
		}else if(p_index < max_index){
			next.removeClass('disabled');
		}

		if(p_index == 0){
			prev.addClass('disabled');
		}else if(p_index > 0){
			prev.removeClass('disabled');
		}

		partner_index	= p_index;
		var logo		= getPartnerLogoUrl();

		if(logo){
			image.attr('src', logo);
			image.attr('alt', partner.name);
		}
	}

	function getPartnerLogoUrl(p_index, c_index){
		if(arguments.length === 2){
			try{
				var name = cadooz_partners[c_index].childs[p_index].title;
			}catch(e){}
		}else if(arguments.length === 1){
			try{
				var name = category.childs[p_index].title
			}catch(e){}
		}else{
			try{
				var name = category.childs[partner_index].title;
			}catch(e){}
		}

		if(name === undefined){
			return false;
		}

		return MAG.public + 'images/site_images/home/partners/' + name + '.jpg';
	}
}