/* Init functionality ***********/

function init()
{	
	//Init site
	initFlash();
	initMedia();
	initMenus();
	
	//Corner and shadow the content
	$("#main-body").corner();
	$("#main-header-text").dropShadow({"opacity": 0.9, "top": 2, "left": 2, "blur": 0});
	
	//Bind submit events
	//Wigbi.getControl("hcc_subMenu").onSubmit = function(){ reinit(); }
	//Wigbi.getControl("hcc_page").onSubmit = function(){ reinit(); }
}

function initFlash()
{
	//MP3 links ***************
    $('a[href$=".mp3"]').flash(
        {
			wmode: 'transparent',
			src: Wigbi.webRoot() + 'swf/singlemp3player.swf',
			width: 30
		},
        { version: 7 },
        function(htmlOptions)
		{
			$this = $(this);
	        htmlOptions.width = /*$this.hasClass("nodl") ? 25 :*/ 40;
	        htmlOptions.height = 19;
			htmlOptions.flashvars.showDownload = true; //!$this.hasClass("nodl");
            htmlOptions.flashvars.file = $this.attr('href');
            $this.after($.fn.flash.transform(htmlOptions));						
        }
    );
	
	//YouTube links ***********
	$('a[href^="http://www.youtube.com"]').flash(
        {
			width: 400,
			height: 330
		},
        { version: 8 },
        function(htmlOptions)
		{
			//Init private variables
			$this = $(this);
			var width = 400;
			var height = 330;
			
			//Set proper width and height
			if ($this.hasClass("bigfront"))
			{
				width = 423;
				height = 370;
			}
			
			//Set HTML options
	        htmlOptions.width = width;
	        htmlOptions.height = height;
            htmlOptions.src = $this.attr('href');
            $this.before($.fn.flash.transform(htmlOptions));						
        }
    );	
}

function initMedia()
{
    $(".media").media();
}

function initMenus()
{
	/*$("#main-menu ul li").each(function() {
		$(this).children("a").each(function() {
			$(this).unbind("click").click(function() { return loadSection($(this).attr("href")); });
		});
	});
	
	$("#main-menu ul li").removeClass("selected");
	$("#main-menu ul li a[href$='" + section + "']").parent().addClass("selected");
	
	$("#main-subMenu ul li").each(function() {
		$(this).children("a").each(function() {
			$(this).unbind("click").click(function() { return loadPage($(this).attr("href")); });
		});
	});
	
	$("#main-subMenu ul li").removeClass("selected");
	$("#main-subMenu ul li a[href$='" + page + "']").parent().addClass("selected");*/
}

function initSlimbox()
{
	$("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el)
	{
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
}

function reinit()
{
	initFlash();
	initMedia();
	initMenus();
	initSlimbox();
}



/* Menu tabs *******************/

function MenuTab_slideIn(element)
{
	$(element).animate({ "top": "-5px"}, "fast");
}

function MenuTab_slideOut(element)
{
	$(element).animate({ "top": "-15px"}, "fast");
}


function MenuButton_slideIn(element)
{
	$(element).animate({ "paddingTop": "2px", "paddingBottom": "2px"}, "fast");
}

function MenuButton_slideOut(element)
{
	$(element).animate({ "paddingTop": "0px", "paddingBottom": "0px"}, "fast");
}



/* Comment functions ******************** */

//Define comment list function
function listComment(obj, index)
{
	//Set CSS class
	var cssClass = "";
	if (index%2)
		cssClass = " class='even'";

	//Make sure the object has an author name
	if (!obj.authorName())
		obj.setAuthorName("Anonymous");
		
	//Build list item string
	var result = obj.authorName();
	if (obj.authorEmail())
		result = "<a href='mailto:" + obj.authorEmail() + "'>" + result + "</a>";
	if (obj.authorUrl())
		result += ", <a href='" + obj.authorUrl() + "'>[web site]</a>";
	result += ", " + obj.createdDateTime(); 
	result = "<span class='small'>" + result + " wrote:</span><br/>" + obj.text();
	return "<li " + cssClass + ">" + result + "</li>"; 
}

//Comment load function
function loadComments(maxCount, groupName)
{	
	//Create a search filter for the comment list
	var commentSearchFilter = new SearchFilter();
	commentSearchFilter.setPaging(0, maxCount);
	commentSearchFilter.addSortRule("createdDateTime DESC");
	commentSearchFilter.addSearchRule("groupName = '" + groupName + "'");

	//Load all required comments
	var comments = new Comment();
	comments.search(commentSearchFilter.searchFilter(), function(result)
	{
		//Update the control
		Wigbi.getControl("commentList").refresh(result);
		
		//Update total count, if any
		if (result.length > 0)
			$("#spanNumComments").html(" (" + result[0].totalCount() + " comments)");
		else
			$("#spanNumComments").html(" (no comments)");
		
		//Show/hide the load button
		if (result.length == 0 || result.length >= result[0].totalCount())
			$("#loadDiv").hide();
		else
			$("#loadDiv").show();
	});
}
