/* COOKIE OBJECT
*************************************************************/
function cookie(domain_name) {
  //shortcut for encapsulation
  var self = this;
  
  /*
    Default Variables
  */
  self._domain = (domain_name == null) ? "" : domain_name;
  
  /*
    Getters and setters
  */
  self.setDomain = function (domain_name) {
        self._domain = domain_name;
  }
  self.getDomain =  function() {
      return self._domain;
  }
    
  /*
    initialize the download button based on the specified
    container ID
  */
  self.init = function(domain_name) {
    self._domain = (domain_name == null) ? "" : domain_name;    
  } 
  
  /*
    set a cookie c_name with value that expires in expiredays
  */
  self.setCookie = function(c_name,value,expiredays)
  {
    //function to set a cookie
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";path=/;expires="+exdate.toGMTString());
  }
  
  /*
    get the value of c_name cookie
  */
  self.getCookie = function(c_name)
  {
    //function to get a cookie
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1; 
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        } 
      }
    return "";
  }
  
  /*
    check to see if cookie cookie_name exists
  */
  self.checkCookie = function(cookie_name)
  {
    //function to check if the cookie exists
    cookie_val=self.getCookie(cookie_name);
    if (cookie_val!=null && cookie_val!="")
    {
      return true;
    }
    else 
    {
      return false;
    }
  }
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

/* EmailObfuscation
*************************************************************/
function EmailObfuscator(){
	var self = this;
	self.map = '';
	self.init = function(){
		self.map = self.rot13init();
		$("a[href^='/obfuser']").each(function(){
			var content = $(this).html();
			var href = $(this).attr('href').replace(/.*\/obfuser\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			content = (content.indexOf('..') > 0) ? self.str_rot13(href,self.map) : content;
			$(this).html(content);
			$(this).click(function(){
				self.geo_decode(this);
			});	
		});
		/*
		$("span.obfused").each(function(){
			var content = $(this).html();
			content = (content.indexOf('..') > 0) ? self.str_rot13(content,self.map) : content;
			$(this).html(content);
		});*/
	}
	
	self.geo_decode = function(anchor) { // function to recompose the orginal address
		var href = anchor.getAttribute('href');
		var address = href.replace(/.*\/obfuser\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
		var linktext = anchor.innerHTML; // IE Fix
		if (href != address) {
			anchor.setAttribute('href','mailto:' + self.str_rot13(address,self.map)); // Add mailto link	
			anchor.innerHTML = linktext; // IE Fix
		}
	}
	
	self.rot13init = function() {
		var map = new Array();
		var s = "abcdefghijklmnopqrstuvwxyz";
		for (var i = 0 ; i < s.length ; i++)
			map[s.charAt(i)] = s.charAt((i+13)%26);
		for (var i = 0 ; i < s.length ; i++)
			map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
		return map;
	}

	self.str_rot13 = function(a,map) {
		var s = "";
		for (var i = 0 ; i < a.length ; i++) {
			var b = a.charAt(i);
			s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
		}
		return s;
	}
	
	self.init();
}

/* MENU FUNCTIONS
*************************************************************/
function HomeSlideShow(){
	var self = this;
	self.timer = null;
	self.init = function(){
		if($('.entry_shell .entry_images').length > 0){
			self.setup_slideshows();
		}
	}
	
	self.setup_slideshows = function(){
		$('.entry_shell .entry_images').each(function(){
			var tmp = new SlideShowPlayer($(this));
		});
	}
	
	self.init();
}

function SlideShowPlayer(container_obj){
	var self = this;
	self.slide_container = null;
	self.current_slide = 0;
	self.next_slide = 0;
	self.total_slides = 0;
	self.startTiming = 3000;
	self.timing = 5000;
	self.fadeTiming = 500;
	self.heightTiming = 500;
	self.timeout = null;
	
	self.init = function(slide_container){
		self.slide_container = slide_container;
		self.total_slides = $(self.slide_container).children('img').length;
		if(self.total_slides > 0){
			tmp_height =  parseInt($(self.slide_container).children('img:eq(0)').height());
			$(self.slide_container).css('height',tmp_height + 'px');
		}
		if(self.total_slides > 1){
			$(self.slide_container).children('img:gt(0)').hide();
			self.current_slide = 0;
			self.next_slide = 1;
			self.total_slides = self.total_slides - 1;
			self.timeout = setTimeout(function(){self.transition_slide();}, self.startTiming);
		}
	}
	
	self.transition_slide = function(){
		var prev_slide = self.current_slide;
		self.current_slide = self.next_slide;
		self.next_slide = (self.current_slide + 1 > self.total_slides) ? 0 : self.current_slide + 1;
		
		$(self.slide_container).children('img:eq('+ prev_slide + ')').css('z-index','1');
		$(self.slide_container).children('img:eq('+ self.current_slide + ')').css('z-index','2');
		
		var tmp_height =  parseInt($(self.slide_container).children('img:eq('+ self.current_slide + ')').height());
		var container_height = parseInt($(self.slide_container).height());
		if(container_height != tmp_height){
			$(self.slide_container).animate({
				height: tmp_height + 'px'
			}, self.heightTiming, 'easeOutQuad',function(){
				$(self.slide_container).children('img:eq('+ self.current_slide + ')').fadeIn(self.fadeTiming, function(){
					$(self.slide_container).children('img:eq('+ prev_slide + ')').hide();
				});
			});
		} else {
			$(self.slide_container).children('img:eq('+ self.current_slide + ')').fadeIn(self.fadeTiming, function(){
				$(self.slide_container).children('img:eq('+ prev_slide + ')').hide();
			});
		}
		self.timeout = setTimeout(function(){self.transition_slide();}, self.timing);
	}
	
	self.init(container_obj);
}

/* MENU FUNCTIONS
*************************************************************/
function menu() {
  //shortcut for encapsulation
  var self = this;
  self.timer= null;

  self.init = function(){
		$("#nav_menu .menu").each(function(){
			$(this).mouseover(function(){
				menuObj.hide_all();
				$(this).addClass('active');
				clearTimeout(self.timer);
				$(this).parent().children('ul').show();
			});
			$(this).mouseout(function(){
				self.timer = setTimeout("menuObj.hide_all()", 500);
			});
			if($(this).attr("href") == "#"){
				$(this).css('cursor','pointer').click(function(){
					return false;
				});
			}
		});
		$("#nav_menu ul").each(function(){
			$(this).mouseover(function(){
				clearTimeout(self.timer);
			});
			$(this).mouseout(function(){
				self.timer = setTimeout("menuObj.hide_all()", 500);
			});
		});
	}
  
  self.hide_all = function(){
		$('#nav_menu .active').removeClass('active');
		$('#nav_menu ul').hide();
  }
	
  self.init();
}


/* FORM HELPER
*************************************************************/

function formHelper() {
	var self = this;
	
	self.radio_height = "12px";
	self.radio_padding_left = "13px";
	self.background_box = "/_img/sprites/bg_ffffff.gif";
	self.background_pos_start = "-4px -38px";
	self.background_pos_hover = "-4px -20px";
	self.background_pos_end = "-4px -2px";
	self.cursor = "pointer";
	
	self.init = function(){
		if($("input[type='radio']").length > 0){
			self.init_radio();
		}
		if($("input[type='checkbox']").length > 0){
			self.init_checkbox();
		}
		
	}
	
	self.init_checkbox = function() {
		//hide the checkbox buttons
		$("input[type='checkbox']").hide();
		
		//style up the labels
		$("input[type='checkbox']").parent().css('paddingLeft', self.radio_padding_left);
		$("input[type='checkbox']").parent().css('cursor', self.cursor);
		$("input[type='checkbox']").parent().css('height', self.radio_height);
		$("input[type='checkbox']").parent().css('background', 'url(' + self.background_box + ') ' + self.background_pos_start + ' no-repeat');
		
		if($("input[type='checkbox']:checked").length > 0){
			$("input[type='checkbox']:checked").parent().css('backgroundPosition', self.background_pos_end);
		}
			
		//Create the mouseover function
		$("input[type='checkbox']").parent().mouseover(function() {
			if($(this).children('input:checked').length == 0){
				$(this).css('backgroundPosition', self.background_pos_hover);
			}
		});

		//Create the mouseout function
		$("input[type='checkbox']").parent().mouseout(function() {
			if($(this).children('input:checked').length > 0){
				$(this).css('backgroundPosition', self.background_pos_end);
			} else {
				$(this).css('backgroundPosition', self.background_pos_start);
			}
		});

		//hijack the click event
		$("input[type='checkbox']").parent().unbind("click");
		$("input[type='checkbox']").parent().click(function() {		
			//figure out which background position to use
			var tmp_pos = ($(this).css('backgroundPosition') == self.background_pos_end) ? self.background_pos_start : self.background_pos_end;
			
			//setup the radio button to be checked
			if($(this).children('input:checked').length == 0){				
				$(this).children('input').attr('checked','checked');
				//setup the new background for the parent label
				$(this).css('backgroundPosition', tmp_pos);
				
			} else {		
				$(this).children('input').attr('checked','');
				$(this).parent().css('backgroundPosition', self.background_pos_start);
			}
			return false;
		});
	}
	
	self.reset_radios = function (n){
		$("input[type='radio'][name='"+ n +"']").each(function(){
			$(this).parent().css('backgroundPosition', self.background_pos_start);
		});
	}
		
	self.init_radio = function() {
		//hide the radio buttons
		$("input[type='radio']").hide();
		
		//style up the labels
		$("input[type='radio']").parent().css('paddingLeft', self.radio_padding_left);
		$("input[type='radio']").parent().css('cursor', self.cursor);
		$("input[type='radio']").parent().css('height', self.radio_height);
		$("input[type='radio']").parent().css('background', 'url(' + self.background_box + ') ' + self.background_pos_start + ' no-repeat');
		
		if($("input[type='radio']:checked").length > 0){
			$("input[type='radio']:checked").parent().css('backgroundPosition', self.background_pos_end);
		}
		
		//Create the mouseover function
		$("input[type='radio']").parent().mouseover(function() {
			if($(this).children('input:checked').length == 0){
				$(this).css('backgroundPosition', self.background_pos_hover);
			} else {
				$(this).css('backgroundPosition', self.background_pos_end);
			}
		});

		//Create the mouseout function
		$("input[type='radio']").parent().mouseout(function() {
			if($(this).children('input:checked').length > 0){
				$(this).css('background', 'url(' + self.background_box + ') ' + self.background_pos_end + ' no-repeat');
			} else {
				$(this).css('background', 'url(' + self.background_box + ') ' + self.background_pos_start + ' no-repeat');
			}
		
		});

		//hijack the click event
		$("input[type='radio']").parent().unbind("click");
		$("input[type='radio']").parent().click(function() {		
			//figure out which background position to use
			var tmp_pos = ($(this).css('backgroundPosition') == self.background_pos_end) ? self.background_pos_start : self.background_pos_end;
			
			//setup the radio button to be checked
			if($(this).children('input:checked').length == 0){
				self.reset_radios($(this).children("input[type='radio']:eq(0)").attr('name')); //reset only the radio buttons that have the same name as the one that was clicked.
				
				$(this).children('input').attr('checked','checked');
				//setup the new background for the parent label
				$(this).css('background', 'url(' + self.background_box + ') ' + tmp_pos + ' no-repeat');
				
			}
			return false;
		});
	}
	
	self.init();
}
		
/* FUNCTION MORE AND LESS FUNCTIONALITY
*************************************************************/
function subNav() {
	self = this;
	
	self.init = function() {
		if($(".subnav").length>0) {
			$(".subnav a").each(function(){
				$(this).prepend('<span></span>');
			});
		}
	}
	self.init();
}

/* INFO
*************************************************************/
function info() {
	var self = this;
	
	self.init = function(id_to_show) {		
		self.init_hide();
		if(id_to_show != null){
			$("a." + id_to_show).addClass('active');
			$("#" + id_to_show).show();
		}
		$("#project_options_icons a").unbind('click').click(function(){
			//$("#at15s").hide();
			$("#project_options_icons a").removeClass('active');
			self.init_show($(this).attr("class"));
			return false;
		});
		/*
		$("#project_options_icons a.share_details").unbind('click').click(function(){
			$("#project_options_icons a").removeClass('active');
			self.init_show($(this).attr("class"));
			//addthis_open(this, '', '[URL]', '[TITLE]');
			return false;
		});
		*/
		self.init_ratings();
	}
	self.init_ratings = function(){
		$("#ratings").show();
		
		selected_rating = ($("#ratings input[type='radio']:checked").length > 0) ? $("#ratings input[type='radio']:checked").val() : "5/10";
		$("#ratings label").remove();
		
		$("#ratings").append("<p>Overall: <input type='text' name='rating' id='rating_txt' value='" + selected_rating + "' /></p>");
		$("#ratings").append("<div id='rating-slider' class='ui-slider-horizontal ui-widget ui-widget-content ui-corner-all'></div>");
		$('#rating-slider').show();
		$("#rating-slider").slider({
			value: selected_rating.split("/")[0],
			range:'min',
			min: 1,
			max: 10,
			slide: function(event, ui) {				
				$(".ui-slider-handle:eq(0)").html(ui.value +"/10");
				$("#rating_txt").val(ui.value +"/10");
			}
		});
		$("#rating-slider .ui-slider-handle:eq(0)").html(selected_rating);
	}
	self.init_hide = function() {
		$("#project_options div[id$='_details']").hide();	
		$("#project_options a.active").removeClass('active');
	}
	self.init_show = function(passed) {
		self.init_hide();
		$("#project_options a."+passed).addClass('active');
		$("#" + passed).fadeIn();
	}
}

function thumbnails() {
	var self = this;
	
	self.init = function() {
		$("#project_thumbnails a").unbind('click').click(function(){
			self.init_show($(this).attr("class"));
			return false;
		});
	}
	self.init_hide = function() {
		$("#middle p").hide();
	}
	self.init_show = function(passed) {
		self.init_hide();
		$("#" + passed).fadeIn();
	}
	self.init();
}

function workSlides() {
	var self = this;
	self.total_slides = 0;
	self.current_slide = 1;
	self.prev_slide = 1;
	self.info_label = null;
	
	self.show_next = function() {	
		if(self.current_slide == 1 || self.current_slide < self.total_slides){
			self.prev_slide = self.current_slide;
			self.current_slide++;
			if(self.current_slide > 1 && self.total_slides > 1){
				$("#work_nav_links .prev_nav").removeClass('disabled');
			}
			
			if(self.current_slide >= self.total_slides){
				$("#work_nav_links .next_nav").removeClass('disabled');
				$("#work_nav_links .next_nav").addClass('disabled');
				self.current_slide = self.total_slides;
			}
//			$(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").css('zIndex',1)
//			$(".work_assets span.work_img:eq(" + (parseInt(self.current_slide) - 1) + ")").css('zIndex',2).fadeIn(500,function(){ $(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").hide(); });
			$(".work_assets span.work_img:eq(" + (parseInt(self.current_slide) - 1) + ")").fadeIn(500);
			$(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").fadeOut(500);
			
			$("#work_nav_links span").html(self.current_slide + " / " + self.total_slides);
		}
	}
	
	self.show_prev = function() {	
		if(self.current_slide >1){
			self.prev_slide = self.current_slide;
			self.current_slide--;
			if(self.current_slide <= 1 && self.total_slides > 1){
				$("#work_nav_links .prev_nav").removeClass('disabled');
				$("#work_nav_links .prev_nav").addClass('disabled');
				self.current_slide = 1;
			}
			$(".work_assets span.work_img:eq(" + (parseInt(self.current_slide) - 1) + ")").fadeIn(500);
			$(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").fadeOut(500);
			
			//$(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").css('zIndex',1)
			//$(".work_assets span.work_img:eq(" + (parseInt(self.current_slide) - 1) + ")").css('zIndex',2).fadeIn(500,function(){ $(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").hide(); });
			/*
			$(".work_assets span.work_img:eq(" + (parseInt(self.prev_slide) - 1) + ")").hide();
			$(".work_assets span.work_img:eq(" + (parseInt(self.current_slide) - 1) + ")").show();
			*/
			if(self.current_slide < self.total_slides){
				$("#work_nav_links .next_nav").removeClass('disabled');
			}
			
			$("#work_nav_links span").html(self.current_slide + " / " + self.total_slides);
		}
	}
	
	self.init = function() {
		self.total_slides = $(".work_assets span.work_img").length;
		
		if(parseInt(self.total_slides) > 1){
			$("#work .middle").css('width', $(".work_assets").width() + "px");
			$("#work_nav_links .prev_nav").addClass('disabled');
			$("#work_nav_links .next_nav").removeClass('disabled');
			$(".work_assets span.work_img").css('position','absolute');
			$(".work_assets span.work_img").css('left','0');
			//$(".work_assets span.work_img:eq(0)").css('left','0');
			//$(".work_assets span.work_img:gt(0)").css('left',$(".work_assets span.work_img:eq(0)").width()+"px");
		}
		
		$("#work_nav_links .prev_nav").unbind('click').click(function(){
			self.show_prev();
			return false;
		});
		$("#work_nav_links .next_nav").unbind('click').click(function(){
			self.show_next();
			return false;
		});
	}
	
	self.init();
}



/* IMAGE ROTATOR
*************************************************************/
function rotateImage() {
	var self = this;
	
    self.current_image = 0;
    self.num_images = 0;
    self.image_container = "";
    self.interval = null;
	self.do_ani= false;
	self.timeout= 5000;
	self.fadeSpeed='slow';
	self.objNam = 'self';
	
    self.init = function(id, timing, name){
		if(id != null){
			self.timeout = (timing != null)? timing : self.timeout;			
			self.objNam = (name != null) ? name : self.objNam;
			
			self.image_container = id;
			if($("#"+self.image_container+" img").length > 1){
				self.num_images = $("#"+self.image_container+" img").length;
				self.setup();
			}
		}
    };
	
    self.setup = function(){
        $("#"+self.image_container+" img").hide();
        $("#"+self.image_container+" img:eq(0)").show();
		self.do_ani = true;
        self.interval = setTimeout(self.objNam + ".show_image()",self.timeout);
    };
	
    self.show_image = function (){
        next_image = self.current_image + 1;
        prev_image = self.current_image;
        if(next_image >= self.num_images){
            next_image = 0;
        }
        if(prev_image < 0){
            prev_image = (self.num_images-1);
        }
        self.current_image = next_image;
        
        $("#"+self.image_container+" img:eq("+next_image+")").css("z-index","1002").fadeIn(self.fadeSpeed, function(){
			if(self.do_ani == true){
				self.interval = setTimeout(self.objNam + ".show_image()",self.timeout);
				$("#"+self.image_container+" img:eq("+prev_image+")").css("z-index","1000").hide();
				$("#"+self.image_container+" img:eq("+next_image+")").css("z-index","1000")
			}
        });
    };
	
    self.stop_animation= function (){
		self.do_ani = false;
		clearTimeout(self.interval);
	};
}


/* FUNCTION ARCHIVE POP-OVERLAY
*************************************************************/
var rotateImageObj = null;
function archiveOverlay() {
	var self = this;
	self.rotateImageObj = null;
	
	self.init = function(){
		if($("div.entry_row div.entry_shell.container").children("div.entry_slides").length > 0){
			$("div.entry_row div.entry_shell.container a").unbind("mouseover").mouseover(function(){
				$(this).parent().children("div.entry_slides").show();
				$(this).parent().children("div.entry_slides").attr('id','archive_overlay');
				rotateImageObj = new rotateImage();
				rotateImageObj.init("archive_overlay",1000, "rotateImageObj");
			});
			$("div.entry_row div.entry_shell.container a").unbind("mouseout").mouseout(function(){
				$("#archive_overlay").hide();
				$("#archive_overlay").attr('id','');
				rotateImageObj.stop_animation();
				rotateImageObj = null;
			});
		}
	}
}

/* ROTATE HAND
*************************************************************/

function rotateHand(rating){
	var self = this;
	self.rotate_amt = 0;
	self.img_ref = "review_hand";
	self.rotation_obj = null;
	
	self.init = function(rating){
		if($.browser.name == 'msie' && $.browser.versionNumber == 6){
			$("#review_hand").replaceWith("<img src='" + $("#review_hand").attr('src').replace(".png",".gif") + "' id='review_hand'/>");
		}
		
		if(rating != null){
			self.rotate_amt = (Math.abs(rating-10) / 10) * 180;
		}
		self.rotation_obj = $("#" + self.img_ref).rotate({maxAngle:180,minAngle:0});
		
		setTimeout("rotateHandObj.animate()", 500);
		//$("#" + self.img_ref).rotateAnimation(self.rotate_amt);
	}
	
	self.animate = function(){
		self.rotation_obj.rotateAnimation(self.rotate_amt);
	}
	
	self.init((rating*10));
}


/* FORM HELPER
*************************************************************/

function WorkHelper() {
	var self = this;
	
	self.selected_client = "";
	self.selected_dates = "";
	self.selected_media = "";
	self.recent_hash = "";
	self.link_timer = "";
	self.current_filters = "";
	self.current_year = "";
	self.radio_height = "12px";
	self.radio_padding_left = "13px";
	self.background_box = "/_img/sprites/bg_ffffff.gif";
	self.background_pos_start = "-3px -38px";
	self.background_pos_hover = "-3px -20px";
	self.background_pos_end = "-3px -2px";
	self.cursor = "pointer";
	self.base_archive_url="/our-work/archives";
	self.postRequest=null;
	
	self.init = function(base_url,current_year, start_year, end_year){
		self.base_archive_url = base_url;
		self.current_year = current_year;
		$('#slider-range').show();
		$("#slider-range").slider({
			range: true,
			min: 1999,
			max: current_year,
			values: [start_year,end_year],
			slide: function(event, ui) {
				$(".ui-slider-handle:eq(0)").html(ui.values[0].toString().substring(2));
				$(".ui-slider-handle:eq(1)").html(ui.values[1].toString().substring(2));
				$("#year_range").val(ui.values[0] + '-' + ui.values[1]);
				self.change_url();
			}
		});
		
		$(".ui-slider-handle:eq(0)").html($("#slider-range").slider("values", 0).toString().substring(2) );
		$(".ui-slider-handle:eq(1)").html($("#slider-range").slider("values", 1).toString().substring(2) );
		$("#year_range").val($("#slider-range").slider("values", 0) + '-' + $("#slider-range").slider("values", 1));
		self.setup_form();
		setTimeout("workHelperObj.init_url()", 100);
	}
	
	self.init_url = function(){
		if(location.href.split("#").length > 1){
			tmp_hash = location.href.split("#");
			filters = tmp_hash[1].split("/");
			self.selected_client =Url.decode(Url.decode(filters[0]));
			self.selected_dates =filters[1];
			self.selected_media =Url.decode(Url.decode(filters[2]));
			self.current_filters = tmp_hash[1];
			
			self.update_form();
			self.update_entries();
		}
	}
	
	self.update_form = function(){
		dates = self.selected_dates.split("-");
		start_year = dates[0];
		end_year = dates[1];
		media_types = self.selected_media.split("|");
		
		
		$("#slider-range").slider('destroy');
		$("#slider-range").slider({
			range: true,
			min: 1999,
			max: self.current_year,
			values: [start_year,end_year],
			slide: function(event, ui) {
				$(".ui-slider-handle:eq(0)").html(ui.values[0].toString().substring(2));
				$(".ui-slider-handle:eq(1)").html(ui.values[1].toString().substring(2));
				$("#year_range").val(ui.values[0] + '-' + ui.values[1]);
				self.change_url();
			}
		});
		
		$(".ui-slider-handle:eq(0)").html($("#slider-range").slider("values", 0).toString().substring(2) );
		$(".ui-slider-handle:eq(1)").html($("#slider-range").slider("values", 1).toString().substring(2) );
		$("#year_range").val($("#slider-range").slider("values", 0) + '-' + $("#slider-range").slider("values", 1));
		
		$("#client_name option:selected").attr('selected','');
		
		if(self.selected_client.toLowerCase() == 'all'){
			$("#client_name option").each(function(){
				if($(this).text().toLowerCase() == "all clients"){
					$(this).attr('selected','selected');
				}
			});

		} else {
			$("#client_name option").each(function(){
				if($(this).text().toLowerCase() == self.selected_client.toLowerCase()){
					$(this).attr('selected','selected');
				}
			});
		}
		
		$("div.filter.media input[type='checkbox']:checked").attr("checked","");
		$("div.filter.media input[type='checkbox']").parent().css('backgroundPosition', self.background_pos_start);
		if(self.selected_media.split("|").length > 1) {
			for(x=0;x<media_types.length;x++){
				media_type = media_types[x];
				$("div.filter.media input[type='checkbox']").each(function(){
					if($(this).parent().text().toLowerCase() == media_type){
						$(this).attr('checked','checked');
					}				
				});
			}
		} else {
			$("div.filter.media input[type='checkbox']").each(function(){
				if($(this).parent().text().toLowerCase() == self.selected_media){
					$(this).attr('checked','checked');
				}
			});
		}
		if($("div.filter.media input[type='checkbox'][name^='media']:checked").length == $("div.filter.media input[type='checkbox'][name^='media']").length){
			$("#all_check").attr('checked','checked');
		} else {
			$("#all_check").attr('checked','');
		}
		$("div.filter.media input[type='checkbox']:checked").parent().css('backgroundPosition', self.background_pos_end);
	}
	
	self.reset_filters = function(){	
		//reset the sliders
		/*
		$("#slider-range").slider("values", 0,1999);
		$("#slider-range").slider("values", 1,self.current_year);
		$(".ui-slider-handle:eq(0)").html($("#slider-range").slider("values", 0).toString().substring(2) );
		$(".ui-slider-handle:eq(1)").html($("#slider-range").slider("values", 1).toString().substring(2) );
		$("#year_range").val(1999 + '-' + self.current_year);
		*/
		//reset the media filters
		$("div.filter.media input[type='checkbox']").parent().each(function(){
			$(this).children('input').attr('checked','checked');
			//setup the new background for the parent label
			$(this).css('backgroundPosition', self.background_pos_end);
		});
		
	}
	
	self.setup_form = function(){
		$("#btn_submit").hide();
		$("#year_range").hide();
		$("#filter_form").submit(function(){
			return false;
		});
		$("#client_name").change(function(){
			self.reset_filters();
			self.change_url();
			return false;
		});
		self.init_checkbox();
	}
	
	
	self.init_checkbox = function() {
		//hide the checkbox buttons
		$("input[type='checkbox']").hide();
		
		//style up the labels
		$("input[type='checkbox']").parent().css('paddingLeft', self.radio_padding_left);
		$("input[type='checkbox']").parent().css('cursor', self.cursor);
		$("input[type='checkbox']").parent().css('height', self.radio_height);
		$("input[type='checkbox']").parent().css('background', 'url(' + self.background_box + ') ' + self.background_pos_start + ' no-repeat');
		
		if($("input[type='checkbox']:checked").length > 0){
			
			if($("div.filter.media input[type='checkbox'][name^='media']:checked").length == $("div.filter.media input[type='checkbox'][name^='media']").length){
				$("#all_check").attr('checked','checked');
			} else {
				$("#all_check").attr('checked','');
			}
			$("input[type='checkbox']:checked").parent().css('backgroundPosition', self.background_pos_end);
		}
			
		//Create the mouseover function
		$("input[type='checkbox']").parent().mouseover(function() {
			
			if($(this).children('input:checked').length == 0){
				$(this).css('backgroundPosition', self.background_pos_hover);
			}
			
		
		});

		//Create the mouseout function
		$("input[type='checkbox']").parent().mouseout(function() {
			
			if($(this).children('input:checked').length > 0){
				$(this).css('backgroundPosition', self.background_pos_end);
			} else {
				$(this).css('backgroundPosition', self.background_pos_start);
			}
			
		
		});

		//hijack the click event
		$("input[type='checkbox']").parent().unbind("click");
		$("input[type='checkbox']").parent().click(function() {		
			//figure out which background position to use
			var tmp_pos = ($(this).css('backgroundPosition') == self.background_pos_end) ? self.background_pos_start : self.background_pos_end;
			
			if($("input[type='checkbox']:checked").length == $("input[type='checkbox']").length){
				$("div.filter.media input[type='checkbox'][name^='media']").parent().each(function(){
					$(this).children('input').attr('checked','');
					//setup the new background for the parent label
					$(this).css('backgroundPosition', self.background_pos_start);
				});
			}
			
			//setup the radio button to be checked
			if($(this).children('input:checked').length == 0){				
				$(this).children('input').attr('checked','checked');
				//setup the new background for the parent label
				$(this).css('backgroundPosition', tmp_pos);
				
			} else {		
				$(this).children('input').attr('checked','');
				$(this).parent().css('backgroundPosition', self.background_pos_start);
			}
			
			if($("div.filter.media input[type='checkbox'][name^='media']:checked").length == $("div.filter.media input[type='checkbox'][name^='media']").length){
				$("#all_check").attr('checked','checked');
				$("#all_check").parent().css('backgroundPosition', self.background_pos_end);
			} else {
				$("#all_check").attr('checked','');
				$("#all_check").parent().css('backgroundPosition', self.background_pos_start);
			}
			self.change_url();
			return false;
		});
		
		$("#all_check").parent().unbind("click");
		$("#all_check").parent().click(function() {				
			//setup the radio button to be checked
			if($(this).children('input:checked').length == 0){				
				$(this).children('input').attr('checked','checked');
				//setup the new background for the parent label
				$(this).css('backgroundPosition', self.background_pos_end);
				
				$("div.filter.media input[type='checkbox'][name^='media']").parent().each(function(){
					$(this).children('input').attr('checked','checked');
					//setup the new background for the parent label
					$(this).css('backgroundPosition', self.background_pos_end);
				});
				
				self.change_url();
				/*
			} else {		
				$(this).children('input').attr('checked','');
				$(this).parent().css('backgroundPosition', self.background_pos_start);
				
				$("div.filter.media input[type='checkbox'][name^='media']").parent().each(function(){
					$(this).children('input').attr('checked','');
					//setup the new background for the parent label
					$(this).css('backgroundPosition', self.background_pos_start);
				});
				*/
			}
			
			return false;
		});
	}
	
	self.change_url = function(){
		self.hide_loader();
		self.selected_client = ($("#client_name option:selected").val().length > 1) ? $("#client_name option:selected").text().toLowerCase().replace(/\s/g, "-") : "all";
		self.selected_dates = $("#year_range").val();
		
		self.selected_media = "";
		$("div.filter.media input[type='checkbox'][name^='media']:checked").each(function(){
			self.selected_media += (self.selected_media.length > 0) ? "|" + $(this).parent().text().toLowerCase().replace(/\s/g, "-") : $(this).parent().text().toLowerCase().replace(/\s/g, "-");
		});
		
		self.current_filters = self.selected_client + "/" + self.selected_dates + "/" + self.selected_media + "/";
		$.history.add(self.current_filters);
		
		clearTimeout(self.link_timer);
		self.link_timer = setTimeout("workHelperObj.check_url()", 100);
	}
	
	self.check_url = function(){
		if($.history.getCurrent().length>0){ //check if the current history item has anything in it
			if($.history.getCurrent() != self.recent_hash){
				self.recent_hash = $.history.getCurrent(); //get the new hash
				self.show_loader();
				self.update_entries();//move the navigation to the right nav item
			}
		}
	}
	
	self.show_loader = function(){
		$('body').append("<div id='loader'><img src='/_img/layout/loader.gif' height='75' width='75' alt='Loading...' /></div>");
		$('#loader').dialog({
			bgiframe: true,
			height: 100,
			width: 150,
			modal: true,
			movable: false,
			resizable: false
		});

	}
	
	self.hide_loader = function(){
		$('#loader').dialog('destroy');
		$('#loader').remove();
	}
	
	self.update_entries = function(){
		if(self.postRequest){
			self.postRequest.abort();
		}
		self.postRequest = $.post("/"+self.base_archive_url+Url.encode(Url.encode(self.current_filters)),"&dyn=1",function(data){
			if(data.length>0){
				$("#work_entries").html(data);
				self.hide_loader();
			}
		});
	}
}


/* RETHINK QUESTIONAIRE
*************************************************************/

function divOpenClose() {
	var self = this;
	self.current_height = 0;
	self.tallest_div = 0;
	self.init = function(){		
		$('.open_div').parent().next().hide();
		$('.open_div').parent().next().addClass('opendiv_content');
		
		$('.opendiv_content').each(function(){
			self.tallest_div = (parseInt($(this).height()) > self.tallest_div) ? parseInt($(this).height()) : self.tallest_div;
		});
		self.current_height = parseInt($('.middle').height()) + self.tallest_div;
		if(self.current_height > parseInt($(document).height())){
			$("#content_stage").css('height', self.current_height + 'px');
		}
		
		$('.open_div').unbind('click').click(function(){
			self.openDiv($(this));
			return false;
		});
		
		$('.true').unbind('click').click(function(){
			self.closeDivs();
			$(this).parent().children('.active').removeClass('active');
			$(this).addClass('active');	
			return false;
		});
		$('.false').unbind('click').click(function(){
			self.closeDivs();
			$(this).parent().children('.active').removeClass('active');	
			$(this).addClass('active');
			return false;
		});
	}
	
	self.openDiv = function(obj){
		
		if($(obj).parent().next('div').css('display') == 'none'){
			self.closeDivs();
			$(obj).parent().next('div').slideDown(150);
			$(obj).addClass('active');
		} else {
			self.closeDivs();
		}
		if($('.toronto').length > 0){
			initTOMap();
		}
	}
	self.closeDivs = function(){
		$('.open_div.active').removeClass('active');
		$('.opendiv_content').hide();
	}
	
	self.init();
}
/* OVERLAY MEDIA
*************************************************************/
function overlayMedia() {
	var self = this;
	self.override_height = 0;
	self.override_width = 0;
	self.total_items = 0;
	
	self.init = function(){
		$("a.overlay").each(function(){
			$(this).unbind('click').click(function(){
				self.override_height = ($(this).metadata().height) ? parseInt($(this).metadata().height) : 0;
				self.override_width = ($(this).metadata().width) ? parseInt($(this).metadata().width) : 0;
				self.setup_overlay($(this));
				return false;
			});
		});
		$("a.setup_overlay").unbind('click').click(function(){
			self.setup_overlay($('#overlay_items #overlay_item_0'));
		});
	}
	
	self.setup_overlay = function(obj){
		var href = ($(obj).attr('href')) ? $(obj).attr('href') : "1.gif";
		var isImage = ( href.indexOf(".PNG") > 0 || href.indexOf(".png") > 0 || href.indexOf(".jpg") > 0 || href.indexOf(".JPG") > 0 || href.indexOf(".gif") > 0 || href.indexOf(".GIF") > 0 || href.indexOf(".pdf") > 0);
		var isSound = ( href.indexOf(".mp3") > 0);
		
		if(!isImage){	
			if($("#featured_media").length > 0 ){
				$("#featured_media").dialog("destroy");
				$("#featured_media").remove();
			}
			
			var img_height = ($(obj).children('img').length > 0) ? parseInt($(obj).children('img').attr('height')) : 320;
			var img_width = ($(obj).children('img').length > 0) ? parseInt($(obj).children('img').attr('width')) : 480;
			var img_href = ($(obj).children('img').length > 0) ? $(obj).children('img').attr('src').replace('/vid','') : '';
			
			var img_ratio = img_height / img_width;
			img_width += 100;
			img_height = Math.round(img_height + (img_ratio * 100));
			
			img_width = (self.override_width > 0) ? self.override_width : img_width;
			img_height = (self.override_height > 0) ? self.override_height : img_height;
			
			
			$("body").append("<div id='featured_media'><a href='"+href+"'></a></div>");
			$("#featured_media").dialog({
				modal: true,
				height: 40,
				width: 30,
				draggable: false,
				resizable: false,
				background: "#FFFFFF"
			});
			
			$("#featured_media").parent().css('overflow','visible');
			$("#featured_media").css('width','auto');
			$("#featured_media").css('height','auto');
			$("#featured_media").parent().animate({
				height: (img_height + 10)+'px',
				width: (img_width + 10)+'px',
				left: Math.round(($("#featured_media").parent().position().left) - (img_width / 2)),
				top: Math.round(($("#featured_media").parent().position().top) - (img_height / 2))
			}, 'fast', function(){
				$("#featured_media a").media( { 
					bgColor: '#ffffff',
					width: img_width, 
					height: img_height, 
					flashvars: { image: img_href,autoplay: true, shareURL:true}
				});
				$(".ui-dialog .ui-icon-closethick").html('X');
				
				$(".ui-widget-overlay").click(function(){
					self.close_overlay();
					return false;
				});
				$(".ui-dialog-titlebar a").click(function(){
					self.close_overlay();
					return false;
				});
				$(".ui-dialog-titlebar-close").click(function(){
					self.close_overlay();
					return false;
				});
				$("#featured_media").append('<a href="' + location.href + '" target="_self" title="Share" id="video_share" class="share_details addthis_button_expanded" style="background:none;">&nbsp;</a>');
				if(addthis != null){
					addthis.button("#video_share");
				}
			});
			$(".ui-dialog").css('overflow','visible');
		} else if($("#exhibits_reviews").length >0){
			self.show_editorial_images(obj);
		} else if(isImage){
			if($('#overlay_items').length>0){
				if($("#overlay_items").length > 0 ){
					$("#overlay_items").dialog("destroy");
				}
				
				var img_width = 463; 
				
				var img_ratio = img_width / parseInt($(obj).children('img').attr('width'));
				var img_height = Math.round(parseInt($(obj).children('img').attr('height')) * img_ratio);	
				var item_selected = $(obj).attr('id').replace('overlay_link_','overlay_item_');
				var num_selected = ($(obj).attr('id').indexOf('overlay_link_','').length >= 0) ? $(obj).attr('id').replace('overlay_link_','') : 0;
				
				self.total_items = $("#overlay_items .overlay_item").length;
				
				$("#overlay_items").dialog({
					modal: true,
					width: (img_width + 10)+'px',
					draggable: false,
					resizable: false,
					background: "#FFFFFF"
				});
				
				$("#overlay_items").parent().css('overflow','visible');
				$("#overlay_items").css('width','auto');
				$("#overlay_items").css('height','auto');
				$(".ui-dialog .ui-icon-closethick").html('X');
				$("#overlay_items").parent().css('top','150px');
				
				self.switch_item(num_selected);
				
				$(".ui-widget-overlay").click(function(){
					$("#overlay_items").dialog("destroy");
					return false;
				});
				$(".ui-dialog-titlebar-close").click(function(){
					$("#overlay_items").dialog("destroy");
					return false;
				});
				$(".ui-dialog").css('overflow','visible');
			} else {
				if($("#featured_media").length > 0 ){
					$("#featured_media").dialog("destroy");
					$("#featured_media").remove();
				}
				
				var img_width = 463;
				var img_ratio = img_width / parseInt($(obj).children('img').width());
				var img_height = Math.round(parseInt($(obj).children('img').attr('height')) * img_ratio);
				
				var img_href = $(obj).attr('href');
				var img_alt = $(obj).attr('title');
				img_alt = img_alt.split("|");
				$("body").append("<div id='featured_media'><img src='"+img_href+"' height='"+img_height+"' width='"+img_width+"' alt=\""+img_alt[0]+"\" /><div class='overlay_content'><br /><br /><strong>"+img_alt[0]+"</strong><br /><br />"+img_alt[1]+"<br /></div></div>");
				$("#featured_media").dialog({
					modal: true,
					height: 40,
					width: 30,
					draggable: false,
					resizable: false,
					background: "#FFFFFF"
				});
				
				$("#featured_media").parent().css('overflow','visible');
				$("#featured_media").css('width','auto');
				$("#featured_media").css('height','auto');
				$("#featured_media").parent().animate({
					height: (img_height + 100)+'px',
					width: (img_width + 10)+'px',
					left: Math.round(($("#featured_media").parent().position().left) - (img_width / 2)),
					top: Math.round(($("#featured_media").parent().position().top) - (img_height / 2))
				}, 'fast', function(){
					$(".ui-dialog .ui-icon-closethick").html('X');
				});
				
				$(".ui-widget-overlay").click(function(){
					self.close_overlay();
					return false;
				});
				$(".ui-dialog-titlebar-close").click(function(){
					self.close_overlay();
					return false;
				});
				$(".ui-dialog").css('overflow','visible');
			}
		}
	}
	
	
	self.show_editorial_images = function(obj) {
		current_image = parseInt($(obj).attr('class').split("overlay_link_")[1]); //figure out which thumbnail was clicked.
		total_images = parseInt($(obj).parent().parent().parent().children('.exhibit_reviews_images').children('img').length);
		current_review = $(obj).parent().parent().parent().attr('id');
		$("#overlay_items").dialog("destroy");
		$('#overlay_items').remove();//remove any existing nav
		
		$("body").append("<div id='overlay_items'>" + $(obj).parent().parent().parent().children('.exhibit_reviews_images').html() + "</div>");
		$('#overlay_items .exhibit_reviews_images').show();//show the container div
		$('#overlay_items img').hide();//hide all of the images
		
		//add in the overlay nav
		$('#overlay_items').prepend('<div id="overlay_nav_links"><a class="prev_nav" href="#prev">previous</a><span>' + (current_image+1) +'/'+total_images+'</span><a class="next_nav" href="#next">next</a></div>');
		if(total_images ==1){
			$("#overlay_nav_links").hide();
		}
		$("#overlay_items").dialog({
			modal: true,
			height: 40,
			width: 350,
			draggable: false,
			resizable: false,
			background: "#FFFFFF"
		});
				
		$("#overlay_items").parent().css('overflow','visible');
		$("#overlay_items").css('width','auto');
		$("#overlay_items").css('height','auto');
		$(".ui-dialog .ui-icon-closethick").html('X');
		$("#overlay_items").parent().css('top','150px');
		
		self.switch_review_image(current_image);
		
		$(".ui-widget-overlay").click(function(){
			$("#overlay_items").dialog("destroy");
			return false;
		});
		$(".ui-dialog-titlebar-close").click(function(){
			$("#overlay_items").dialog("destroy");
			return false;
		});
		$(".ui-dialog").css('overflow','visible');
	}
	
	self.switch_review_image = function(item_num){
		var current_num = parseInt(item_num);
		var total_images = parseInt($('#overlay_items img').length);
		var next_num = current_num + 1;
		var prev_num = current_num - 1;
		
		prev_num = (prev_num < 0) ? total_images-1 : prev_num;
		next_num = (next_num > total_images-1) ? 0 : next_num;
		
		$("#overlay_items img").hide();
		$("#overlay_nav_links span").html((parseInt(item_num)+1) + " / " + total_images);
		$("#overlay_items img:eq("+current_num+")").show();	
		$("#overlay_nav_links a.next_nav").unbind('click').click(function(){
			self.switch_review_image(next_num);
		});
		$("#overlay_nav_links a.prev_nav").unbind('click').click(function(){
			self.switch_review_image(prev_num);			
		});
		
	}
	
	self.switch_item = function(item_num){
		var current_num = parseInt(item_num);
		var next_num = current_num + 1;
		var prev_num = current_num - 1;
		
		prev_num = (prev_num < 0) ? self.total_items-1 : prev_num;
		next_num = (next_num > self.total_items-1) ? 0 : next_num;
		
		$("#overlay_items .overlay_item").hide();
		$("#overlay_nav_links span").html((parseInt(item_num)+1) + " / " + self.total_items);
		$("#overlay_items #overlay_item_"+item_num).show();	
		$("#overlay_nav_links a.next_nav").unbind('click').click(function(){
			self.switch_item(next_num);
		});
		$("#overlay_nav_links a.prev_nav").unbind('click').click(function(){
			self.switch_item(prev_num);			
		});
		
	}
	
	self.close_overlay = function(){
		$("#featured_media").dialog("destroy");
		$("#featured_media").remove();
		$("#at15s").hide();
		$(".ui-dialog").remove();
	}
	
	self.init();
}
function shareVideo(){
	$("#video_share").click();
}

/* EXHIBITS
*************************************************************/
function SetupExhibits(){
	var self = this;
	self.get_exhibit_url = "/_lib/get_exhibit_item.php";
	self.postRequest = null;
	self.current_image = 0;
	
	self.init = function(){
		if($("#exhibit_item").length > 0){
			$("#exhibit_item p img:gt(0)").hide();
			self.setup_exhibit_nav();
			$('#project_thumbnails a').unbind('click').click(function(){
				$('#project_thumbnails a.active').removeClass('active');
				$(this).addClass('active');
				self.load_exhibit_item($(this).attr('id'));
				return false;
			});
		}
	}
	self.load_exhibit_item = function(item_id){
		if(self.postRequest){
			self.postRequest.abort();
		}
		
		self.postRequest = $.post(self.get_exhibit_url,"&item_id="+item_id,function(data){
			if(data.length>0){
				$("#exhibit_item").html(data);
				$("#exhibit_item p img:gt(0)").hide();
				self.setup_exhibit_nav();
				if(hoverExhibitObj){
				   hoverExhibitObj.reposition_check();
				}
				if($(".media").length > 0){
					$(".media").media();
				}
			}
		});
	}
	
	self.setup_exhibit_nav = function(){
		if($("#exhibits_nav_links a").length > 0){
			$("#exhibits_nav_links a.prev_nav").unbind('click').click(function(){
				self.change_exhibit_image(-1);
				return false;
			});
			$("#exhibits_nav_links a.next_nav").unbind('click').click(function(){
				self.change_exhibit_image(1);
				return false;
			});
		}
	}
	
	self.change_exhibit_image = function(amount){
		total_images = $("#exhibit_item p img").length;
		if(self.current_image + amount < total_images && self.current_image + amount >= 0){
			self.current_image = parseInt(self.current_image) + parseInt(amount);
			$("#exhibit_item p img").hide();
			$("#exhibit_item p img:eq("+self.current_image +")").show();
			$("#exhibits_nav_links span").html((parseInt(self.current_image) + 1) + " / " + total_images);
		}
	}
	
	self.init();
}


/* MEDIA PLAYER DEFAULTS
*************************************************************/
$.fn.media.defaults.flvPlayer= '/_lib/player.swf';
$.fn.media.defaults.flashvars={autoplay: true};
/*$.fn.media.defaults.flashvars={overstretch: 1, controlbar: 'over'};*/
$.fn.media.defaults.mp3Player= '/_lib/player.swf';
/*$.fn.media.defaults.params= {allowFullScreen: "true"};*/

function resizeBackground(){
	if($('#work').length <= 0){
		var new_height = (parseInt($(document).height()) > 1200) ? parseInt($(document).height()) + 300 : parseInt($(document).height()) + 100;
		$("#bg").css('height', new_height + 'px');
	}
}

function RethinkHoliday() {
	var self = this;
	
	self.init = function(){
		$('a.holiday_greating').unbind('click').click(function(){
			self.setup_card_overlay();
			return false;
		});
	}
	
	self.setup_card_overlay= function(){
		$("body").append('<div id="card_overlay"><div id="myFlashContent">&nbsp;</div></div>');
		$("#card_overlay").dialog({
			modal: true,
			height: 620,
			width: 990,
			draggable: false,
			resizable: false,
			background: "#FFFFFF"
		});
		swfobject.embedSWF("/holidays2009/idea-snow2.swf", "myFlashContent", 980, 600, "9.0.0");
		
		$(".ui-dialog .ui-icon-closethick").html('X');
		$(".ui-widget-overlay").click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog-titlebar a").click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog-titlebar-close").click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog").css('overflow','visible');
	}
	
	self.close_overlay = function(){
		$("#card_overlay").dialog("destroy");
		$("#card_overlay").remove();
		$(".ui-dialog").remove();
	}
	
	self.init();
}

function RatingsObj(){
	var self = this;
	self.current_comments = "";
	self.save_rating_url = "/_lib/save_stars.php";
	self.save_comment_url = "/_lib/save_comments.php";
	self.saving_comment = false;
	
	self.init = function(){
		$('.review_star').rating({
			focus: function(value, link){
				if($('#star_review .star-rating-control.star-rating-readonly').length == 0){
					var tip = $('#star_tip');
					tip[0].data = tip[0].data || tip.html();
					self.current_comments = $('#star_tip .num_comments').html();
					tip.html(link.title);
				}
			},
			blur: function(value, link){
				if($('#star_review .star-rating-control.star-rating-readonly').length == 0){
					var tip = $('#star_tip');
					$('#star_tip').html(tip[0].data || '');
				}
			},
			callback: function(value, link){
				work_id = $('#star_review input.work_id').val();
				self.save_star_rating(value, work_id);			
			}
		});	
		$('#comment_form #btn_submit').unbind('click').click(function(){
			comment = $('#comment_form #comment_body').val();
			if(comment.length > 0){
				self.save_comment();
			}
			return false;
		});
		self.saving_comment = false;
	}
	
	self.save_star_rating = function(value, work_id){
		var num_comments = self.current_comments;
		if(!self.saving_comment){
			$.post(self.save_rating_url, "work_id="+work_id+"&rating="+value+"&num_comments="+num_comments, function(data){
				if(data){
					$('#star_review').html(data);
					self.init();
					$('.review_star').rating('disable'); 
				}
			});
		}
	}
	
	self.save_comment = function(){
		var content_to_send = $('#comment_form').serialize();
		$('#comment_form .error_message').remove();
		
		$.getJSON(self.save_comment_url, content_to_send, function(data){
			if(data){
				if(data.error_mess.length > 0){
					$("#comment_form").prepend(data.error_mess);
					$("#comment_form .error_message:hidden").fadeIn('fast');
				} else {
					$("#work_comments").prepend(data.comment);
					var num_comments = $('#star_tip .num_comments').html();
					var tmp = num_comments.split(' ');
					num_comments = parseInt(tmp[0])+1;
					$('#star_tip .num_comments').html(num_comments+ " comments");
					$("#work_comments .comment:hidden").fadeIn('fast');
				}
			}
		});
	}
	
	self.init();
}


function CoreTeam() {
	var self = this;
	
	self.init = function(){
		$('#to_core_team a').mouseover(function(){
			$(this).children('span.label').show();
			/*
			label_width = $(this).children('span.label').width();
			div_width = $(this).width();
			left_margin = Math.round((div_width / 2) - (label_width / 2));
			
			$(this).children('span.label').css('marginLeft',left_margin);*/
		});
		$('#to_core_team a').mouseout(function(){
			$(this).children('span.label').hide();
		});
	}
	
	self.init();
}
/* Twitter Update Function
*************************************************************/
function TwitterUpdater(args){
	var self = this;
	self.twitter_table = '';
	self.search_keywords = '';
	self.timestamp = '';
	self.after_timestamp = '';
	self.updater_url = '/_lib/get_latest_sxsw.php';
	self.filter_by_friends = true;
	
	self.tweet_container = null;
	self.ajax_post = null;
	self.still_loading = true;
	self.update_timer = 10000;
	self.tweet_limit = 20;
	self.updates = null;
	
	self.init = function(args){
		if(args != null){
			for(var arg in args){
				eval('self.'+arg+"='" + args[arg]+"'");
			}
		}
		self.setup_scroll_load();
		setTimeout(function(){ self.get_updates() }, parseInt(self.update_timer));
	}
	
	self.setup_scroll_load = function(){
		$(window).scroll(function(){
			if  (self.still_loading && $(window).scrollTop() == ($(document).height() - $(window).height())){
			   self.get_more_posts();
			}
		}); 
		$("#"+self.tweet_container).after("<div id='loading' style='display: none;'><img src='/_img/layout/loading.gif' alt='loading' width='24' height='24' /></div>");
	}
	
	self.get_more_posts = function(){
		if(self.ajax_post){
			self.ajax_post.abort();
		}
		
		$("#loading").show();
		var output_vars = "timestamp=" + self.after_timestamp + "&after=1";
		$.post(self.updater_url,output_vars,function(data){
			if(data != null){
				self.updates = data;
				if(parseInt(self.updates.feeds.length) > 0){
					$.each(self.updates.feeds,function(index,value){
						$("#"+self.tweet_container).append(value);
					});
					$("#"+self.tweet_container).children('li.new').fadeIn('fast',function(){ $(this).removeClass('new'); });
					self.after_timestamp = self.updates.timestamp;
				} else {
					self.still_loading = false;
				}
				$("#loading").hide();
			}
		}, "json");
	}
	
	self.get_updates = function(){
		var output_vars = "timestamp=" + self.timestamp ;
		$.post(self.updater_url,output_vars,function(data){
			if(data != null){
				self.updates = data;
				if(parseInt(self.updates.feeds.length) > 0){
					/* KEEP THE OLD POSTINGS NOW
					if($("#"+self.tweet_container).children('li').length > self.tweet_limit){
						gt = self.tweet_limit - self.updates.feeds.length;
						$("#"+self.tweet_container).children('li:gt(' + gt + ')').fadeOut('fast');
					}
					*/
					$.each(self.updates.feeds,function(index,value){
						$("#"+self.tweet_container).prepend(value);
					});
					$("#"+self.tweet_container).children('li.new').fadeIn('fast',function(){ $(this).removeClass('new'); });
					self.timestamp = self.updates.timestamp;
				}
			}
			setTimeout(function(){ self.get_updates() }, self.update_timer);
		}, "json");
	}
	
	self.init(args);
	
}

function hoverExhibit(){
	var self = this;
	self.current_top = 0;
	self.top_offset = 30;
	
	self.init = function(){
		if($("#exhibit_item").length > 0){
			$("#exhibit_item").css('position','absolute');	
			$("#exhibit_item").css('margin-bottom','50px');		
			$("#exhibit_item").css('width','484px');		
			self.current_top = $("#exhibit_item").offset().top+self.top_offset;
			self.setupScrollerSniffer();
		}
	}
	self.setupScrollerSniffer = function(){
		$(window).scroll(function(){
			self.reposition_check();
		}); 
	}
	self.reposition_check = function(){
		if(parseInt($('.left').height()) > parseInt($('.middle').height()+$('#exhibit_item').height())){
			if($(window).scrollTop() >= self.current_top){
			   self.reposition_exhibit($(window).scrollTop());
			} else {
			   self.reposition_exhibit(self.current_top);
			}
		} else {
			self.reposition_exhibit(self.current_top);
		}
	}
	self.reposition_exhibit = function(newTop){
		$("#exhibit_item").css('top', parseInt(newTop) + 'px');
	}
	
	self.init();
}

/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	if($(".media").length > 0){
		$(".media").media();
	}
	if($(".mp3").length > 0){
		$(".mp3").media({
			flashvars: {overstretch: 0,type: "sound", controlbar: 'bottom',backcolor:"000000"},
			params: {allowFullScreen: "false"}
		});
	}
	var holidayObj = new RethinkHoliday();
	
	if($('.overlay').length > 0){
		var overlayObj = new overlayMedia();
	}

	menuObj = new menu();	
	divopencloseobj = new divOpenClose();
	hoverExhibitObj = new hoverExhibit();
	
	if($("#filter_form").length<=0 && $("#star_review").length<=0){
		if($('form').length> 0){
			formObj = new formHelper();
		}
	}
	
	$("#news #news_years").change(function () {
		if($("#news #news_years option:selected").val().length > 0){
			window.location = $("#news #news_years option:selected").val();
		}
	});
	
	if($('.rethink_radios').length > 0){
		var philCookie = new cookie();
		var curr_q = 0;
		var answers = Array();
		philCookie.setCookie('philAnswers',answers,1);
		$('.rethink_radios').each(function(){
			$(this).attr('id','quest_'+curr_q);
			$(this).children('a.true').click(function(){
				var q = $(this).parent().attr('id').split('_');
				answers[q[1]] = 1;
				return false;
			});
			$(this).children('a.false').click(function(){
				var q = $(this).parent().attr('id').split('_');
				answers[q[1]] = 0;
				return false;
			});
			curr_q++;
		});
		$('.submit_btn').click(function(){
			philCookie.setCookie('philAnswers',answers,1);
		});
	}
	
	if($('.true_answers').length > 0){
		var ansCookie = new cookie();
		var q_answers = ansCookie.getCookie('philAnswers');
		q_answers.split(',').sort();
		tAnswers = 0;
		fAnswers = 0;
		for(x=0;x<q_answers.length;x++){
			if(q_answers[x] == 0){
				fAnswers++;
			} else if(q_answers[x] == 1){
				tAnswers++;
			}
		}
		$('.true_answers').html(tAnswers);
		$('.false_answers').html(fAnswers);
	}
	
	
	setTimeout('resizeBackground()',250);
});

