$(document).ready(function () {
	// video-entry
	$('.video-entry').live("mouseover", function() {
		$(this).css("background-color","#FFFFCC");
	}).live("mouseout", function() {
		$(this).css("background-color","#FFFFFF");
	});
	
	$('.video-entry').live("click", function() {
     		
			if ($.browser.mozilla) {
			$(this).click(function() {
				window.location = $(this).find("a").attr("href");
				return false;   
		   	});
		}
		
		if ($.browser.msie) {
			if (event.button != 0) {
				// wasn't the left button - ignore
				return true;// do some stuff
			}
			window.location = $(this).find("a").attr("href");
			return false; // "capture" the click
		}
		
		
	});
	
	// share
	$('#share-title').toggle(function() {
		$('#share-form').show("slow");
	},function() {
		$('#share-form').hide("slow");
	});
	
	// comment-entry-box
	/*
	$('#comment-entry-box').click(function() {
		if (this.value == '請寫評論...' ) {
			this.value = '';
			$(this).css("color", "#000000");
		}
	});
	$('#comment-entry-box').blur(function() {
		if (!this.value.length) {
			this.value='請寫評論...';
			$(this).css("color", "#777777");
		}
	});
	*/
	
	// search-box
	$('#search-box').click(function() {
		if (this.value == '關鍵字搜尋' ) {
			this.value = '';
			$(this).css("color", "#000000");
		}
	});
	$('#search-box').blur(function() {
		if (!this.value.length) {
			this.value='關鍵字搜尋';
			$(this).css("color", "#777777");
		}
	});
	
	// hd list
	/*
	$('#hd > ul > li').mouseover(function() {
		$(this).css("background-color", "#EFEFEF");
	}).mouseout(function() {
		$(this).css("background-color","");
	});*/
	
	// ajax comment
	var options = { 
		url: 'post-comment.php',   // target element(s) to be updated with server response 
		type: 'get',
		success: showResponse,  // post-submit callback 
		resetForm: true
	};
	$('#comment-form').ajaxForm(options);
	
	// ajax share
	var share_options = { 
		url: 'post-news.php',   // target element(s) to be updated with server response 
		type: 'post',
		success: showShareResponse,  // post-submit callback 
		resetForm: true
	};
	$('#share-form').ajaxForm(share_options);
	
	// ajax news
	var news_options = { 
		url: 'search-news.php',   // target element(s) to be updated with server response 
		type: 'post',
		success: showNewsResponse,  // post-submit callback 
		resetForm: false
	};
	$('#news-form').ajaxForm(news_options);
	
	// recent-news
	start = 10;
	$('#recent-news-list').scroll(function () { 
		clientHeight = document.getElementById('recent-news-list').clientHeight;
		scrollHeight = document.getElementById('recent-news-list').scrollHeight;
		scrollTop = document.getElementById('recent-news-list').scrollTop;
		
		if (scrollTop >= (scrollHeight-clientHeight-30)) {
			// fetch 10 more news
			$.ajax({
				type: "post",
				url: "get-recent-news.php",
				data: "start=" + start,
				success: function(newscontent){
					$('#recent-news-list').append(newscontent);
				}
			});
			start += 10;
		}
    });
	
	// autoplay
	$('#autoplay').click(function() {
		// change playlist
		$('embed').attr('src', 'playlist.php');		
		$('object').attr('url', 'playlist.php');	
		// yui-g h1
		$('#news-title h1').html("自動播放昨天新聞");
		// change playlist
		$('embed').attr('src', 'playlist.php');		
		$('object').attr('url', 'playlist.php');
	});
	
	// today-news and recent-news show or hide
	$('#today-news > h2 > a').live("click", function() {
		$('#today-news > h2 > a > img').attr("src", "img/right.png");
		$('#today-news-list').hide();
		$(this).toggle(function() {
			$('#today-news > h2 > a > img').attr("src", "img/down.png");
			$('#today-news-list').show();
		},function() {
			$('#today-news > h2 > a > img').attr("src", "img/right.png");
			$('#today-news-list').hide();
		});
	});
	
	$('#recent-news > h2 > a').toggle(function() {
		$('#recent-news > h2 > a > img').attr("src", "img/right.png");
		$('#recent-news-list').hide();
	},function() {
		$('#recent-news > h2 > a > img').attr("src", "img/down.png");
		$('#recent-news-list').show();
	});
	
});

function showResponse(responseText, statusText)  { 
	// update while success
	$('.comment > .comment-box').before(responseText);
	// responseText
}

function showShareResponse(responseText, statusText)  { 
	// update while success
	alert(responseText);
}

function showNewsResponse(responseText, statusText)  { 
	// update while success
	$('#today-news').html(responseText);
}

// fix click here to activate it
theObjects = document.getElementsByTagName("object");
for (var i = 0; i < theObjects.length; i++) {
	theObjects[i].outerHTML = theObjects[i].outerHTML;
}