function SuperScript () {
	if ($.browser.msie && $.browser.version.substr(0,1)<=6) {
		$('.campaignOverlay').attr('src','/beta/images/uploads/clear.png');
	}
	if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
		$('#entryButtons').hide();
	}

	this.lastThumbnail = "";
	// Scale based on body font size

        var bodyFontSize = $("body").css('font-size');
        bodyFontSize = bodyFontSize.substr(0, bodyFontSize.length -2);
        this.mediaWidth = bodyFontSize * 50;
        this.mediaHeight = bodyFontSize * 28.125;

	// Check for mobile version

	var agent=navigator.userAgent.toLowerCase();
	this.isMobile = ((agent.indexOf('iphone')!=-1) || (agent.indexOf('ipad')!=-1) || (agent.indexOf('android')!=-1));

	// Handle thumbnail clicks

	this.thumbnailClick = function (curThumbnail, packageLink) {
		if (superScript.lastThumbnail == curThumbnail) {
			return 0;
		}
		if (superScript.lastThumbnail != "" && superScript.lastThumbnail != curThumbnail ) {
			$(superScript.lastThumbnail).find(".thumbnailDesc").fadeOut();
			$(superScript.lastThumbnail).hoverIntent( function () {$(this).find(".thumbnailDesc").fadeIn();}, function () { $(this).find(".thumbnailDesc").fadeOut();} );
		}
		superScript.lastThumbnail = curThumbnail;

		var entryID = $(curThumbnail).attr('data-entry_id');				
		var entryJSON = $(curThumbnail).attr('data-json');				
		var entryURL = $(curThumbnail).find('.galleryImage').attr('data-entry_url');				

		// Load Entry Data

		$.getJSON(entryJSON, function(data) {
			if (data.channel == "work") {
				if (!packageLink) $('#packageFrame').slideUp();
				projectData = data;
			} else if (data.channel == "package") {
				projectData = data.project[0];
				$('#packageFrame').slideUp(500, function () {
					superScript.loadToElement('#packageGallery',data.packageURL, function () {
						$('#packageFrame').slideDown();
						$("#packageFrame").find(".thumbnail").click( function () { superScript.thumbnailClick(this,true); return false;});
						$("#packageFrame").find(".thumbnail").hoverIntent( function () {$(this).find(".thumbnailDesc").fadeIn();}, function () { $(this).find(".thumbnailDesc").fadeOut();} );
					});
				});
			}

			$('#projectCredits').slideUp( 'fast',  function () {
				superScript.loadToElement('#projectCredits',projectData.creditsURL, function (creditsReturned) {
					if (creditsReturned)
						$('#projectCreditsHeader').slideDown();
					else
						$('#projectCreditsHeader').slideUp();
						$('#projectCredits').slideUp();
				});
			});

			if (projectData.vimeo != "") { // Video
				superScript.replaceVideo(projectData.title, projectData.vimeo, projectData.poster , superScript.mediaWidth,superScript.mediaHeight,1);
			} else if (projectData.vimeo == "" && projectData.flickr != "") { // Print
				superScript.replaceSlides(projectData.title, projectData.flickr, superScript.mediaWidth,superScript.mediaHeight, function () {
					$('#packageFrame').slideDown();
				});
			} else if (projectData.vimeo == "" && projectData.flickr == "") { // Interactive?
				superScript.setImage(projectData.poster,700, null, "#mediaFrame", function () {
					if (projectData.link != "") {
						$("#mediaFrame").append('<div id="mediaDesc">Click To Launch</div>');
						$("#mediaDesc").fadeIn();
					}
				});
			}
			
			if (projectData.link != "") {
				$("#mediaFrame").click(function () { window.open(projectData.link, projectData.title, "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=1, scrollbars=1, width=1280, height=780");});
				$("#mediaFrame").css('cursor','pointer');
			} else {
				$("#mediaFrame").unbind('click');
				$("#mediaFrame").css('cursor','default');
			}

		});

		$(curThumbnail).unbind('mouseover');
		$(curThumbnail).unbind('mousemove');
		$(curThumbnail).unbind('mouseout');

	}

	this.loadToElement = function (element, dataURL, completeCallback) {
		$.ajax({
			url: dataURL,
			success: function(creditsData) {
					if (creditsData.replace(/^\s+|\s+$/g,"") != "") {
						$(element).html(creditsData);
						completeCallback(true);
					} else {
						completeCallback(false);
					}
			}
		});
	}

	// Function to dynamically insert Vimeo content

	this.replaceVideo = function (title, vimeoID, poster, videoWidth, videoHeight,autoplay) {
		if (this.isMobile) {
			$("#mediaFrame").html('');
			var autoplayString = (autoplay == 1) ? "autoplay" : "";
			if ( Math.ceil(videoHeight) != $("#mediaFrame").height()) {
				$("#mediaFrame").animate({'height':videoHeight},500, function () {
					$("#mediaFrame").html('<video src="http://www.vimeo.com/play_redirect?clip_id=' + vimeoID + '" poster="' + poster + '" ' + autoplayString +' controls height="' + videoHeight + '" width="' + videoWidth + '"></video>')
				});
			} else {
				$("#mediaFrame").html('<video src="http://www.vimeo.com/play_redirect?clip_id=' + vimeoID + '" poster="' + poster + '" ' + autoplayString +' controls height="' + videoHeight + '" width="' + videoWidth + '"></video>')
			}
		} else {
			$("#mediaFrame").oembed( "http://vimeo.com/" + vimeoID, {vimeo: { maxHeight: videoHeight, maxWidth:videoWidth, autoplay: autoplay, color:'ffffff', byline:false, title:true,portrait:false}}, function (container, oembed) {
				if (oembed != null) {
					$("#mediaFrame").html('');
					if ( Math.ceil(oembed.height) != $("#mediaFrame").height()) {
						$("#mediaFrame").animate({'height':oembed.height},500, function () {
							$("#mediaFrame").html('<div style="margin:auto;width:'+ oembed.width +'px;">' + oembed.code + '</div>');
						});
					} else {
						$("#mediaFrame").html('<div style="margin:auto;width:'+ oembed.width +'px;">' + oembed.code + '</div>');
					}
				}
			});
		}

		//$("#videoTitle").html('<h3>' + title + '</h3>');

		//if ($("#mediaFrame").css('display') == "none") { $("#mediaFrame").show("blind", {}, 1000); } 
	}

	this.replaceSlides = function( title, flickrID, mediaWidth, mediaheight, completeCallback) {
		flickrAPIKey = "bc8390d13ae9ff573188d060415a55f9";
		flickrPhotosetID = flickrID;
		flickrExtras = "url_s, url_l, url_o, o_dims";
		apiURL = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key="+flickrAPIKey+"&photoset_id=" + flickrPhotosetID+"&format=json&extras="+flickrExtras+"&jsoncallback=?";

		$("#packageGallery").html('');

		$.getJSON(apiURL, function(data){ 
			$.each(data.photoset.photo, function(i,photo) {
				big_src = (photo.url_l != null) ? photo.url_l : photo.url_o;

				big_ratio = mediaWidth  / photo.o_width;
				big_width = photo.o_width * big_ratio;
				big_height = photo.o_height * big_ratio;

				if (i == 0) {
					superScript.setImage(big_src,big_width, big_height, "#mediaFrame");
				}

				thumbHTML = '<div class="thumbnail "data-big_src="'+  big_src  +'" data-big_width="'+ big_width +'" data-big_height="'+ big_height +'" ><div class="slideThumb galleryImage" style="background-image:url(\''+photo.url_s+'\'); src="' + photo.url_s +'" ></div></div>';
				$thumb = $("#packageGallery").append(thumbHTML);
			});

			$("#packageGallery").find(".thumbnail").click( function () {
				
				var big_src  = $(this).attr('data-big_src');				
				var big_width  = $(this).attr('data-big_width');				
				var big_height  = $(this).attr('data-big_height');				
				superScript.setImage(big_src,big_width, big_height, "#mediaFrame");
			});
			completeCallback();
		});


	}

	this.setImage = function (imageSRC, imageWidth, imageHeight, container,completeCallback) {
		var newImage = new Image();
		$(newImage)
			.load(function () {
				if ($(container + " :first-child")[0].tagName == "IMG") {
					$(container + " :first-child").fadeOut(500, function () {
						$(newImage).hide()
						$(container).html(newImage);
						imageHeight = (imageHeight == null) ? $(newImage).height() : imageHeight;
						$(container).animate({'height':imageHeight},500, function () {
							$(newImage).height(imageHeight); $(newImage).fadeIn();
							if (completeCallback != null) completeCallback();
						});
					});
				} else {
					$(container).html('');
					$(newImage).hide()
					$(container).html(newImage);
					imageHeight = (imageHeight == null) ? $(newImage).height() : imageHeight;
					$(container).animate({'height':imageHeight},500, function () {
						$(newImage).height(imageHeight); $(newImage).fadeIn();
						if (completeCallback != null) completeCallback();
					});
				}

				//$(this).fadeIn();
			})
			.error(function () { })
			.attr('src', imageSRC)
			.css('width',imageWidth + 'px');

	}

	this.buildSlideshow = function (flickrID, mediaWidth, mediaHeight) {

		if (flickrID == "")
			return -1;

		flickrAPIKey = "bc8390d13ae9ff573188d060415a55f9";
		flickrPhotosetID = flickrID;
		flickrExtras = "url_s, url_l, url_o, o_dims";
		apiURL = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key="+flickrAPIKey+"&photoset_id=" + flickrPhotosetID+"&format=json&extras="+flickrExtras+"&jsoncallback=?";

		$.getJSON(apiURL, function(data){ 
			$.each(data.photoset.photo, function(i,photo) {
				big_src = (photo.url_l != null) ? photo.url_l : photo.url_o;

				big_ratio = mediaWidth  / photo.o_width;
				big_width = photo.o_width * big_ratio;
				big_height = photo.o_height * big_ratio;

				thumbHTML = '<div class="thumbnail"><a><div class="slideThumb galleryImage" style="background-image:url(\''+photo.url_s+'\'); src="' + photo.url_s +'" big_src="'+  big_src  +'" big_width="'+ big_width +'" big_height="'+ big_height +'" ></div></a></div>';
				$thumb = $("#slideshow").append(thumbHTML);
			});

			$("#slideshow").find(".slideThumb").click( function () {

				nextImage = $(this).attr("big_src");
				nextWidth = $(this).attr("big_width");
				nextHeight = $(this).attr("big_height");

				superScript.setImage(nextImage, nextWidth, nextHeight, "#slideshowImageBox");

				//if ($("#slideshowImageBox").css('display') == "none") { $("#slideshowImageBox").show("blind", {}, 1000); } 
			});
		});
	}
}
