jQuery(document).ready(function($) {
	$("ul.sf-menu").superfish({
		autoArrows: false,
		delay: 500, // one second delay on mouseout
		animation: {opacity:'toggle', height:'show'}, // fade-in and slide-down animation
		speed: 500, // faster animation speed
		autoArrows: false, // disable generation of arrow mark-up
		dropShadows: false // disable drop shadows		
	});
	
	$("#left_sidebar").appendTo("#content-wrapper");
	
	$("#grid-content").vgrid({
		easeing: "easeOutQuint",
		time: 500,
		delay: 0
	});
	
	var vg = "";
	$(window).load(function () {
		vg = $("#grid-content").vgrid({
			easeing: "easeOutQuint",
			time: 500,
			delay: 0
		});
	});
	
	
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 15, /* padding for each side of the picture */
		opacity: 0.7, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		modal: false, /* If set to true, only the close button will close the window */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){} /* Called when prettyPhoto is closed */
	});
	
	$("#advertising a:nth-child(odd)").addClass("marginright5px");

	
	$(".post_float").click(function(){
		var hreff = $(this).find("h2 a").attr("href");
		//document.location(hreff);
		window.location = hreff;
	});
	
	$("#tweets").tweet({
          join_text: "auto",
          username: twitterlogin,
          avatar_size: 48,
          count: 5,
          auto_join_text_default: "", 
          auto_join_text_ed: "",
          auto_join_text_ing: "",
          auto_join_text_reply: "",
          auto_join_text_url: "",
          loading_text: "loading tweets..."
        });
		
	$("#twitter_toggle").click(function (e) {
		$("#tweets").slideToggle(400);
		e.preventDefault();
	});
	
	$("#archives-popout").click(function (e) {
		$("#archives-popout-box").slideToggle(400);
		$("#tags-popout-box").slideUp(400);
		e.preventDefault();
	});
	$("#tags-popout").click(function (e) {
		$("#tags-popout-box").slideToggle(400);
		$("#archives-popout-box").slideUp(400);
		e.preventDefault();
	});
	$(".close-popout-box").click(function(e) {
		$("#archives-popout-box").slideUp(400);
		$("#tags-popout-box").slideUp(400);
	});
	
	$("#s").click(function () {
		if($(this).val() == "Enter Keywords"){
			$(this).val("");
		}
	});
	$("#s").blur(function () {
		if($(this).val() == ""){
			$(this).val("Enter Keywords");
		}
	});
	$("#searchsubmit").click(function (e) {
		if($("#s").val() == "Enter Keywords" || $("#s").val() == ""){
			e.preventDefault();
		}
	});
	
	//hover effect for photo/video
	if($(".post_float").length){
		if ($.browser.msie){
			$("#grid-content").delegate(".post_float","mouseover",function(e){
				$(this).children(".overlay").css("display","none");
			});
			
			$("#grid-content").delegate(".post_float","mouseout",function(e){
				$(this).children(".overlay").css("display","block");
			});
		}else{
			$(".post_float").delegate(".overlay","mouseover",function(e){
				$(this).fadeTo(300,.01);
			});
			$(".post_float").delegate(".overlay","mouseout",function(e){
				$(this).fadeTo(300,1);
			});
		}
	}
	
	//builder page effects
	if($(".builder").length){
		
		var activeBuilderID = ""; //current expanded builder div id
		$(".builder").unbind(); //disable normal vgrid linking
		$(".builder").not(".content").click(function(e){
			e.stopPropagation();
			if($(this).hasClass("active-builder")){
				//shrink back to smaller size
				$(this).animate({ height: $(this).find(".builder-small").innerHeight(), width: 255 }, 700, function(){
					$(this).find(".builder-small").fadeIn();
				});
				$(this).children(".builder-content").animate({ opacity:0 }, 300, function(){  //hide extended content
					$(this).css("display","none");
					$.doTimeout(410, function() { vg.vgsort(builderSort, "easeInOutExpo", 600, 0); }); //sort by position 
				});
				// $(this).find(".builder-position").text($(this).find(".original-builder-position").text()); //reset to old position
				$(this).removeClass("active-builder"); // no longer active
				activeBuilderID = ""; // now none active
				return; //don't worry about the rest
			}			
			
			if(activeBuilderID){
				var activeBuilder = $("#" + activeBuilderID);
				activeBuilder.removeClass("active-builder");
				// activeBuilder.find(".builder-position").text(activeBuilder.find(".original-builder-position").text());
				
				var builderSmall = activeBuilder.find(".builder-small");
				activeBuilder.delay(300).animate({ height: builderSmall.innerHeight(), width: 255 }, 700);
				
				activeBuilder.children(".builder-content").delay(300).animate({opacity:0},400,function(){
					$(this).css("display","none");
					activeBuilder.find(".builder-small").fadeIn();
				});
			}
			
			activeBuilderID  = $(this).attr("id");
			$(this).addClass("active-builder");
			
			// $(this).find(".builder-position").text("1");
			
			$.doTimeout(200,function(){
				// vg.vgsort(builderSort, "easeInOutExpo", 600, 0).delay(200).queue(function(){
					var activeBuilder = $("#" + activeBuilderID);
					var builderContent = activeBuilder.children(".builder-content").first();
					
					activeBuilder.find(".builder-small").fadeOut(100); //fade out starting content
					
					builderContent.animate({ opacity:0 }, 1, function() { //get ready to show active content
						$(this).css("display","block");
					});
					
					// activeBuilder.find(".builder-content").animate({ opacity:1 }, 300); //fade in active content
					activeBuilder.animate({width:554, height: builderContent.innerHeight() },600, function(){ //resize to new size
						$(this).css("height","auto"); 
						$.doTimeout(210, function() { vg.vgsort(builderSort, "easeInOutExpo", 600, 0); }); //sort by position 
						$(this).find(".builder-content").delay(100).animate({ opacity:1 }, 600); //fade in active content
					});
				// });
			});
		});
		
		$("#post-1172").click(function(e){
			vg.vgsort(builderSort, "easeInOutExpo", 600, 0);
		});
	}
	
	function builderSort(a, b) {
		var _a = parseInt($(a).find('.builder-position').text());
		var _b = parseInt($(b).find('.builder-position').text());
		//console.log(_a + ", " + _b);
		if(!_a) return false;
		if(!_b) return true;
		if(_a > _b) {
			//console.log("_a > _b : " + _a + ", " + _b);
			return true;
		} else {
			//console.log("_a <= _b : " + _a + ", " + _b);
			return false;
		}
	};
});
