/* jQuery
-------------------------------------------------- */
var toggleH2boxes = {
	init : function(){
		$("h2.toggle a, h3.toggle a").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parent().toggleClass("toggle-open");
		$(this).parent().next("div.toggle-wrap:first").slideToggle("fast");
		return false;
	}
};

/* data tables */

function dataHover() {
	$("table.data tbody:not(.form) tr").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
}
var toggleSubrow = {
	init : function(){
		$("table.data tbody a.toggle-subrow").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).parents("tr").next("tr.subrow").css("display") == "none") {
			$(this).parents("tr").addClass("toggler");
			$(this).parents("tr").next("tr.subrow").css("display","");
		} else {
			$(this).parents("tr").removeClass("toggler");
			$(this).parents("tr").removeClass("marked");
			$(this).parents("tr").next("tr.subrow").css("display","none");
		}
		return false;
	}
};
var toggleSubrow2 = {
	init : function(){
		$("table.data tbody.subrows tr.toggle-level a.toggle-subrows").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).parents("tr").nextAll("tr").css("display") == "none") {
			$(this).addClass("toggle-open");
			$(this).parents("tr").nextAll("tr").css("display","");
		} else {
			$(this).removeClass("toggle-open");
			$(this).parents("tr").removeClass("marked");
			$(this).parents("tr").nextAll("tr").css("display","none");
		}
		return false;
	}
};
function fixIEoverflow() {
	if (!/*@cc_on!@*/0) return;

	var all = document.getElementsByTagName('*'), i = all.length;
	while (i--) {
		// adding a class match just to show the difference
		if (all[i].className.match(/scroll/) && all[i].scrollWidth > all[i].offsetWidth) {
			all[i].style['overflowY'] = 'hidden';
			all[i].style['paddingBottom'] = '17px';
		}
	}
}

function toggleActions() {
	if( $("ul.menu").length > 0 ) {
	$("table.data td.actions ul.menu > li").hoverIntent(
		function(){
			$(this).children("div").css("display", "block");
				$(this).children("div").css("margin-left", $(this).children("span").width() - $(this).children("div").width() + 79);
			if($.browser.msie && ($.browser.version < 8) ){
				$(this).children("div").css("margin-left", - $(this).children("div").width() - 2);
			}
			$(this).children("a").addClass("open");
		},
		function(){
			$(this).children("div").css("display","none");
			$(this).children("a").removeClass("open");
		}
	);
}
}
var headingSort = {
	init : function(){
		$("table.data thead th.sort a").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).parent().is(".ascending")) {
			$(this).parents("thead").find("th").removeClass("ascending");
			$(this).parents("thead").find("th").removeClass("descending");
			$(this).parent().removeClass("ascending");
			$(this).parent().addClass("descending");
		}
		else if ($(this).parent().is(".descending")) {
			$(this).parents("thead").find("th").removeClass("ascending");
			$(this).parents("thead").find("th").removeClass("descending");
			$(this).parent().removeClass("descending");
			$(this).parent().addClass("ascending");
		}
		else {
			$(this).parents("thead").find("th").removeClass("ascending");
			$(this).parents("thead").find("th").removeClass("descending");
			$(this).parent().addClass("ascending");
		}
		return false;
	}
};

/* radio, checkbox */

var markRows = {
	init : function(){
		$("table.data tbody:not(.form) tr").dblclick(this.clickIt);
	},
	clickIt : function(){
		$(this).toggleClass("marked");
	}
};
var markAllCheck = {
	init : function(){
		$("div.sub-action a.mark-all").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input:checkbox").attr("checked","checked");
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input:checked").parents("tr").addClass("checked");
		return false;
	}
};
var markNoneCheck = {
	init : function(){
		$("div.sub-action a.mark-none").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input:checkbox").removeAttr("checked");
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input").not(":checked").parents("tr").removeClass("checked");
		return false;
	}
};
var setCheckbox = {
	init : function(){
		$("table.data tr td.check input:checkbox").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).is(":checked")) {
			$(this).parents("tr").addClass("checked");
		} else {
			$(this).parents("tr").removeClass("checked");
		}
	}
};
var setRadio = {
	init : function(){
		$("table.data tr td.check input:radio").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("table").find("tr.checked").removeClass("checked");
		$(this).parents("tr").addClass("checked");
	}
};
var setRadioDatasp = {
	init : function(){
		$("table.data-sp tr td.price input:radio").click(this.clickIt);
	},
	clickIt : function(){
		$("table.data-sp tr td.price input:radio").next("label").css("font-weight","normal");
		$("table.data-sp tr td.price input:radio:checked").next("label").css("font-weight","bold");
	}
};
function controlCheckbox() {
	$("table.data tbody tr td.check input:checkbox:checked").parents("tr").addClass("checked");
}
function controlRadio() {
	$("table.data tbody tr td.check input:radio:checked").parents("tr").addClass("checked");
}

var Stone = {
	init: function() {
		$('.stone>a').mouseover(Stone.over);
		$('.stone>a').mouseout(Stone.out);
	},
	
	over: function() {
		var id = $(this).parent().attr('id');
		$('#' + id).css('background-image', 'url(gfx/' + id + '_active.png)');
	},
	
	out: function() {
		var id = $(this).parent().attr('id');
		$('#' + id).css('background-image', 'url(gfx/' + id + '.png)');
	}
}

var Question = {
	value: null,
		
	init: function() {
		$('.answer').click(Question.answer);
	},
	
	answer: function() {
		Question.value = $(this).val();
		
		$('#answers').remove();
		$.ajax({
		  url: '/newsletter/join/',
		  success: function(data) {
			$('#for').html(data);
			$('#send').click(Question.send);
		  }
		});
	},
	
	send: function() {
		var name = $('#name').val();
		var mail = $('#mail').val();
		$.ajax({
		  url: '/newsletter/join-successful/name/' + name + '/mail/' + mail + '/value/' + Question.value,
		  success: function(data) {
			$('#join').remove();
			$('#send').remove();
			$('#for').html(data);
			
		  }
		});
	}
}

var Tags = {
	init: function () {
		$("#tag").autocomplete(
			"/blog/autocomplete", {
				delay:10,
				minChars:1,
				matchContains:1,
				cacheLength:10,
				autoFill:false
			}
		);
	}
};

$(document).ready(function(){
	Stone.init();
	Question.init();
	Tags.init();
	toggleActions();
	
	$("table.data tr.subrow").css("display","none");
	toggleSubrow.init();
	toggleSubrow2.init();
	$("table.data tbody.subrows tr").css("display","none");
	$("table.data tbody.subrows tr.toggle-level").css("display","");
	
	
});
