/********************************************* 
		JavaScript Document 
		Web Design and Development by:

		www.Developer7.com
		contact email:
		info@developer7.com

*********************************************/
$(document).ready(function() {
													 
		Cufon.replace('#Nav a');
		Cufon.replace('.bottom_nav a');
	
		$('.slidedeck').slidedeck({
			scroll : false													
		});
		
		$('.delete_button').click(function() {
			return confirm("Are you sure you want to delete?");
		});
			
		$('.global_message').delay(5000).fadeOut();
		
		$('.admin_open').toggle(function() {
			$(this).html('<img src="/img/icons/toggle_closed.png" />');
		}, function() {
			$(this).html('<img src="/img/icons/toggle_open.png" />');
		});
	
		$('#admin_edit').hide();
		$('.admin_open').click(function () {
			$('#admin_edit').slideToggle();	
				return false;
		});

		$('.edit, a.add_news, a.add_events').hide();
		$('.featured, .release, .event').mouseenter(function(){			
			$(this).children('.edit').fadeIn();															 
		});
		$('.featured, .release, .event').mouseleave(function(){											 
			$(this).children('.edit').fadeOut();				
		});

		$('.content').mouseenter(function(){			
			$('a.add_news, a.add_events').fadeIn();															 
		});
		$('.content').mouseleave(function(){											 
			$('a.add_news, a.add_events').fadeOut();				
		});
		
		$('.adminedit').hide();
		$(".posts").mouseenter(function(){			
				$(this).children('.adminedit').fadeIn('fast');															 
		});
		$(".posts").mouseleave(function(){											 
				$(this).children('.adminedit').fadeOut('fast');				
		});
		
		$(".full_posts").mouseenter(function(){			
				$(this).children('.adminedit').fadeIn('fast');															 
		});
		$(".full_posts").mouseleave(function(){											 
				$(this).children('.adminedit').fadeOut('fast');				
		});	
	
		$('.admincatedit').hide();
		$(".cattitle").mouseenter(function(){			
				$(this).children('.admincatedit').fadeIn('fast');															 
		});
		$(".cattitle").mouseleave(function(){											 
				$(this).children('.admincatedit').fadeOut('fast');				
		});
	
		$('.admincommentedit').hide();
		$(".full_comments").mouseenter(function(){			
				$(this).children('.admincommentedit').fadeIn('fast');															 
		});
		$(".full_comments").mouseleave(function(){											 
				$(this).children('.admincommentedit').fadeOut('fast');				
		});
		
		$('.edit_blog_cat, .delete_blog_cat, .handle').hide();
		$("ul.BlogNav li").mouseenter(function(){			
				$(this).children('a.edit_blog_cat, a.delete_blog_cat, .handle').fadeIn('fast');															 
		});
		$("ul.BlogNav li").mouseleave(function(){											 
				$(this).children('a.edit_blog_cat, a.delete_blog_cat, .handle').fadeOut('fast');				
		});	
		
		$("#Nav a").mouseenter(function(){			
				$(this).children('.nav_handle').fadeIn('fast');															 
		});
		$("#Nav a").mouseleave(function(){											 
				$(this).children('.nav_handle').fadeOut('fast');				
		});		
		
		$('#contact_reason_list').hide();		
		$('.list_reason').click(function(){
				if ($('#contact_reason_list').is(':visible')) { 
				 $('#contact_reason_list').fadeOut();
				 $('a.list_reason').removeClass('active');
			 }else if ($('#contact_reason_list').is(':hidden')){
				 $('#contact_reason_list').fadeIn();
				 $('a.list_reason').addClass('active');
				}	
				return false;
		});

		// Login
		$('.login').fancybox({
			'type'			: 'iframe',	
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'fade',
			'speedIn'		:	600, 
			'speedOut'		:	200,			
			'height'		:	280,
			'width'		:	520,
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': false,
			'scrolling' : 'no',
			'titleShow'  : false,
			'modal'   : true
		});		

		// Create user
		$('.create_user').fancybox({
			'type'			: 'iframe',	
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',			
			'height'		:	420,
			'width'		:	680,
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': false,
			'scrolling' : 'no',
			'titleShow'  : false,
			'modal'   : true
		});	

		// Change password
		$('.change_password').fancybox({
			'type'			: 'iframe',	
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',			
			'height'		:	365,
			'width'		:	353,
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': false,
			'scrolling' : 'no',
			'titleShow'  : false,
			'modal'   : true
		});	

		// Add page content
		$('.add_page_content, .edit_page_article, .add_news, .edit_news, .add_events, .edit_event, .add_blog_post, .editpost').fancybox({
			'type'			: 'iframe',	
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',			
			'height'		:	'100%',
			'width'		:	'100%',
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'scrolling' : 'no',
			'titleShow'  : false,
			'showCloseButton'   : true,
			'overlayShow'	:	false,			
			'onClosed': function() { parent.location.reload(true); }
		});	

		// Add single field box
		$(".add_blog_cat, .edit_blog_cat").fancybox({
			'type'			: 'iframe',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',			
			'autoDimensions' : true,
			'autoScale' : true,
			'height' : 175,
			'width' : 430,
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'scrolling' : 'no',
			'titleShow'  : false,
			'overlayShow'	:	false,			
			'showCloseButton'   : false
		});	

		// Add edit contact reason
		$('.add_contact_reason, .edit_contact_reason').fancybox({
			'type'			: 'iframe',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',			
			'autoDimensions' : true,
			'autoScale' : true,
			'height' : 175,
			'width' : 430,
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'scrolling' : 'no',
			'titleShow'  : false,
			'overlayShow'	:	false,			
			'showCloseButton'   : false
		});	
				
		// Add edit page content
		$('.add_page, .edit_page').fancybox({
			'type'			: 'iframe',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'height'		:	480,
			'width'		:	570,
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'scrolling' : 'no',
			'titleShow'  : false,
			'overlayShow'	:	false,
			'showCloseButton'   : true,
			'onClosed': function() { parent.location.reload(true); } 
		});	
		
		// Add edit site info
		$('.edit_info').fancybox({
			'type'			: 'iframe',														
			'height'		:	480,
			'width'		:	370,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'scrolling' : 'no',
			'titleShow'  : false,
			'overlayShow'	:	false,
			'onClosed': function() { parent.location.reload(true); } 
		});
	
		// Add edit site info
		$('.contact_reason').fancybox({
			'type'			: 'iframe',														
			'height'		:	350,
			'width'		:	520,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'padding'			:    10,
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'scrolling' : 'no',
			'titleShow'  : false,
			'overlayShow'	:	false,		
			'onClosed': function() { parent.location.reload(true); } 
		});	
		
		// Single image
		$('a.single_image').fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'titlePosition' :	'over',
			'overlayShow'	:	false								 								 
		});
	
		// Grouped image by rel="group1"
		$('a.gallery').fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'titleShow' :	false,
			'overlayShow'	:	false
		});		
		
		$('.contactinput, .contactemailinput, select, .contacttextarea').focus(function(){
				$(this).css({border: '2px solid #eb9507'});		
		});
		
		$('.contactinput, .contactemailinput, select, .contacttextarea').blur(function(){
				$(this).css({border: '2px solid #cccccc'});		
		});			

		$("#start_date, #end_date, #date").datepicker({ dateFormat: 'yy-mm-dd' });
		
		$("a.anchorLink").anchorAnimate();
		
		$('#Nav').sortable({
				opacity: '0.5',
				handle : '.nav_handle',
				update: function(e, ui){
					order = $(this).sortable("serialize");
					$.ajax({
									url: "/ajax/process_sortable_navigation/",
									type: "POST",
									data: order,
									success: function(data){												
										$('#extra_message').html('<div class="global_message">Navigation order has been updated!</div>');
										$('.global_message').delay(5000).fadeOut('slow');
									}
									},'json');
							}
			});
		
		///////////////////////////// start blog functions ///////////////////////////////////
		$("#comments_form").submit(function() {
			var comment_author = $("#comment_author").val(); 
			var email = $("#email").val(); 
			var comment_body = $("#comment_body").val(); 
			var ip = $("#ip").val(); 
			var post_id = $("#post_id").val(); 
			var comment_date = $("#comment_date").val(); 
			var comment_status = $("#comment_status").val(); 
			$.post("/blog/comment", { 
				 comment_author:comment_author, 
				 email:email, 
				 comment_body:comment_body, 
				 ip:ip, 
				 post_id:post_id, 
				 comment_date:comment_date, 
				 comment_status:comment_status 
				},function(data){
					$("#comment_author_error").html(data.comment_author);
					$("#email_error").html(data.email);
					$("#comment_body_error").html(data.comment_body);				
					if(data.success == true){					
							$('#comments_form').fadeOut(1000, function() {
									$('.commentform').html('<h2>Your comment was sent succesfully!</h2>');														
								});
					}					
				},'json');
				return false;
		});	
		
		
	$('.BlogNav').sortable({
					opacity: '0.5',
					handle: '.handle',
					update: function(e, ui){
							order = $(this).sortable("serialize");
							$.ajax({
											url: "/blog/process_sortable_categories/",
											type: "POST",
											data: order,
											success: function(data){												
												$('#extra_message').html('<div class="global_message">Blog category order has been updated!</div>');
												$('.global_message').delay(5000).fadeOut('slow');
											}
											},'json');
									}
			});	

		$(".view_count").click(function() {																		
			var id = $(this).attr("id");													  
			$.post("/blog/post_view_count", { id:id });
		});
		
		///////////////////////////// custom functions ///////////////////////////////////
		
		
	$(".tweet").tweet({
			username: "developer_7",
			join_text: "auto",
			avatar_size: 32,
			count: 3,
			auto_join_text_default: "",
			auto_join_text_ed: "we",
			auto_join_text_ing: "we were",
			auto_join_text_reply: "we replied to",
			auto_join_text_url: "we were checking out",
			loading_text: "loading tweets..."
	});
		
		
		var interval;		
		
		$('#home_featured_sites ul').roundabout({
			easing: 'easeOutInCirc',
			duration: 600
			
		});

		$('ul#folio_scroller').roundabout({
			easing: 'easeOutInCirc',
			shape: 'waterWheel',
			duration: 600
		});

			interval = startAutoPlay();

			function startAutoPlay() {
				return setInterval(function() {																		
				var count = $('#home_featured_sites ul, ul#folio_scroller').children().length;
					if(count > 1){							
						$('#home_featured_sites ul, ul#folio_scroller').roundabout_animateToNextChild();					
					}
				}, 6000);
			}
		
		// Add edit contact reason
		$('a.contact').fancybox({
			'type'			: 'ajax',
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',		
			'autoDimensions' : true,
			'autoScale' : true,
			'padding'			:    0,
			'hideOnContentClick': false,
			'hideOnOverlayClick': false,
			'scrolling' : 'no',
			'titleShow'  : false,
			'overlayShow'	:	true,			
			'showCloseButton'   : true
		});			
	
 }); // end functions
	
	
	(function($) {
	 
		$.fn.tweet = function(o){
			var s = {
				username: ["developer_7"],              
				list: null,                              
				avatar_size: null,                      
				count: 3,                               
				intro_text: null,                       
				outro_text: null,                       
				join_text:  null,                       
				auto_join_text_default: "i said,",      
				auto_join_text_ed: "i",                 
				auto_join_text_ing: "i am",             
				auto_join_text_reply: "i replied to",   
				auto_join_text_url: "i was looking at", 
				loading_text: null,                     
				query: null                             
			};
			
			if(o) $.extend(s, o);
			
			$.fn.extend({
				linkUrl: function() {
					var returning = [];
					var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
					this.each(function() {
						returning.push(this.replace(regexp,"<a href=\"$1\">$1</a>"));
					});
					return $(returning);
				},
				linkUser: function() {
					var returning = [];
					var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
					this.each(function() {
						returning.push(this.replace(regexp,"<a href=\"http://twitter.com/$1\">@$1</a>"));
					});
					return $(returning);
				},
				linkHash: function() {
					var returning = [];
					var regexp = /(?:^| )[\#]+([A-Za-z0-9-_]+)/gi;
					this.each(function() {
						returning.push(this.replace(regexp, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'));
					});
					return $(returning);
				},
				capAwesome: function() {
					var returning = [];
					this.each(function() {
						returning.push(this.replace(/\b(awesome)\b/gi, '<span class="awesome">$1</span>'));
					});
					return $(returning);
				},
				capEpic: function() {
					var returning = [];
					this.each(function() {
						returning.push(this.replace(/\b(epic)\b/gi, '<span class="epic">$1</span>'));
					});
					return $(returning);
				},
				makeHeart: function() {
					var returning = [];
					this.each(function() {
						returning.push(this.replace(/(&lt;)+[3]/gi, "<tt class='heart'>&#x2665;</tt>"));
					});
					return $(returning);
				}
			});
	
			function parse_date(date_str) {
				return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
			}
	
			function relative_time(time_value) {
				var parsed_date = parse_date(time_value);
				var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
				var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
				var pluralize = function (singular, n) {
					return '' + n + ' ' + singular + (n == 1 ? '' : 's');
				};
				if(delta < 60) {
				return 'less than a minute ago';
				} else if(delta < (60*60)) {
				return '' + pluralize("minute", parseInt(delta / 60)) + ' ago';
				} else if(delta < (24*60*60)) {
				return '' + pluralize("hour", parseInt(delta / 3600)) + ' ago';
				} else {
				return '' + pluralize("day", parseInt(delta / 86400)) + ' ago';
				}
			}
	
			function build_url() {
				var proto = ('https:' == document.location.protocol ? 'https:' : 'http:');
				if (s.list) {
					return proto+"//api.twitter.com/1/"+s.username[0]+"/lists/"+s.list+"/statuses.json?per_page="+s.count+"&callback=?";
				} else if (s.query == null && s.username.length == 1) {
					return proto+'//api.twitter.com/1/statuses/user_timeline.json?screen_name='+s.username[0]+'&count='+s.count+'&callback=?';
				} else {
					var query = (s.query || 'from:'+s.username.join(' OR from:'));
					return proto+'//search.twitter.com/search.json?&q='+escape(query)+'&rpp='+s.count+'&callback=?';
				}
			}
	
			return this.each(function(i, widget){
				var list = $('<ul class="tweet_list">').appendTo(widget);
				var intro = '<p class="tweet_intro">'+s.intro_text+'</p>';
				var outro = '<p class="tweet_outro">'+s.outro_text+'</p>';
				var loading = $('<p class="loading">'+s.loading_text+'</p>');
	
				if(typeof(s.username) == "string"){
					s.username = [s.username];
				}
	
				if (s.loading_text) $(widget).append(loading);
				$.getJSON(build_url(), function(data){
					if (s.loading_text) loading.remove();
					if (s.intro_text) list.before(intro);
					var tweets = (data.results || data);
					$.each(tweets, function(i,item){
						// auto join text based on verb tense and content
						if (s.join_text == "auto") {
							if (item.text.match(/^(@([A-Za-z0-9-_]+)) .*/i)) {
								var join_text = s.auto_join_text_reply;
							} else if (item.text.match(/(^\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+) .*/i)) {
								var join_text = s.auto_join_text_url;
							} else if (item.text.match(/^((\w+ed)|just) .*/im)) {
								var join_text = s.auto_join_text_ed;
							} else if (item.text.match(/^(\w*ing) .*/i)) {
								var join_text = s.auto_join_text_ing;
							} else {
								var join_text = s.auto_join_text_default;
							}
						} else {
							var join_text = s.join_text;
						};
	
						var from_user = item.from_user || item.user.screen_name;
						var profile_image_url = item.profile_image_url || item.user.profile_image_url;
						var join_template = '<span class="tweet_join"> '+join_text+' </span>';
						var join = ((s.join_text) ? join_template : ' ');
						var avatar_template = '<a class="tweet_avatar" href="http://twitter.com/'+from_user+'"><img src="'+profile_image_url+'" height="'+s.avatar_size+'" width="'+s.avatar_size+'" alt="'+from_user+'\'s avatar" title="'+from_user+'\'s avatar" border="0"/></a>';
						var avatar = (s.avatar_size ? avatar_template : '');
						var date = '<div class="tweet_time"><a href="http://twitter.com/'+from_user+'/statuses/'+item.id+'" title="view tweet on twitter">'+relative_time(item.created_at)+'</a></div>';
						var text = '<div class="tweet_text">' +$([item.text]).linkUrl().linkUser().linkHash().makeHeart().capAwesome().capEpic()[0]+ '</div>';
	
						list.append('<li>' + avatar + text + join + date + '</li>');
	
						list.children('li:first').addClass('tweet_first');
						list.children('li:odd').addClass('tweet_even');
						list.children('li:even').addClass('tweet_odd');
					});
					if (s.outro_text) list.after(outro);
					$(widget).trigger("loaded").trigger((tweets.length == 0 ? "empty" : "full"));
				});
	
			});
		};
	})(jQuery);
	
	
	// Begin Animated Scroll
	jQuery.fn.anchorAnimate = function(settings) {
		settings = jQuery.extend({
		speed : 1100
		}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
			return false;
			})
		});
	} 
