
var enotionz = {
    createTwitterWidget: function() {
        new TWTR.Widget({
            version: 2,
            type: 'profile',
            rpp: 5,
            interval: 6000,
            width: 'auto',
            height: 300,
            theme: {
                shell: {
                    background: 'transparent',
                    color: '#E4E4E4'
                },
                tweets: {
                    background: 'transparent',
                    color: '#E4E4E4',
                    links: '#0099FF'
                }
            },
            features: {
                scrollbar: false,
                loop: false,
                live: false,
                hashtags: true,
                timestamp: true,
                avatars: false,
                behavior: 'all'
            }
        }).render().setUser('enotionz').start();
    },
	createTwitterBadge: function() {
			
		$.jTwitter('enotionz', 5, function(posts){
			var output = '<div class="ez-sidebarTitle border_radius_3px box_shadow_fff"> <p><a href="http://twitter.com/enotionz" title="follow me!" target="_new">@Enotionz</a></p></div> \
				<div id="ez-tweets">';
			
			for (var i=0; i<posts.length; i++){
				var date = posts[i].created_at,
					id = posts[i].id,
					tweet = posts[i].text,
					replyTo = posts[i].in_reply_to_screen_name,
					replyId = posts[i].in_reply_to_status_id;

				output += '\
					<div class="tw-post" id="'+ id +'"> \
						<div class="tw-date">' + enotionz.convertTwitterDate(date) +  '</div> \
						<div class="tw-tweet">' + tweet ;
					
				if (replyTo != null) { output += ' <span class="tw-inReply">(<a href="http://twitter.com/' + replyTo + '/status/' + 
						replyId + '" target="_new">in reply to ' + replyTo + '</a>)</span>'; }
				output += '</div></div>';
				
			}
			output += '</div>';
			
			$("#ez-TwitterBadge").html(output).slideDown('slow');
			return false;
		});
		return false;
	},
	convertTwitterDate: function(strDate) {

        var dateArray = strDate.split(" ");
        var dayOfWeek = dateArray[0],
            month = dateArray[1],
            day = dateArray[2],
            hours = dateArray[3].substr(0,2),
            minutes = dateArray[3].substr(3,2),
            year = dateArray[5];

        /* internet explorer can't process new Date(strDate) correctly */
        if (navigator.appName !== "Microsoft Internet Explorer"){
            var date = new Date(strDate);
            hours = date.getHours();
            minutes = date.getMinutes();
            minutes = (minutes < 10 ? "0" + minutes : minutes);
        }
        hours = (hours > 12 ? hours - 12 : hours);
		return hours + ':' + minutes + ' ' + (hours>11 ? "PM" : "AM") + ' ' + month + '. ' + year;
	},

	showImage: function(id,label){
		if(label == "") label = "all";

		$.getJSON('../gallery/functions.php?id=' + id + '&label=' + label + '&sid=' + Math.random(), function(image) {
			var listing = "", traverse = "", ezImage = "", ezdesc = "";
			var prev = "0", next = "0";		
			/* Creates listing of images */
			for (var i=0; i<image.listing.length; i++){
				var active = (image.listing[i] == image.id ? ' class="ezgal-active"' : '');
				
				if(image.listing[i] == image.id) {
					if (i != 0)	prev = image.listing[i-1];
					if (i != image.listing.length-1)	next = image.listing[i+1];
				}

				listing += '<li' + active  + '><a href="gallery.php?id=' + image.listing[i] + '&label=' + image.setLabel +
					'" onclick="enotionz.showImage(' + image.listing[i] + ',\'' + image.setLabel + '\'); return false;">' + 
					(i+1 < 10 ? '0': '') + (i+1) + '</a></li>';

			}

			/* creates previous & next functionality */
			traverse += '<a ' + (prev == "0" ? 'class="ez-previous disabled" href="#" onclick="return false;"' : 'class="ez-previous" href="gallery.php?id=' + prev + '&label=' + image.setLabel + '" onclick="enotionz.showImage(' + prev + ',\'' + image.setLabel + '\'); return false;"') + '><span>Previous Item</span></a>'; 
			traverse += '<a ' + ( next == "0" ? 'class="ez-next disabled" href="#" onclick="return false;"' : 'class="ez-next" href="gallery.php?id=' + next + '&label=' + image.setLabel  + '" onclick="enotionz.showImage(' + next + ',\'' + image.setLabel + '\'); return false;"') + '><span>Next Item</span></a>'; 

			$("ul#ez-gallery").html(listing);
			$(".ez-traverse").html(traverse);

			/* Creates image & description */
			$("#ez-galPlaceholder").css({"width" : image.width + "px", "height" : image.height + "px", "background-image" : "url(gallery/" + 
				image.label.replace(/ /gi,'_') + "/" + image.file  + ")"});
			$("#imageTitle").html(image.name);
			$("#imageDesc").html(image.description);
		});
	}
}

$(document).ready(function(){
	if ($("#ez-TwitterBadge").length > 0) {
		enotionz.createTwitterBadge();
	}

	if ($("#ez-gallery").length > 0) {
		


	}


});
 
/*##############################################
 Gallery
 ##############################################*/
// when the DOM is ready
function loadImage(myLink) {
  $('#loader').addClass('loading');
  $('#loader').children().addClass("visibilityHidden");
	
  $('#galThumbs a').removeClass('active');
  $(myLink).addClass('active');
	
  var img = new Image();
  // wrap our new image in jQuery, then:
  $(img)
    // once the image has loaded, execute this code
    .load(function () {
      $(this).hide();		// set the image hidden by default
      $('#loader')
        .removeClass('loading')	// done loading? remove load icon
		.html('')
        .append(this);			// add the image
		
	// add title
	$('#loader').append(getLinkCaption(myLink));
    
      // fade our image in to create a nice effect
      $(this).fadeIn();
    })
    // if there was an error loading the image
    .error(function () {
    })
    .attr('src', $(myLink).attr('href').substring(1));
	
}

function getLinkCaption(myLink) {
	var output;
	var broken = $(myLink).attr('title').split("||");
	
	output = broken[0] + "<br>" + broken[1];
	return output
}
