$(document).ready(function(){
	/* for embeding of youtube */
	// I added the video size here in case you wanted to modify it more easily
	var vidWidth = 560;
	var vidHeight = 345;

	 var obj = '<object width="' + vidWidth + '" height="' + vidHeight + '"><param name="movie" value="http://www.youtube.com/v/[vid]&hl=en&fs=1">' +
		 '</param><param name="allowFullScreen" value="true"></param><param ' +
		 'name="allowscriptaccess" value="always"></param><em' +
		 'bed src="http://www.youtube.com/v/[vid]&hl=en&fs=1" ' +
		 'type="application/x-shockwave-flash" allowscriptaccess="always" ' +
		 'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' +
		 vidHeight + '"></embed></object> ';

		$('.postURL a:contains("youtube.com/watch")').each(function(){
			var that = $(this);
			var vid = that.html().match(/(?:v=)([\w\-]+)/g); // end up with v=oHg5SJYRHA0
			if (vid.length) {
				$.each(vid, function(){
					$(".postURLMedia").append( obj.replace(/\[vid\]/g, this.replace('v=','')) );
					return (false);
				});
			}
		});    
		
 
	/* for ajax loading of page */
	$("a[name='displayUpdate']").live("click",function(event) {
		//alert("postdisplayupdate");
		event.preventDefault();
		var link = $(this).attr("href");
		if (link.indexOf("?") == -1) { link += '?ajax=1'; }
		else { link += '&ajax=1'; }
		$("#stage").html("<img src='loading.gif'>");
		$.getJSON(link,function(data) {
			if (data.error == null) { $("#stage").html(data.content); }
			else { showErrorDialog(data.error); }
		});
	});	
		
	$("#postFormChooseType a").live("click",function(event) {
		event.preventDefault();
		$("#postFormChooseType a").css("background","none").css("color","#000").css("fontStyle","normal");
		$(this).css("background","#000").css("color","#fff").css("fontStyle","italic");
	});
	
	$("a[name='linkToShow']").live("click",function(event) {
		event.preventDefault();
		var varHref = $(this).attr("href");
		//alert("href: "+varHref);
		var hash = varHref.substring(varHref.indexOf("#")+1);
		//alert("hash: "+hash);
		var hashElements = hash.split("/");
		var closeDivClass = hashElements[0];
		var openDivID = hashElements[1];
		$("."+closeDivClass).hide();
		$("#"+openDivID).show();
	});
		
	
	$("form[name='voteForm']").live("click",function() {
		if (!isLoggedIn()) { return false; }
		formObject = this;
		dataString = $(this).serialize();
		if (dataString.length > 0) dataString += "&";
		dataString += "ajax=1";
		$("input[type='submit']",this).attr("disabled","disabled");
		$("input[type='image']",this).attr("disabled","disabled");
		$.post("index.php",dataString,
		function(data) {
			//alert(data);
			if (data.error == null) {
				$(formObject).closest(".vote").replaceWith(data.content);
			}
			else { showErrorDialog(data.error); }
		},"json");
		return false;
	});
	
	$("a.requireLogin").live("click",function(event) {
		if (isLoggedIn()) { }
		else { 
			$(this).attr("href","?view=login"); 
		}
	});
	$("form.requireLogin").live("click",function(event) {
		if (isLoggedIn()) { }
		else { 
			window.location = "?view=login";
			event.preventDefault();
		}
	});
	$("a.dialog").live("click",function(event) {
		event.preventDefault();
		showDialog($(this).attr("href"));
	});
	$("a[name='closeNotification']").live("click",function(event) {
		event.preventDefault();
		var varHref = $(this).attr("href");
		//alert("href: "+varHref);
		var hash = varHref.substring(varHref.indexOf("#")+1);
		alert("hash: "+hash);
		$("#"+hash).hide();
		$.post("index.php",varHref.substring(varHref.indexOf("?")+1),function(data) {
			alert(data);
		});
	});
	
	$("div.voteUp").live("mouseenter",function() {
		$(this).css("background-image","url(voteUp.png)");
		$(this).siblings(".voteTally").addClass("voteTallyHighlight");
	});
	$("div.voteUp").live("mouseleave",function() {
		$(this).css("background-image","url(voteUp1.png)");
		$(this).siblings(".voteTally").removeClass("voteTallyHighlight");
	});
	$("div.voteDown").live("mouseenter",function() {
		$(this).css("background-image","url(voteDown.png)");
		$(this).siblings(".voteTally").addClass("voteTallyHighlight");
	});
	$("div.voteDown").live("mouseleave",function() {
		$(this).css("background-image","url(voteDown1.png)");
		$(this).siblings(".voteTally").removeClass("voteTallyHighlight");
	});	
	$("div.voteUpComment").live("mouseenter",function() {
		$(this).css("background-image","url(voteUpComment.png)");
		$(this).siblings(".voteTally").addClass("voteTallyHighlight");
	});
	$("div.voteUpComment").live("mouseleave",function() {
		$(this).css("background-image","url(voteUpComment1.png)");
		$(this).siblings(".voteTally").removeClass("voteTallyHighlight");
	});
	$("div.voteDownComment").live("mouseenter",function() {
		$(this).css("background-image","url(voteDownComment.png)");
		$(this).siblings(".voteTally").addClass("voteTallyHighlight");
	});
	$("div.voteDownComment").live("mouseleave",function() {
		$(this).css("background-image","url(voteDownComment1.png)");
		$(this).siblings(".voteTally").removeClass("voteTallyHighlight");
	});	
	
    $('.postURLThumbnail img').each(function() {
        var maxWidth = 70; // Max width for the image
        var maxHeight = 70;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
		//alert("height: "+height+", width: "+width);
 
 /*
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }
 */
        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }
    });	
	
	//$("body").prepend('<div id="siteNotice"><a href="mailto:hello@community.sg">We are still in beta now. Please submit any bugs and quirks to us, we appreciate it! &rarr;</a></div>');

});

function isLoggedIn() {
	if ($("#loginStatus").html() == 1) { return true; }
	else { return false; }
}

function getSiteLoginActions() {
	$.getJSON("?view=siteLoginActions&ajax=1",function(data) {
		if (data.error == null) { $("#siteLoginActions").html(data.content); }
		else { showErrorDialog(data.error); }
	});
}

function showErrorDialog(errorMsg) {
	//$("#errorMsg").html(errorMsg).dialog({modal:true});
	alert(errorMsg);
}
	
function showDialog(varURL) {
	$("#dialog").html("<img src='loading.gif'>").load(varURL+"&ajax=1").dialog({modal:true});
}
