 $(document).ready(function() {

	// history back link
	$("a.i-backward").click(function() {
		history.back();
		return false;
 	});

 	// productlist.phtml
	$("form[name=product_sorting] select").change(function() {
		$("form[name=product_sorting]").submit();
	});

	// productfilter.phtml
	$("form[name=product_price] input").click(function() {
		$(this).attr("checked","checked")
		$("form[name=product_price]").submit();
	});
	$("form[name=product_checkbox] input").click(function() {
		if($('#selected_'+$(this).attr('id')).attr("value") == $(this).attr('id')) {
			$(this).removeAttr("checked");
			$('#selected_'+$(this).attr('id')).attr("value", '');
		} else {
			$(this).attr("checked","checked");
			$('#selected_'+$(this).attr('id')).attr("value", $(this).attr('id'));
		}
		$("form[name=product_checkbox]").submit();
	});

	// productabstract.phtml
	$("ul#pr-mediaselect a").click(function() {
		$("ul#pr-mediaselect li").removeClass('active');
		$(this).parent().addClass('active');
		switch ($(this).attr('class')) {
			case 'pr-mediaselect-video':
				var flashvars = {
					file: $(this).attr('href'),
					image: $('#pr-image').attr('src'),
					autostart: "true",
					plugins:"googlytics-1"
				}

				var params = {
					allowfullscreen: "true",
					allowscriptaccess: "always",
					bufferlength: "1",
					wmode: 'opaque'
				}

				var attributes = {
					id: "pr-video",
					name: "pr-video"
				}
				swfobject.embedSWF("/lib/portal/flash/skinned-player.swf", "pr-video", "386", "335", "9.0.115", false, flashvars, params, attributes);

				$("#pr-image").hide();
				$("#pr-video").show();
			break;
			case 'pr-mediaselect-image':
				$("#pr-image").attr('src', $(this).attr('href'));
				$("#pr-video").hide();
				$("#pr-image").show();
			break;
		}

		return false;
	});

	//comments.phtml
	$("ol.rating a").click(function() {

		$("form[name=productcomment] input[name=rating]").val($(this).text());
		$("ol.rating").removeClass('null');
		$("ol.rating").removeClass('one');
		$("ol.rating").removeClass('two');
		$("ol.rating").removeClass('three');
		$("ol.rating").removeClass('four');
		$("ol.rating").removeClass('five');

		switch($(this).text()) {
			case '1': {
				$("ol.rating").addClass('one');
				break;
			}
			case '2': {
				$("ol.rating").addClass('two');
				break;
			}
			case '3': {
				$("ol.rating").addClass('three');
				break;
			}
			case '4': {
				$("ol.rating").addClass('four');
				break;
			}
			case '5': {
				$("ol.rating").addClass('five');
				break;
			}
		}

		return false;
	});

	$("form[name=productcomment]").submit(function() {

		$.post(
			$(this).attr('action'),
			{
				rating: $("input[name=rating]", this).val(),
				comment: $("input[name=comment]", this).val()
			},
			function(data){
				for(var i in data) {
					$(i).html(data[i]);
				}
			},
		"json");

		return false;
	});

 });

