/**
*
*  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;
	}
 
}






/* OVERLAY MEDIA
*************************************************************/
function overlayMedia() {
	var self = this;
	
	self.total_items = 0;
	
	self.init = function(){
		$("a.overlay").each(function(){
			$(this).unbind('click').click(function(){
				self.setup_overlay($(this));
				return false;
			});
		});
	}
	
	self.setup_overlay = function(obj){
		var href = ($(obj).attr('href'))? $(obj).attr('href') : ".gif";
		var isImage = ( href.indexOf(".png") > 0 || href.indexOf(".jpg") > 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));
			
			$("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');
				//$("#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>');
			});
			
			$(".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');
		} 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').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').replace('overlay_link_','');
				
				self.total_items = $("#overlay_items .overlay_item").length;
				
				$("#overlay_items").dialog({
					modal: true,
					height: (img_height + 100)+'px',
					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]+"\" /><br /><br /><strong>"+img_alt[0]+"</strong><br /><br />"+img_alt[1]+"<br /></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.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();
}


/* 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"};*/




/* BLUE BUTTONS
*************************************************************/
function blueButton() {
	var self = this;
	
	self.init = function() {
		if($(".blue_button").length > 0) {
			$(".blue_button").append("<span class='left'></span><span class='right'></span>");
		}
	}
	
	self.init();
}

/* SHOW
*************************************************************/
function showInfo() {
	var self = this;
	self.reviews_count = 0;
	self.ajax_post = null;
	self.feature_id = 0;
	self.still_loading = true;
	self.get_reviews_url = "/_lib/get_reviews.php";
	
	self.init = function() {
		if($(".hide").length > 0) {
			$(".more").click(function () {
				$("#more").removeClass("hide");
				self.setup_loading();
				self.feature_id = $(this).attr('href');
				self.get_more_reviews();
				self.setup_scroll_load();
				return false;
			});
		}
	}
	self.setup_scroll_load = function(){
		$(window).scroll(function(){
			if  (self.still_loading && $(window).scrollTop() == $(document).height() - $(window).height()){
			   self.get_more_reviews();
			}
		}); 
	}
	
	self.get_more_reviews = function(){
		if(self.ajax_post){
			self.ajax_post.abort();
		}
		
		$("#loading").show();
		self.reviews_count = $(".more_reviews .review").length;
		self.ajax_post = $.post(self.get_reviews_url,"&from="+self.reviews_count+"&feature_id="+self.feature_id,function(data){
			if(data.length>0){
				$("#more br").before(data);
				$("#loading").hide();
			} else {
				self.still_loading=false;
				$("#loading").hide();
			}
		});
	}
	self.setup_loading = function(){
		$("#view_more").replaceWith("<div id='loading'><img src='/_img/interface/loading.gif' alt='loading' width='24' height='24' /></div>");
	}
	self.init();
}

/* REVIEW FORM
*************************************************************/
function reviewForm(){
	var self = this;
	
	self.init = function(){
		$('#review_form').validate({	
			messages: {
				name: "Please enter your name",
				title: "Please enter the title",
				by: "Please enter who it's by",
				review: "Please enter your review"
			},   
			submitHandler: function(form) {
			   // do other stuff for a valid form
				$("#input_submit").click();
		   }
		});
		
		$("#review_form #btn_submit").unbind('click').click(function(){
			$("#review_form").submit();
			return false;
		});
	}
	
	
	self.init();
}

/* TWEET MODULE
*************************************************************/
function Tweets(){
	var self = this;
	self.tweet_content = "home_tweets";
	self.tweet_container = "home_tweet_container";
	
	self.init = function(){
		console.log($('#'+self.tweet_container).height() + " | " + $('#'+self.tweet_content).height());
	}
	
	self.setup_scroller = function(){
		
	}
	
	self.init();
}

/* TWEET SCROLL BAR
*************************************************************/
function tweetScroll(){
	var self = this;
	
	self.height_offset = 18;
	self.scroll_amount = 10;
	self.scroll_time = 100;
	self.timer = "";
	
	self.init = function(){
		if($('#home_tweet_nav').length > 0){
			self.setup_scrolling();
		}
	}
	
	self.setup_scrolling = function(){
		
		$('#home_tweet_nav a.up_nav').click(function(){ return false; });
		$('#home_tweet_nav a.down_nav').click(function(){ return false; });
		
		$('#home_tweet_nav a.up_nav').mousedown(function(){
			clearInterval(self.timer);
			self.timer = setInterval("move_container('down')",self.scroll_time);
			return false;
		});			
		$('#home_tweet_nav a.up_nav').mouseup(function(){
			clearInterval(self.timer);
		});
		
		$('#home_tweet_nav a.down_nav').mousedown(function(){
			clearInterval(self.timer);
			self.timer = setInterval("move_container('up')",self.scroll_time);
			return false;
		});			
		$('#home_tweet_nav a.down_nav').mouseup(function(){
			clearInterval(self.timer);
		});
	}
	
	self.init();
}

move_container = function(direction) {
	var copy_height = parseInt($('#home_tweets').height())+10;
	var new_height = parseInt($('#home_tweet_container').css('height'));
	
	if(direction == 'up'){
		new_top = parseInt($('#home_tweets').css('top')) - 10;
		if(new_top >= 0-(copy_height - new_height)){
			$('#home_tweets').css('top', new_top + "px");
		}
	} else {
		new_top = parseInt($('#home_tweets').css('top')) + 10;
		if(new_top <= 0){
			$('#home_tweets').css('top', new_top + "px");
		}
	}
}




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

/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	
	var blueLinks = new blueButton();
	
	var showIt = new showInfo();
	
	if($('#home_tweets').lenght > 0){
		var tweets = new Tweets();
	}
	
	if($(".media").length > 0){
		$(".media").media();
	}

	if($('.overlay').length > 0){
		var overlayObj = new overlayMedia();
	}
	
	tweetScrollObj = new tweetScroll();


});