/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

/*--------------------------------------------------------------------------*/

jQuery.url=function(){var segments={};var parsed={};var options={url:window.location,strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};var parseUri=function(){str=decodeURI(options.url);var m=options.parser[options.strictMode?"strict":"loose"].exec(str);var uri={};var i=14;while(i--){uri[options.key[i]]=m[i]||""}uri[options.q.name]={};uri[options.key[12]].replace(options.q.parser,function($0,$1,$2){if($1){uri[options.q.name][$1]=$2}});return uri};var key=function(key){if(!parsed.length){setUp()}if(key=="base"){if(parsed.port!==null&&parsed.port!==""){return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/"}else{return parsed.protocol+"://"+parsed.host+"/"}}return(parsed[key]==="")?null:parsed[key]};var param=function(item){if(!parsed.length){setUp()}return(parsed.queryKey[item]===null)?null:parsed.queryKey[item]};var setUp=function(){parsed=parseUri();getSegments()};var getSegments=function(){var p=parsed.path;segments=[];segments=parsed.path.length==1?{}:(p.charAt(p.length-1)=="/"?p.substring(1,p.length-1):path=p.substring(1)).split("/")};return{setMode:function(mode){strictMode=mode=="strict"?true:false;return this},setUrl:function(newUri){options.url=newUri===undefined?window.location:newUri;setUp();return this},segment:function(pos){if(!parsed.length){setUp()}if(pos===undefined){return segments.length}return(segments[pos]===""||segments[pos]===undefined)?null:segments[pos]},attr:key,param:param}}();

/*--------------------------------------------------------------------------*/

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

/*--------------------------------------------------------------------------*/

var PopUp = function(trigger, pop) {
	this.trigger = trigger;
	this.pop = pop;
	this.setup();
};
PopUp.prototype = {
	setup: function () {
		var self = this;
		this.trigger.click(function (ev) {
			ev.preventDefault();
			self.open();
		});
	},
	open: function () {	
		if ($('#modal_overlay')) {
			this.initModalOverlay($('#modal_overlay'));
		}
		var self = this;
		$(this.pop).center();
		$(this.pop).show();
		var closeBtn = this.pop.find('.close');
		closeBtn.click(function () {
			self.close();
		});
	},
	close: function () {
		$(this.pop).hide();
		if ($('#modal_overlay')) {
			$('#modal_overlay').hide();
		}
	},
	initModalOverlay: function (el) {
		el.css("height", $('body:first').height());
		$(el).show();
	}
};

/*--------------------------------------------------------------------------*/

var ClientPopUp = function(trigger, pop) { 
	this.trigger = trigger;
	this.pop = pop;
	this.setup();
};
ClientPopUp.prototype = {
	setup: function () {
		var self = this;
		this.trigger.click(function (ev) {
			ev.preventDefault();
			self.open();
		});
	},
	open: function () {	
		if ($('#modal_overlay')) {
			this.initModalOverlay($('#modal_overlay'));
		}
		var self = this;
		$(this.pop).center();
		$(this.pop).show();
		var closeBtn = this.pop.find('.close');
		closeBtn.click(function () {
			self.close();
		});
		this.pop.find('.content').html('');
		var clients = $('.projects .clients').html();
		this.pop.find('.content').html(clients);
	},
	close: function () {
		$(this.pop).hide();
		if ($('#modal_overlay')) {
			$('#modal_overlay').hide();
		}
	},
	initModalOverlay: function (el) {
		el.css("height", $('body:first').height());
		$(el).show();
	}
};

/*--------------------------------------------------------------------------*/

var SlideShow = function(container, slides, delay, initial){
    this.container = container;
    this.slides = slides;
    this.delay = delay;
    this.current = initial;
    this.show = null;
    this.hide = null;
    this.togglers = this.container.find('.togglers li');
    this.bullets = this.container.find('.content li');
	this.curly = this.container.find('.curly:first');
    this.setup();
};
SlideShow.prototype = {
    setup: function(){
        var self = this;
        this.showOnly($(this.slides[this.current]));
        $(this.togglers[this.current]).addClass('active');
        
        this.togglers.click(function(){
            self.pause();
            self.goTo($(this).index());
        });
        this.start();
		this.moveCurly(0);
    },
    showOnly: function(el){
        this.slides.hide();
        el.show();
    },
    goTo: function(idx){
        var self = this;
        if (idx === this.current) {
            return;
        }
		
        $(this.togglers[this.current]).removeClass('active');
        
		$(this.slides[this.current]).fadeOut(1200);
        this.current = idx;
		
		this.moveCurly(this.current);

        $(this.togglers[this.current]).addClass('active');
        $(this.slides[this.current]).fadeIn(1200);
        self.start();
    },
	moveCurly: function (idx) {
		$(this.curly).animate(
			{ top: $(this.bullets[this.current]).position()['top'] },
			{ 
				duration: 600,
				easing: 'easeOutCubic'
			}
		);
	},
    start: function(){
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 500);
    },
    pause: function(){
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        if (this.hide) {
            this.hide.cancel();
        }
        else {
            return;
        }
    },
    back: function(ev){
        if (this.show || this.hide) {
            return;
        }
        this.pause();
        if (this.current === 0) {
            this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }
        this.start();
        //ev.stop(); 
    },
    forward: function(ev){
        if (this.show || this.hide) {
            return;
        }
        this.pause();
        this.next();
        //ev.stop(); 
    },
    next: function(){
        this.timer = false;
        if (this.current === this.slides.length - 1) {
            this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
    }
};


/*--------------------------------------------------------------------------*/

var ProjectFilter = function(path) {
    this.filters = $('#sidebar li a');
	this.content = $('#project_list');
	this.path = path;
	this.loader = $('#ajax_loader');
	this.errorMsg = this.content.find('.error:first');
    this.setup();
};
ProjectFilter.prototype = {
	setup: function () {
		var self = this;
		this.cache = {};
		this.clicked = false;
		this.filters.bind('click', function (ev) {
			self.clicked = true;
			ev.preventDefault();
			var key = $(this).attr('href').split('#')[1];
			self.setActive(key);
			self.loadContent(key);
			var hash = self.getAddr($(this));
			$.address.value(hash);
		});	
		
		$.address.change(function (ev) { 
			if (!self.clicked) {			
				var query = ev.value.split("/")[1];
				if (query) {
					self.loadContent(query);
					self.setActive(query);
				} 
				else {
					self.loadContent('all');
					self.setActive('all');
				}
			}
		});
	},
	getAddr: function (el) {
		return $(el).attr('href').replace(/^#/, '');
	},
	setActive: function (k) {
		this.filters.each(function () {
			if ($(this).attr('href') == ('#' + k)) {
				$(this).addClass('active');
			}
			else {
				$(this).removeClass('active');	
			}
		});
	},
	loadContent: function (k) {
		// var fullPath = this.path + k + ".html";
		var fullPath = this.path + k + "/";
		var targetContent = $(this.content).find('.inner:first');
		$(this.content).addClass('opaque');
		targetContent.html('');
		
		if (k == 'all') {
			if (this.path == "/projects/") { 
				$('h2:first').html('All Projects');
			}
			if (this.path == "/bios/") {
				$('h2:first').html('All Bios');
			}
		}
		else {
			var initCap = (k.substr(0, 1)).toUpperCase() + (k.substr(1, k.length));
			$('h2:first').html(initCap);
		}
		
		if (!this.cache[fullPath]) {
			this.loader.show();
			$.ajax(
				{ 
					url: fullPath,
					dataType: 'html',
					context: this,
					success: function (r) {	
						this.loader.hide();
						this.errorMsg.hide();
						this.cache[fullPath] = r;
						targetContent.html(r);
						//targetContent.fadeIn(300);
						$(this.content).removeClass('opaque');
						this.addHovers();
						this.showThumbs(1000);
						new ProjectGallery();
						new ClientPopUp($('#btn_clientlist'), $('.lightbox.client_list'));
					},
					error: function () {
						this.loader.hide();
						this.errorMsg.show();
					}
				}
			);
		}
		else {
			targetContent.html(this.cache[fullPath]);
			//targetContent.fadeIn(300);
			this.errorMsg.hide();
			$(this.content).removeClass('opaque');
			this.addHovers();
			this.content.find('li').show();
			new ProjectGallery();
			new ClientPopUp($('#btn_clientlist'), $('.lightbox.client_list'));
		}
	},
	showThumbs: function (dur) {
		this.content.find('li.thumb').each(function (i) {
			var self = this;
			setTimeout(function () {
				$(self).fadeIn(dur);
			}, 250 * i);
		});	
	},
	addHovers: function () {
		$(this.content).find('li.thumb').hoverIntent({
			interval: 200,
			over: function () {
				$(this).addClass('active');
			},
			out: function () {
				$(this).removeClass('active');
			}
		});	
	}
};

/*--------------------------------------------------------------------------*/

var ProjectGallery = function () {
    this.pop = $('#gallery');
	this.thumbs = $('.projects li.thumb');
	this.prev_proj = this.pop.find('.hdr.left:first');
	this.next_proj = this.pop.find('.hdr.right:first');
	this.prev_btn = this.pop.find('.prev');
	this.next_btn = this.pop.find('.next');
	this.toggler_list = this.pop.find('.togglers ul:first');
	this.more = this.pop.find('.more:first');
	//this.togglers = this.pop.find('.togglers li');	
	this.delay = 5;
    this.setup();
};
ProjectGallery.prototype = {
    setup: function() {
		var self = this;
		
		this.thumbs.hoverIntent({
			interval: 200,
			over: function () {
				$(this).addClass('active');
			},
			out: function () {
				$(this).removeClass('active');
			}
		});	
		
		this.thumbs.bind('click', function (ev) {
			ev.preventDefault();
			self.open($(this).index());
		});
		this.pop.find('.close:first').bind('click', function () {
			self.close();
		});
		$('#btn_slideshow').bind('click', function () {
			self.open(0);
		});
	},
	open: function (idx) {
		var self = this;
		this.pop.center();
		this.initModalOverlay($('#modal_overlay'));
		
		if ($.browser.msie) {
			this.pop.show();
		}
		else {
			this.pop.fadeIn(400);
		}
		this.updatePop(idx);
		this.current_idx = idx;
		
		this.prev_btn.unbind('click');
		this.next_btn.unbind('click');
		
		this.prev_btn.bind('click', function () {
			self.updatePop(self.current_idx - 1);
			self.current_idx -= 1;
		});
		this.next_btn.bind('click', function () {
			self.updatePop(self.current_idx + 1);
			self.current_idx += 1;
		});
	},
	updatePop: function (idx) {
		var self = this;
		
		this.more.hide();
		this.prev_btn.show();
		this.next_btn.show();
		this.prev_proj.show();
		this.next_proj.show();
		
		var header = $(this.thumbs[idx]).find('strong:first').html();
		var body = $(this.thumbs[idx]).find('.body:first').html();
		var slides = $(this.thumbs[idx]).find('.photos:first').html();
		
		this.pop.find('.body').scrollTop(0);
		
		if (idx == 0) {
			this.prev_btn.hide();
			this.prev_proj.hide();
			this.prev_proj.html('');
		}
		else {
			this.prev_proj.html($(this.thumbs[idx - 1]).find('strong:first').html());
		}
		if (idx == (this.thumbs.length - 1)) {
			this.next_btn.hide();
			this.next_proj.hide();
			this.next_proj.html('');
		}
		else {
			this.next_proj.html($(this.thumbs[idx + 1]).find('strong:first').html());
		}
		
		this.pop.find('h2:first').html(header);
		this.pop.find('.body:first').html(body);
		this.pop.find('.slides:first').html(slides);
		
		this.current = 0;
		
		this.slides = this.pop.find('.slides img').hide();
		$(this.slides[0]).show();
		
		this.toggler_list.html('');
		
		
		for (i = 0; i < this.slides.length; i++) {
			var li = $('<li>Slide ' + i+1 + '</li>');
			this.toggler_list.append(li);
		}
		
		this.togglers = this.toggler_list.find('li');
		this.togglers.removeClass('active');
		$(this.togglers[this.current]).addClass('active');
        this.togglers.bind('click', function() {
            self.pause();
            self.goTo($(this).index());
        });

		this.title = this.pop.find('.togglers .title:first');
		
		if (this.slides.length > 1) {
			this.more.show();
		}
		this.title.html($(this.slides[this.current]).attr('alt'));
		
		this.start();
	},
	close: function () {
		if ($.browser.msie) {
			this.pop.hide();
		}
		else {
			this.pop.fadeOut(400);
		}		
		
//		this.togglers.removeClass('active');
		this.pop.find('h2:first').html('');
		this.pop.find('.slides:first').html('');
		this.pop.find('.body:first').html('');
		this.more.hide();
		this.pause();
		$('#modal_overlay').hide();
	},
	initModalOverlay: function (el) {
		el.css("height", $('body:first').height());
		$(el).show();
	},
    start: function(){
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1500);
    },
    goTo: function(idx){
        var self = this;
        if (idx === this.current) {
            return;
        }
		
        $(this.togglers[this.current]).removeClass('active');
        
		$(this.slides[this.current]).fadeOut(800);
        this.current = idx;
		
		this.title.html($(this.slides[this.current]).attr('alt'));
		
        $(this.togglers[this.current]).addClass('active');
        $(this.slides[this.current]).fadeIn(800);
        self.start();
    },
    pause: function(){
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        if (this.hide) {
            this.hide.cancel();
        }
        else {
            return;
        }
    },
    back: function(ev){
        if (this.show || this.hide) {
            return;
        }
        this.pause();
        if (this.current === 0) {
            this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }
        this.start();
        //ev.stop(); 
    },
    forward: function(ev){
        if (this.show || this.hide) {
            return;
        }
        this.pause();
        this.next();
        //ev.stop(); 
    },
    next: function(){
        this.timer = false;
        if (this.current === this.slides.length - 1) {
            this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
    }
};

/*--------------------------------------------------------------------------*/

var ContactUs = function () {
    this.trigger = $('#contact_link');
	this.pop = $('#contact.lightbox'); 
	this.path = "/projects/municipal/client-list";
    this.setup();
};
ContactUs.prototype = {
    setup: function() {
		var self = this;
		this.trigger.bind('click', function (ev) {
			ev.preventDefault();
			self.open();
		});
	},
	open: function () {
		var self = this;
		var iframe = $('<iframe />').attr({
			src: this.path,
			scrolling: 'no'
		});	
		this.pop.find('.content:first').html(iframe).css("visibility", "hidden");
		$(iframe).load(function () {
			$(iframe).contents().find('html:first').attr('id', 'iframe');
			self.pop.find('.content:first').css("visibility", "visible");
		});
		this.pop.center();
		this.pop.show();
		
		var close_btn = this.pop.find('.close');
		close_btn.click(function () {
			self.close();
		});
	},
	close: function () {
		this.pop.hide();
	}
};


/*--------------------------------------------------------------------------*/

$(document).ready(function () {
	if ($.browser.msie && $.browser.version=="6.0") {
		try {
			document.execCommand('BackgroundImageCache', false, true);
		} catch (e) {}
	}
	
	if ($('#hero').length) {
		new SlideShow($('#hero'), $('#hero .slide'), 5, 0);
	}
	
	if ($('.page-template-work-php').length) {
		new ProjectFilter("/projects/");	
	}
	
	if ($('.page-template-bios-php').length) {
		//new ProjectFilter("/bios/");	
		new ProjectGallery();
	}
	
	new ContactUs();
});
/*--------------------------------------------------------------------------*/
$(document).ready(function() {
  $('div.demo-show2> div').hide();
});

$(document).ready(function() {
  // ...
  $('div.demo-show2> h3').click(function() {
  // ...
  });
});

$(document).ready(function() {
  $('div.demo-show2> div').hide();
  $('div.demo-show2> h3').click(function() {
 $(this).next('div').slideToggle('fast')
 .siblings('div:visible').slideUp('fast');
  });
});
