// JavaScript Document

$(document).ready(function() {
// put all your jQuery goodness in here.

	blockForm();

	$('.inputText').focus(function() {
		$(this).val('');
		$(this).css('color', '#000');
	});

	$('.tabs').click(function() {
		ResetTabs();
		$(this).attr('class', 'select tabs');
	});
	
	$('.selectDiv').mouseover( function() {
		var id = $(this).attr('id');
		
		var elems = 0;
		
		$('#'+id+' ul li').each(function() { elems++; });
		
		$('#'+id+' ul').css('display', 'block');
		$('#'+id+' ul').css('top', -(19*elems));
		$('#'+id+' ul').css('left', -5);
	});
	
	$('.selectDiv').mouseout( function() {
		var id = $(this).attr('id');
		$('#'+id+' ul').css('display', 'none');
	});
	
	$(".addcompare").click( function() {
		var tmp = $(this).attr('id');
			tmp = tmp.split('_');
			var id = tmp[1];
	
		$("#comparateur").load("/chiens/ajaxFiche", { 'id': id});
		$(this).html('Ce chien a bien été ajouté au comparateur');
	});
	
	$('#PrevPet').click(function() {
		var style = $('#PetImageContainer').attr('style');
		var tmp = style.split('margin-left:');
		
		if(tmp.length > 1) {
		
			var tmp2 = tmp[1].split('px');
			var margin = tmp2[0];
			
			if(parseInt(margin) < 0 && (!(margin%46) || margin == 0)) {
				$('#PetImageContainer').animate({"marginLeft": "+=46px"}, "slow");
			}
		}
	});
	
	$('#NextPet').click(function() {
		var style = $('#PetImageContainer').attr('style');
		var tmp = style.split('margin-left:');
		
		if(tmp.length > 1) {
		var tmp2 = tmp[1].split('px');
		var margin = tmp2[0];
		}
		else { margin = 0; }
		
		var tmp = style.split('width:');
		if(tmp.length > 1) {
		var tmp2 = tmp[1].split('px');
		var width = tmp2[0];
		}
		else { width = 0; }
		
		if( (margin-46) > -width && (!(margin%46) || margin == 0)) {
			$('#PetImageContainer').animate({"marginLeft": "-=46px"}, "slow");
		}
	});

	if($(".url_selection")){
		
	$(".url_selection").each( function() {
		$(this).change( function() {
			window.location.replace($(this).val());
		});
	});
	
	$(".menu_cat img").click(function() {
		var id = $(this).parent().attr("id");
		var el = "#"+id+" ul li";
		
		if($(el).css("display") == "none") {
				$(el).css("display", "block");
				$(el+" ul li").css("display", "none");
		}
		else $(el).css("display", "none");
	});
	}

});

function increment(item_id) {
	var val = parseInt($("#"+item_id).html());
	val++;
	$("#"+item_id).html(val);
}


function doPrint(url) {
	window.open(url, "printBox", "menubar=no, status=no, scrollbars=yes, menubar=no, width=650, height=800");
}

function ResetTabs() {
	$('.tabs').each(function() {
		$(this).attr('class','tabs');
	});
}

function blockForm() {	
	$('form').submit(function() {
		$('button.custom').each(function() {
			$(this).attr('disabled', 'disabled');
		});
	});
}

function minimize() {
	var size = parseInt($(".news_description").css("font-size").split("p")[0]);
	if(size > 10) $(".news_description").css("font-size", (size-2)+'px');
};

function maximize() {
	var size = parseInt($(".news_description").css("font-size").split("p")[0]);
	if(size < 20) $(".news_description").css("font-size", (size+2)+'px');
};

function addFriend() {
	
	var nb = parseInt($("#nb_elements").val());
	
	$("#select_boxes").append("<label>Email de votre ami : </label><input type='text' name='selected_emails["+ nb + "]'><br>");
	nb++;
	
	$("#nb_elements").val(nb);
};

function checkcompare(data) {
	if(data['status'] == 1) $("#comparateur").html(data["html"]);
	else alert(data["error"]);
};

function menu_aff(id) {
	$('#'+id+ ' .element').css('display', 'block');
	$('#'+id).css('background-position', '0 -35px');
	$('#'+id+' .tr').addClass('menu_hover');
};

function menu_hide(id) {
	$('#'+id+ ' .element').css('display', 'none');
	$('#'+id).css('background-position', '0 0');
	$('#'+id+' .tr').removeClass('menu_hover');
};

function enableButtons() {
	$('button.custom').each(function() {
		$(this).removeAttr('disabled');
	});
	
	
	$('button.custom', parent.document).each(function() {
		$(this).removeAttr('disabled');
	});

}

function recordOutboundLink(link, category, action) {
    try {
        var pageTracker=_gat._getTracker("UA-7463610-14");
        pageTracker._trackEvent(category, action);
        setTimeout('window.open("' + link.href + '")', 100)  }
    catch(err){}
}
