var sets = {};

function truncate(value,arg) {
    var value_arr = value.split(' ');
    if(arg < value_arr.length) {
        value = value_arr.slice(0,arg).join(' '); }
    return value;
}

String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild)
    return result;
}

function callback(hash, opts){
	hash.replace("+&+", "%26");
	if(hash){
		if(hash.match(/jpg|png|gif/)){
			if(opts && opts.evt.target){
				// do nothing on click event
			}
			else
				jQuery.slimbox(hash);
		}
		else if(hash.indexOf("youtube") != -1){
			var video_id = hash.match(/\?v=([\w-]*)/)[1];
			swfobject.embedSWF("http://youtube.com/v/" + video_id, "video", 425, 344, "9.0.0", "/javascripts/expressinstall.swf");
			if(opts && opts.evt){
				jQuery("#page_title").text(jQuery(opts.evt.target).attr("title"));
				jQuery("#page_desc").text(jQuery(opts.evt.target).attr("alt"));
			}
		}
		else
		jQuery.get(hash, function(data) {
			if(sets){
				var set = sets[hash.match(/set_id=(\d*)/)[1]];
				var flickr_set_url = "http://www.flickr.com/photos/jenrosenthal/sets/" + set.id;
				
				var content_area = jQuery("#content-right").html(jQuery(data).find("#content-right").html());
				var title = set.title;
				jQuery('#right-upper h2').html(title);
				if(set.description && set.description != ""){
					var description = set.description;
					if(description.split(" ").length > 80)
						description = truncate(set.description || "", 60) + "..." + "<a href='" + flickr_set_url + "'>read more</a>";
					content_area.find("#set_description").html(description).find("a").attr("title", set.description);
					jQuery("#set_description a").live("click", function(e){
						e.preventDefault();
						jQuery("#description_overflow").html(jQuery(this).attr("title")).dialog("open");
					});
				}
			}
		});
	}
	
}
jQuery(document).ready(function(){
	
	
	jQuery("#description_overflow").dialog({autoOpen: false});
	
	jQuery.noConflict();
	jQuery.history.init(callback);
	 jQuery("a[rel='history']").click(function(e){
	        var hash = this.href;
						e.preventDefault();
						//hash = hash.replace(/^.*#/, '');
						// moves to a new page. 
						// pageload is called at once. 
						// hash don't contain "#", "?"
						jQuery.history.load(hash, {evt: e});
						return false;
	    });
    	

			jQuery.extend(jQuery.history, {
				load: function(hash, opts) {
				var newhash;
				if (jQuery.browser.safari) {
					newhash = hash;
				} else {
					newhash = '#' + hash;
					location.hash = newhash;
				}
				this._curHash = newhash;

				if (jQuery.browser.msie) {
					var ihistory = jQuery("#jQuery_history")[0]; // TODO: need contentDocument?
					var iframe = ihistory.contentWindow.document;
					iframe.open();
					iframe.close();
					iframe.location.hash = newhash;
					this._callback(hash, opts);
				}
				else if (jQuery.browser.safari) {
					this._dontCheck = true;
					// Manually keep track of the history values for Safari
					this.add(hash, opts);

					// Wait a while before allowing checking so that Safari has time to update the "history" object
					// correctly (otherwise the check loop would detect a false change in hash).
					var fn = function() {jQuery.history._dontCheck = false;};
					window.setTimeout(fn, 200);
					this._callback(hash, opts);
					// N.B. "location.hash=" must be the last line of code for Safari as execution stops afterwards.
					//      By explicitly using the "location.hash" command (instead of using a variable set to "location.hash") the
					//      URL in the browser and the "history" object are both updated correctly.
					location.hash = newhash;
				}
				else {
				  this._callback(hash, opts);
				}
			}});
	
		jQuery("dd").hide();


	    jQuery(".accordion dt a").click(function(e){
				e.preventDefault();
	      jQuery(this).parent().next().slideToggle("fast").siblings("dd").slideUp("fast");
	    	return false;
	    });
		
  });


//add history support to slimbox
jQuery.fn.slimbox = function(_options, linkMapper, linksFilter) {
	linkMapper = linkMapper || function(el) {
		return [el.href, el.title];
	};

	linksFilter = linksFilter || function() {
		return true;
	};

	var links = this;

	return links.click(function(e) {
		// Build the list of images that will be displayed
		var link = this, startIndex = 0, filteredLinks, i = 0, length;
		filteredLinks = jQuery.grep(links, function(el, i) {
			return linksFilter.call(link, el, i);
		});

		// We cannot use jQuery.map() because it flattens the returned array
		for (length = filteredLinks.length; i < length; ++i) {
			if (filteredLinks[i] == link) startIndex = i;
			filteredLinks[i] = linkMapper(filteredLinks[i], i);
		}
		if(jQuery.history)
			jQuery.history.load(jQuery(this).attr("href"), {evt: e});
		return jQuery.slimbox(filteredLinks, startIndex, _options);
	});
};

