$(function () {
	$(".validate").blur( function() {
		var fid = $(this).attr("id");
		var v_task = $(this).attr("task");
		var msgarea = $("#message_" + fid);
		$.post("index.php",
			{ field_id: fid, input_val: $(this).val(), task: v_task, option: "com_dpmaterial" },
			
			function (response) {
				var status = $("status", response).text();
				msgarea.html( (status != 'ok') ? status : $("#message_" + fid).html() );
				if (status != 'ok') {
					if ( msgarea.is(":hidden") ) 
						msgarea.slideDown();
				}
				else if ( ! msgarea.is(":hidden") )
					msgarea.slideUp();
			}
		);
	});
	
	function updateHitCounter(mid, jqmat) { 
		var last_mid = $("input#last_mid").val();
		if ( last_mid != mid ) {
			$.post("index.php", { task: "rh", option: "com_dpmaterial", mid: mid  }, 
			function (response) { 
				var counter = $("counter", response).text();
				if ( counter > 0 ) {
					$("span.hitcounter").html(counter);
					jqmat.attr('mathits', counter);
				}
			});
		}
		$("input#last_mid").val(mid);
	}
	
	function chgImage() {
		// this viittaa siihen elementtiin, jonka onclik-eventti suoritetaan
		$("img.matimg").attr('src', 'components/com_dpmaterial/images/matloader.gif');
		jqmat = $(this);
		$("div.thumbmat_cont").attr('style', '');
		var thumb_img = jqmat.attr('dpimg');
		var file = jqmat.attr('file');
		var info = jqmat.attr('matinfo');
		var name = jqmat.attr('matname');
		var matowner = jqmat.attr('matowner');
		var mathits_txt_start = jqmat.attr('mathits_txt_start');
		var mathits_txt_end = jqmat.attr('mathits_txt_end');
		var mathits = jqmat.attr('mathits');
		var full_size_file = jqmat.attr('full_size_file');
		var mid = jqmat.attr('mid');
		var mat_type = jqmat.attr('mat_type');
		var embed = jqmat.attr('embed');
		var read_comments_txt = jqmat.attr('read_comments_txt');
		var comment_count = jqmat.attr('comment_count');
		var photographer = jqmat.attr('photographer');

		$("input#mat_send_comment").attr('mid', mid);
		$("div#mat_comments_list").html('<img src="components/com_dpmaterial/images/matloader.gif" alt="" />');
		$("span#mat_space_txt").html(read_comments_txt);
		$("span#mat_count_txt").html(comment_count);
		
		$("div.matpreview_abs").css("top", ($(document).scrollTop() + 10) + "px");
		$("div.mathits").html(mathits_txt_start + mathits + mathits_txt_end);
		updateHitCounter(mid, jqmat);
		$("div.matname").html(name);
		$("div.matowner").html(matowner);
		$("a.mat_magnify").attr('href', full_size_file);
		$("a#change_mat_space").attr('mid', mid);
		
		info = (info) ? info : '';
		$("p.matinfo").html(info);
		
		if (mat_type == 1) {
			$("p.matinfo").html(info);
			$("div#tubeinfo").html('');
			$("p.matinfo").show();
			$("div#tubeinfo").hide();
		}
		else {
			$("p.matinfo").html('');
			$("div#tubeinfo").html(info);
			$("div#tubeinfo").show();
		}
		
		if (photographer) {
			$("span#photographer_name").html(photographer);
			$("div#photographer").show();
		}
		else {
			$("span#photographer_name").html('');
			$("div#photographer").hide();
		}
		
		if ( $("div.matpreview_abs").is(":hidden") ) {
			if ( mat_type == 1 ) {
				$("div#you_tube_cont").html('');
				$("img.matimg").attr('src', file);
			}
			else
				$("div#you_tube_cont").html(embed);
		}
		else {
			if ( mat_type == 1 ) {
				$("img.matimg").fadeOut( function() {
					$("div#you_tube_cont").html('');
					$($("img.matimg").attr('src', file)).fadeIn("fast");
				});
			}
			else
				$("div#you_tube_cont").html(embed);
		}
		
		if ( mat_type == 1) {
			$("div.mat_xtra_tools").show();
			$("div#you_tube_cont").hide();
			$("div#dpmat_pic_cont").show();
			$("mat_xtra_tools").hide();
		}
		else {
			$("div.mat_xtra_tools").hide();
			$("div#you_tube_cont").show();
			$("div#dpmat_pic_cont").hide();
		}
		$("div.matpreview_abs").show();
		$("div.mat_thumbcont").attr('style', '');
		//jqmat.parent(".mat_thumbcont").attr('style', 'border: 2px solid #993300');
		$(".borders").attr('style', '');
		$("#"+thumb_img).attr('style', 'border: 1px solid #993300');
		
		$("div#matdetails_holder").show();
		$("div#mat_commentsholder").hide();
	}
	
	function changeList() {
		$("div#mat_thumbholder").html('<img src="components/com_dpmaterial/images/matloader_red.gif" alt="" />');
		$("div#mat_thumbholder").load(
			"index.php", 
			{ start: $(this).attr('dpstart'), task: "get_matlist", option: "com_dpmaterial", cid: $(this).attr("cid") }, 
			function (rtxt){
				// if(console){ console.log("update ok"); }
				$("div#mat_thumbholder").html(rtxt);
				$("div.thumbmat_cont").bind( 'click', chgImage );
				$("a.matbrowser_link").bind( 'click', changeList );
		});
	}

	$("div.thumbmat_cont").bind( 'click', chgImage );
	$("a.matbrowser_link").bind( 'click', changeList );
	
	function closePreview() { 
		$("div#you_tube_cont").html('');
		$("div.matpreview_abs").hide(); 
	}
	
	$("div.mat_closeimg").bind( 'click', closePreview );
	$("div.mat_closetxt").bind( 'click', closePreview );
	
	$("div.catitem_holder").mouseover( function() {
		$(this).css({background: "#ffffcc"});
	});
	
	$("div.catitem_holder").mouseout( function() {
		$(this).css({background: "none"});
	});

	$("div#dpmatperview").draggable(
		{handle:'#maptreview_handle', opacity: 0.8}
	);
	
	$("a#dpbtn_add_you_tube").click( function() {
		$("div#pic_form_elements").hide();
		$("div#youtube_form_elements").show();
		$(this).attr('class','selected');
		$("a#dpbtn_add_pic").removeAttr('class');
		$("input#mat_type").val(2);
		$("div.formmsg").hide();
		return false;
	});
	
	$("a#dpbtn_add_pic").click( function() {
		$("div#youtube_form_elements").hide();
		$("div#pic_form_elements").show();
		$(this).attr('class','selected');
		$("a#dpbtn_add_you_tube").removeAttr('class');
		$("input#mat_type").val(1);
		$("div.formmsg").hide();
		return false;
	});
	
	$("a#change_mat_space").click( function() {
		var read_comments_txt = $(this).attr('read_comments_txt');
		var back_to_mat_txt = $(this).attr('back_to_mat_txt');
		
		if ( $("div#matdetails_holder").is(":hidden") ) {
			$("div#matdetails_holder").show();
			$("div#mat_commentsholder").hide();
			$("span#mat_space_txt").html(read_comments_txt);
			$("span#mat_count_txt").html("("+ $("input#comment_counter").val() +")");
			//$("a#change_mat_space").html(read_comments_txt);
		}
		else {
			$("div#matdetails_holder").hide();
			$("div#mat_commentsholder").show();
			$("span#mat_count_txt").html("&nbsp;");
			$("span#mat_space_txt").html(back_to_mat_txt);
			//$("a#change_mat_space").html(back_to_mat_txt);
			$("div#mat_comments_list").load(
				"index.php", 
				{ task: "print_comments", option: "com_dpmaterial", mid: $(this).attr("mid") }, 
					function (rtxt){
						// if(console){ console.log("update ok"); }
						$("div#mat_comments_list").html(rtxt);
				}
			);
		}
	});
	
	$("input#mat_send_comment").bind( 'click', saveComment );
	
	function saveComment() {
		var jqmat = $(this);
		var nickname = $("input#mat_comment_nickname").val();
		var comment = $("textarea#mat_comment").val();
		var mid = jqmat.attr("mid");
		var mand_fill_error = jqmat.attr("mand_fill_error");
		var comment_success_msg = jqmat.attr('success_msg');
		
		if ( nickname != '' && comment != '' && mid > 0 ) {
			$("div#mat_comments_list").html('<img src="components/com_dpmaterial/images/matloader.gif" alt="" />');
			$.post("index.php",
				{ mid: mid, dpnickname: nickname, dpcomment: comment, task: 'save_comment', option: "com_dpmaterial" },
				
				function (response) {
					var status = $("status", response).text();
					if (status != 'ok') {
						alert('Save failed.');
					}
					else {
						$("div#mat_comments_list").load(
						"index.php", 
						{ task: "print_comments", option: "com_dpmaterial", mid: mid }, 
							function (rtxt){
								// if(console){ console.log("update ok"); }
								$("div#mat_comments_list").html(rtxt);
								$("input#mat_comment_nickname").val('');
								$("textarea#mat_comment").val('');
								$("div.thumbmat_cont"+ mid).attr('comment_count', '('+ $("input#comment_counter").val() + ')');
						});
					}
				}
			);
		}
		else alert(mand_fill_error);
	}
	
	/* functions below because of ie6 */
	$("select.parent_selector option").each(function() {
		if ($(this).attr('disabled')) {
			$(this).css({color: '#aca899'});
		}
	});
	
	$("select.parent_selector").click( function() {
		var selected_index = $("select.parent_selector").get(0).selectedIndex;
		
		$("select.parent_selector option").each(function() {	
			if (($(this).get(0).index == selected_index) && $(this).attr('disabled'))
				$("select.parent_selector").get(0).selectedIndex = 0;
		});
	});
});
