$(window).load(function () {
	
});

$(document).ready(function(){
	
	Cufon.replace('h2, h5, h6');
	
	
	if( $(".slide-gallery-images").length >= 1 ) {
		$(".slide-gallery-images li a").colorbox()
	}
	
	// add <br class="clear" /> at the end of the <p class="news"> to fix the bottom margin caused by the floated image
	$("p.news:has(img)").append('<br class="clear" />');
	
	$("#nav > ul > li").hover(
      function () {
		if( $("#nav ul li.current").length == 1) {
			$("#nav ul li.current ul").hide()
		}

		if( $(this).find("ul").length == 1) {
			$(this).addClass("hovered").find("ul").show();
		}	
        
      }, 
      function () {
		if( $(this).find("ul").length == 1) {
			$(this).removeClass("hovered").find("ul").hide();
		}

		if( $("#nav ul li.current").length == 1) {
			$("#nav ul li.current ul").show()
		}
      }
    );

	$(".slide-gallery-images").each(function() {
		var gImages = $(this).children("li").length;
		$(this).width(gImages * 300);
	});

	$(".slide-gallery-control li a").each(function() {
		$(this).click(function() {
			var rel = $(this).attr("rel"),
				leftPos = $("." + rel).position().left;
			
			$(this).parents(".slide-gallery").find(".slide-gallery-images").animate({ left: - leftPos });
			$(this).parents(".slide-gallery").find(".slide-gallery-control li a.selected").removeClass("selected");
			$(this).addClass("selected");
			
			return false;
		});
	});
	
	
	// $(".slide-gallery-control li a").click(function() {
	// 	var rel = $(this).attr("rel");
	// 	var leftPos = $("." + rel).position().left;
	// 	// alert(leftPos)
	// 	$(".slide-gallery-images").animate({ left: - leftPos });
	// 	$(".slide-gallery-control li a.selected").removeClass("selected");
	// 	$(this).addClass("selected");
	// 	return false;
	// })
	
	
	$('#status').funkytSelect();
    
    $(".funkytSelectCaption").click(function() {
        $(this).parent().find("ul").toggle();
		return false;
    });
                
    $(".funkytSelectList a").click(function() {
		var funkytSelectId = $(this).parents("div").attr("id");
		var inputSelect = funkytSelectId.replace("-funkytSelect","");

        var valueReplace = $(this).html();
		$('#' + funkytSelectId).find(".funkytSelectCaption").html(valueReplace);
        $('#' + funkytSelectId).find("ul").hide();

		$('#' + inputSelect).val($(this).find("span.value").html())
		return false;
    });
	
	
});

$.fn.funkytSelect = function() { 
	return this.each(function() {
		var inputSelect = $(this);
		var inputSelectId = $(this).attr("id");	
		var selected = inputSelect.find("option[selected]");
		var options = $("option", inputSelect);
		
		var funkytSelectHtml = '<div id="' + inputSelectId + '-funkytSelect" class="funkytSelectHolder"></div>';	
		inputSelect.after(funkytSelectHtml);
		
		$('#' + inputSelectId + '-funkytSelect').append('<a class="funkytSelectCaption" href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a>');
		$('#' + inputSelectId + '-funkytSelect').append('<ul class="funkytSelectList"></ul>');
	
		options.each(function(){
	        $('#' + inputSelectId + '-funkytSelect ul').append('<li><a href="#">' + 
	            $(this).text() + '<span class="value">' + 
	            $(this).val() + '</span></a></li>');
	    });
	
		// inputSelect.hide();
	});
}
