var yaili = {};

yaili.clickDragger = function(e) {
	var that = this;
	if (window.event) {
		that = window.event.srcElement;		
	}
	yaili.event.add(document,'selectstart',yaili.event.nullifyEvent);
	yaili.event.preventDefault(e);
    //first re-order z-indexes
    var arrTemp = [];
    for (var i = 0, j = yaili.draggers.length; i<j; i++) {
        if (yaili.draggers[i] != that) {
            arrTemp.push(yaili.draggers[i]);
            yaili.draggers[i].parentNode.style.zIndex = i+1;
        }
    }

    that.parentNode.style.zIndex = yaili.draggers.length+1;
    arrTemp.push(that);
    yaili.draggers = arrTemp;
    //attach mousemove event
    yaili.currentDragger = that;
    var event = event || e;
    yaili.origMouseX = event.clientX;	
    yaili.origDraggerX = parseInt(yaili.dom.getStyle(that.parentNode,'left').replace('px',''));
    yaili.event.mousemove(document,yaili.dragDragger);
    //detach mousemove event on mouseup
    yaili.event.mouseup(document,function() {
		yaili.event.remove(document,'selectstart',yaili.event.nullifyEvent);
        yaili.event.remove(document,'mousemove',yaili.dragDragger,false);
    });
};

yaili.dragDragger = function(e) {
    var event = event || e;
    var iNewX = yaili.origDraggerX + (( event.clientX - yaili.origMouseX));
    if (iNewX <= 0) {
        iNewX = 0;
    }
    if (iNewX >= 730) {
        iNewX = 730;
    }
    yaili.currentDragger.parentNode.style.left = iNewX + 'px';
};

$(function() {
		   
	// Fixing IE's lack of support for CSS3
		   
	$('#music li:nth-child(3n)').addClass('end-row');
	$('.js #wdn.project > ul li:nth-child(3n)').addClass('end-row');
	$('div.col:nth-child(3)').css('margin-top','45px');
	$('div.col:nth-child(4)').css('margin-right',0);
	$('#peoplesay li blockquote > p:last-child').addClass('source');
	
	// Contact box
						   	
	$("a#contact").fancybox({
			'hideOnContentClick': false
		});
	
	// Work images
		
	$('div.slideshow ul.nav li a').click(function() {
		$that = $(this);
		if ($that.parent().is('.active')) {
			return false;
		};
		var idx = $that.closest('ul').find('a').index($that);

		var $slideshow = $that.closest('.slideshow');

		$slideshow.find('.images li.active').css('z-index',2);
		$slideshow.find('.images li').not('.active').css('z-index',0);
		$slideshow.find('.images li').eq(idx).css('z-index',1);
		var $oldImg = $slideshow.find('.images li.active');
		$oldImg.fadeOut(function(){
			$oldImg.css('z-index',0).show().removeClass('active');												  
		});
		$slideshow.find('.images li').eq(idx).addClass('active').css('z-index',1);
			
		$that.closest('ul').find('li').removeClass('active');
		$that.parent().addClass('active');
		return false;
	});
		
	$('div.slideshow ul.nav li:first-child a').click();
    
});

// Flickr API

function jsonFlickrApi(rsp) {
 if (rsp.stat != "ok"){
  // If this executes, something broke!
  return;
 }
 
 //variable "s" is going to contain 
 //all the markup that is generated by the loop below
 var s = "";
 
 //this loop runs through every item and creates HTML 
 for (var i=0; i < 9; i++) {
  photo = rsp.photos.photo[ i ];
  
  //notice that "t.jpg" is where you change the
  //size of the image
  t_url = "http://farm" + photo.farm + 
  ".static.flickr.com/" + photo.server + "/" + 
  photo.id + "_" + photo.secret + "_" + "s.jpg";
    
  p_url = "http://www.flickr.com/photos/" + 
  photo.owner + "/" + photo.id;
  
  s +=  '<li><a href="' + p_url + '">' + '<img alt="'+ 
  photo.title + '"src="' + t_url + '"/>' + '</a></li>';
 }

 document.writeln(s); 
 //this tells the JavaScript to write 
 //everything in variable "s" onto the page
}
