﻿
var settings = {
	
		twitter: {
			selector: ".chain-twitter",
			url: "twitter.php"
		},
		selectors : [
			"#content-left-top",
			"#content-left-bottom",
			"#content-right-top",
			"#content-right-bottom"
		],
		effects : {
			"_default" : {
				_in : { type: "blind", options: {} },
				_out : {	type: "blind", options: {} }
			},
			"#content-left-top": {
				_in : { type: "blind", options: {} },
				_out : { type: "blind", options: {} }
			},
			"#content-left-bottom": {
				_in : { type: "drop", options: { /*direction: "vertical"*/ } },
				_out : { type: "drop", options: { /*direction: "vertical"*/ } }
			},
			"#content-right-top": {
				_in : { type: "clip", options: {} },
				_out : { type: "clip", options: {} }
			},
			"#content-right-bottom" : {
				_in : { type: "blind", options: {} },
				_out : { type: "blind", options: {} }
			},
			".archives-portfolio" : {
				_in : { type: "blind", options: { } },
				_out : { type: "blind", options: { } }
			},
			".archives-all" : {
				_in : { type: "blind", options: {} },
				_out : {type: "blind", options: {} }
			}
		},
		"index.php" : {
			"#content-left-top" : "piece-featured.php",
			"#content-left-bottom" : "piece-recentwork.php",
			"#content-right-top" : "piece-twitter.php",
			"#content-right-bottom" : "piece-archives-all.php"
		},
		"portfolio.php" : {
			"#content-left-top" : "piece-portfolio-slideshow.php",
			"#content-left-bottom" : "piece-portfolio-detail.php",
			"#content-right-top" : "piece-portfolio-archives.php",
			"#content-right-bottom" : ""
		},
		"about.php" : {
			"#content-left-top" : "piece-about.php",
			"#content-left-bottom" : "piece-otherventures.php",
			"#content-right-top" : "piece-twitter.php",
			"#content-right-bottom" : "piece-archives-all.php"
		},
		"contact.php" : {
			"#content-left-top" : "piece-contact.php",
			"#content-left-bottom" : "",
			"#content-right-top" : "piece-twitter.php",
			"#content-right-bottom" : "piece-archives-all.php"
		},
		needsQuery : [
			"portfolio.php",
			"piece-portfolio-slideshow.php",
			"piece-portfolio-detail.php",
			"piece-portfolio-archives.php"
		],
		nav : {
			"portfolio.php?tid=4" : ".nav-art",
			"portfolio.php?tid=3" : ".nav-identity",
			"portfolio.php?tid=2" : ".nav-music",
			"portfolio.php?tid=1" : ".nav-other",
			"about.php" : ".nav-about"
		},
		animate: ($.url.param("animate") == undefined) ? true : eval($.url.param("animate"))
	},	
	_history = [],
	_historyCount = 0;


Array.prototype.last = function(){ return this.length ? this[this.length-1] : false; }

$.bpositive = function(url){

	var f = function(url){
	
		var $this = this, selector = "";
		$.url.setUrl(url);
		var page = $.url.attr("file"), load = {}, prevLoad = _history.last(), startUrl = $.url.attr("source");
		
		if(page == null) page = "index.php";
		
		//portfolio page change - height difference
		if(page == "portfolio.php") $("body").addClass("portfolio");
		else $("body").removeClass("portfolio");
		
		//nav change
		var navSelector = settings.nav[ page + ($.url.param("tid") ? "?tid=" + $.url.param("tid") : "")];
		$(".nav").removeClass("nav-hover");
		if(navSelector) $(navSelector).addClass("nav-hover");
		
		$.each(settings.selectors, function(i,o){
			load[o] = $.url.attr("directory") + settings[page][o] + ($.inArray(settings[page][o], settings.needsQuery) > -1 ? "?" + $.url.attr("query") : "");
		});
		
		if( (url = load[ (selector = settings.selectors[0]) ]) && load[selector] != prevLoad[selector])
			$(selector).bpositive(url, settings.effects[selector], function(){
				if( (url = load[ (selector = settings.selectors[1]) ])  && load[selector] != prevLoad[selector])
					$(selector).bpositive(url, settings.effects[selector]);
			});
		else if( (url = load[ (selector = settings.selectors[1]) ])  && load[selector] != prevLoad[selector] )
			$(selector).bpositive(url, settings.effects[selector]);
			
		if( (url = load[ (selector = settings.selectors[2]) ])  && load[selector] != prevLoad[selector] )
			$(selector).bpositive(url, settings.effects[selector], function(){
				if( (url = load[ (selector = settings.selectors[3]) ])  && load[selector] != prevLoad[selector] )
					$(selector).bpositive(url, settings.effects[selector]);
			});
		else if( (url = load[ (selector = settings.selectors[3]) ])  && load[selector] != prevLoad[selector] )
			$(selector).bpositive(url, settings.effects[selector]);
			
		
		_history.push(load);
		
	}
	
	
	
	$.History.bind("h-" + _history.length, function(){	f(url); });
	$.History.setHash("h-" + _history.length, "_self");
}


$.fn.bpositive = function(url, effects, callback){
	var callback = callback || effects,
		effects = (effects) ? effects : settings.effects._default;
	var $this = this;
	callback = callback || function(){}
	
	if(settings.animate){
		//if(url == "/") $this.hide(effects._out.type, effects._out.options);
		$this.hide(effects._out.type, effects._out.options, function(){
			if(url == "" || url == "/" || url == $.url.attr("directory")) return false;
			$this
				.empty()
				.append('<div class="img img-loading"></div>')
				.show()
				.load(url, function(d){
					$this.removeClass("img img-loading").show(effects._in.type, effects._in.options, callback);
				});
		})
	} else {
		if(url == "" || url == "/" || url == $.url.attr("directory")) return $this.hide();
		$this.show().load(url, callback);;
	}
	return $this;
}

	
/** Twitter
/**/

$(settings.twitter.selector).livequery(function(){
	var $this = $(this), received = false,
		getXml = function(){
			if(received) return true;
			$.ajax({
				url: settings.twitter.url,
				success: function(data){
					received = true;
					items = [];
					$("item", data).each(function(){
						if(items.length > 2) return false;
						var diff = $.datediff(Date.parseExact( (pubDateStr = $(this).find("pubDate").text()).substr(0, pubDateStr.length-6), "ddd, d MMM yyyy HH:mm:ss").addHours(-3).toString("MM/dd/yyyy HH:mm:ss"));
						if(diff.days) (str = "day") && (amt = diff.days);
						else if(diff.hours) (str = "hour") && (amt = diff.hours);
						else if(diff.minutes) (str = "minute") && (amt = diff.minutes);
						else if(diff.seconds) (str = "second") && (amt = diff.seconds);
						if(amt > 1) str += "s";
						items.push({
							"twitter-title" : amt + " " + str + " ago via twitter",
							"twitter-description" : $(this).find("description").text()
						});
					});
					$this.items(items).chain();
					if(settings.animate) $this.show("blind");
					else $this.show();
				},
				dataType: "xml"
			});
			$this.timeout( getXml, 3000);
		};
	getXml();	
})

/** Archives: all/portfolio
/**/
 
 $(".archives-all, .archives-portfolio").livequery(function(){
 	var $this = $(this), $cols = $this.find(".archives-column"), less = $(".archives-button-less"), more = $(".archives-button-more"),
 		$hide = $(".button-hide"), $show = $(".button-show"), $thumbs = $this.find(".portfolio-archive-img"),
 		selector = ($this.is(".archives-all")) ? ".archives-all" : ".archives-portfolio", _next, _prev, _this;
 	$cols.filter(":first").show();
 	if($cols.length == 1){
 		$(".archives-less-more").hide();
 		return this;
 	}
 	more.click(function(){
 		_next = (_this = $cols.filter(":visible") ).next();
 		if( !_next.length ) return false;
 		_this.hide(250);
 		_next.show(250);
 		less.css("opacity", 1);
 		if(!_next.next().length) more.css("opacity", 0.25);
 	});
 	less.click(function(){
 		_prev = (_this = $cols.filter(":visible") ).prev();
 		if( !_prev.length ) return false;
 		_this.hide(250);
 		_prev.show(250);
 		more.css("opacity", 1);
 		if(!_prev.prev().length) less.css("opacity", 0.25);
 	}).css("opacity", 0.25);
 	$show.click(function(){
 		$thumbs.show();
 	});
 	$hide.click(function(){
 		$thumbs.hide();
 	});
 });
 
 
 /** Home featured slides
 /**/
 $(".home-featured-slides").livequery(function(){
 	var $this = $(this), $title = $(".featured-title"), $type = $(".featured-type"), $icons = $(".featured-icons a"), index;
 	if($icons.length <= 1) return $this.find("li").fadeIn(250);
 	var slideshow = $this.slideshow({
	 		width: 700,
	 		height: 350,
	 		controls: false,
	 		interval: 5000,
	 		type: "fade",
	 		change: function(index, el){
	 			$title.html( $(el).find(".featured-slide-title").html() + "&nbsp;" );
	 			$type.html( $(el).find(".featured-slide-type").html() + "&nbsp;" );
	 			$icons.removeClass("featured-state-active").eq(index).addClass("featured-state-active");
	 		}
	 	});
	 	
	$title.html( slideshow.find(".featured-slide-title:first").html() );
	$type.html( slideshow.find(".featured-slide-type:first").html() );
 	
 	$icons.click(function(){
 		$icons.removeClass("featured-state-active");
 		$(this).addClass("featured-state-active");
 		slideshow.slideshow($icons.index(this));
 	})
 	
 	
 }); 
 
 /** Portfolio
 /**/
 $(".portfolio-detail").livequery(function(){
 	var $this = $(this), $title = $(".featured-title"), $type = $(".featured-type"), $icons = $(".featured-icons a"), $thumbs = $(".portfolio-detail-thumb"), index;
 	if($icons.length <= 1) return  $(".featured-slides li").fadeIn(250);
 	var slideshow = $(".featured-slides").slideshow({
	 		width: 700,
	 		height: 350,
	 		interval: false,
	 		controls: true,
	 		type: "fade",
	 		change: function(index, el){
	 			$title.html( $(el).find(".featured-slide-title").html() + "&nbsp;" );
	 			$type.html( $(el).find(".featured-slide-type").html() + "&nbsp;" );
	 			$icons.removeClass("featured-state-active").eq(index).addClass("featured-state-active");
	 			$thumbs.removeClass("featured-state-active").eq(index).addClass("featured-state-active");
	 		}
	 	});

	$title.html( slideshow.find(".featured-slide-title:first").html() );
	$type.html( slideshow.find(".featured-slide-type:first").html() );
 	
 	$icons.click(function(){
 		$icons.removeClass("featured-state-active");
 		$(this).addClass("featured-state-active");
 		$thumbs.removeClass("featured-state-active")
 			.eq( index = $icons.index(this) ).addClass("featured-state-active");
 		slideshow.slideshow(index);
 	})
 	$thumbs.click(function(){
 		$thumbs.removeClass("featured-state-active");
 		$(this).addClass("featured-state-active");
 		$icons.removeClass("featured-state-active")
 			.eq( index = $thumbs.index(this) ).addClass("featured-state-active");
 		slideshow.slideshow(index);
 	})
 	
 	
 });
 
 /** Contact
 /**/
 
 $(".contact-form").livequery(function(){
 	var $this = $(this), $msg = $(".contact-msg");
 	$this.submit(function(){
 		$msg.removeClass("contact-msg-error");
 		var valid = $this.validate(function(msg){
	 			$msg.addClass("contact-msg-error").show().html(msg);
	 		});
	 	if(valid){
	 		$msg.html("sending...").show();
	 		$.ajax({
	 			url: "contact.php",
	 			type: "POST",
	 			data: $this.serialize(),
	 			success: function(d){
	 				$this.slideUp(250);
	 				$msg.html(d);
	 			}
	 		})
	 	}
	 	return false;
 	});
 	
 });


$(function(){

	//remove botted border
	$("a").focus(function(){ $(this).blur() });
	/*
	$.bpositive(window.location);
	
	$("[href]:not([target])").livequery("click", function(){
		var $this = $(this);
		$.bpositive("/"+$this.attr("href"));
		return false;
	});
	*/	


});
