/**
 * @author: Tom Bull
 * @copyright: Louche 2011
 */


// PLUGINS
(function($) {

	
	// Lightbox Video Gallery Plugin
	$.fn.lightboxVideoGallery = function() {
		
		var lightbox = $('#lightbox'),
			lightbox_panel = $('#lightbox-panel');
		
		return this.each(function() {
			var el = $(this),
				el_src = el.attr('href');
			
			el.click(function() {
				lightbox_panel.addClass('loading');
				lightbox.fadeIn(250),lightbox_panel.fadeIn(250);
				
				$('<iframe title="YouTube video player" class="youtube-player" type="text/html" width="480" height="390" src="' + el_src + '" frameborder="0"></iframe>').appendTo(lightbox_panel).hide().load(function() {
					lightbox_panel.removeClass('loading');
					new_video = $(this);
					new_video.fadeIn(750);
				});
				return false;
			});
			
			$('a#close-panel').click(function() {
				lightbox.fadeOut(250),lightbox_panel.fadeOut(250);
				$('.youtube-player').remove();
				return false;
			});
		});
	}
	

	// Lightbox Image Gallery Plugin
	$.fn.lightboxImageGallery = function() {
		
		var el = $(this),
			lightbox = $('#lightbox'),
			lightbox_panel = $('#lightbox-panel');
			
		$('a', el).each(function() {
			var thumb = $(this),
				main_image = thumb.attr('href');
			
			thumb.click(function() {
				lightbox_panel.addClass('loading');
				lightbox.fadeIn(250),lightbox_panel.fadeIn(250);
				
				$('<img src="' + main_image +'">').appendTo(lightbox_panel).hide().load(function() {
					lightbox_panel.removeClass('loading');
					new_image = $(this);
					new_image.fadeIn(750);
				});

				return false;
			});
			
			$('a#close-panel').click(function() {
				lightbox.fadeOut(250),lightbox_panel.fadeOut(250);
				$('img', lightbox_panel).remove();
				return false;
			});
			
		});
	
	}
	
	
	// Default Input Value
    $.fn.defaultInputValue = function(user_settings){

        var o = {text : "Search for..."};

        $.extend(true, o, user_settings);

        return this.each(function(){

			if(!this.value) this.value = o.text;

			$(this).focus(function(){
					if (this.value == o.text){
							this.value = '';
					}
			}).blur(function(){
					if (this.value == ''){
							this.value = o.text;
					}
			});
			
        });
    }
    
    
})(jQuery);


// ON DOCUMENT READY
$(function() {

	$('.artwork #thumbnails').lightboxImageGallery();
	
	$('.show-panel').lightboxVideoGallery();
	
	$('#mce-EMAIL').defaultInputValue({text: "Enter Email Address Here"});
	
	

});


(function ($) {
    //define the tweetable plugin
    $.fn.tweetable = function (options) {
        //specify the plugins defauls
        var defaults = {
            limit: 5, 						//number of tweets to show
            username: 'philipbeel', 	//@username tweets to display
            time: false, 					//display date
            replies: false,				//filter out @replys
            position: 'append'			//append position
        };
        //overwrite the defaults
        var options = $.extend(defaults, options);
		//loop through each instance
        return this.each(function (options) {
			//assign our initial vars
            var act = $(this);
            var $tweetList;
            var tweetMonth = '';
            var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
            var api = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
            var count = "&count=";
            //do a JSON request to twitters API
            $.getJSON(api + defaults.username + count + defaults.limit + "&callback=?", act, function (data) {
				//loop through twitters response
                $.each(data, function (i, item) {
					//check for the first loop
                    if (i == 0) {
                    	//create an unordered list to store tweets in
                        $tweetList = $('<ul class="tweetList">')[defaults.position.toLowerCase() + 'To'](act);
                    }
                    //handle @reply filtering if required
                    if (defaults.replies === false) {
                        if (item.in_reply_to_status_id === null) {
                            $tweetList.append('<li class="tweet_content_' + i + '"><p class="tweet_link_' + i + '">' + item.text.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2')+'</p></li>');
                        }
                    } else {
                        $tweetList.append('<li class="tweet_content_' + i + '"><p class="tweet_link_' + i + '">' + item.text.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2') + '</p></li>');
                    }
                    //display the tiem of tweet if required
                    if (defaults.time == true) {
                    	for(var iterate=0; iterate<=12; iterate++) {
                    		if(shortMonths[iterate] == item.created_at.substr(4, 3)) {
								tweetMonth = iterate + 1;
								if(tweetMonth < 10) {
									tweetMonth = '0' + tweetMonth;
								}
	                   		} 	
                    	}
                        $('.tweet_link_' + i).append('<small> ' + item.created_at.substr(8, 2) + '/' + tweetMonth + '/' + item.created_at.substr(26,4) + ' ' + item.created_at.substr(11,8) + '</small>');
                    }
                });
                //close the unordered list
               });
        });
    }
})(jQuery);

$(window).load(function() {
						$('#slider').nivoSlider({
							effect:'fold', //Specify sets like: 'fold,fade,sliceDown'
							slices:15,
							animSpeed:500, //Slide transition speed
							pauseTime:5000,
							startSlide:0, //Set starting Slide (0 index)
							directionNav:false, //Next & Prev
							directionNavHide:true, //Only show on hover
							controlNav:true, //1,2,3...
							controlNavThumbs:false, //Use thumbnails for Control Nav
							controlNavThumbsFromRel:false, //Use image rel for thumbs
							controlNavThumbsSearch: '.jpg', //Replace this with...
							controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
							keyboardNav:false, //Use left & right arrows
							pauseOnHover:true, //Stop animation while hovering
							manualAdvance:false, //Force manual transitions
							captionOpacity:0.8, //Universal caption opacity
							beforeChange: function(){},
							afterChange: function(){},
							slideshowEnd: function(){}, //Triggers after all slides have been shown
							lastSlide: function(){}, //Triggers when last slide is shown
							afterLoad: function(){} //Triggers when slider has loaded
						});
					});
