// JavaScript Document
// Author: Damjan Gataric, EtonDIGITAL (www.etondigital.com)


function loadPhoto(url) {
	 $.ajax({
	   type: "POST",
	   url: url,
	   success: function(data){
		 $('photo_here').html(data);
	   }
	 });

}

//alert when tagged person is not memeber of SB
function notMember(name) {
	jAlert(name+' is not yet member of StudentBox.','Information');
	return false;
}

//tagging events
function notAvailable() {
	jAlert('This feature is not yet available. Thank you for your understanding.','Information');
}

(function($){
    $.fn.whenLoaded = function(fn){
 
        var $elements = this,
            total = this.length;
 
        $elements.each(function(){
            $(this).load(function(){
                $(this).data('isLoaded',true);
            });
        });
 
        function check() {
            var loaded = 0;
            $elements.each(function(){
                if($(this).data('isLoaded')) {
                    loaded++;
                }
            });
            if(loaded===total) {
                fn.call($elements);
            } else {
                setTimeout(check,300);
            }
        }
 
        check();
    }
})(jQuery);


function addToFavorites(url, title){	//add to favorites
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
		else if(window.opera && window.print){ // opera
			var elem = document.createElement('a');
			elem.setAttribute('href',url);
			elem.setAttribute('title',title);
			elem.setAttribute('rel','sidebar');
			elem.click();
		}
		else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


$(document).ready(function() {
						   
   	$('div.disableTag') 
//    .livequery(function(){ 
//        $(this) 
//            .hover(function() { 
//            }, function() {
//            }); 
//    })
	.livequery(function(){ 
		$(this).click(function(evnt) { $('div.photo_info a.next').click(); });
	});

	
	//for voucher selecting and searching
	$('#voucherList li').livequery('click', function(event) {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
			$(this).children('input').removeAttr('checked');
		} else {
			$(this).addClass('selected');
			$(this).children('input').attr('checked','checked');
		}
	});
	
	$('#voucherFilterShow a.filterAll').livequery('click', function(event) {
		if (!$(this).hasClass('selected')) {
			$('#voucherFilterShow a').removeClass('selected');
			$(this).addClass('selected');
			$('#voucherList li').fadeIn(100);
		}
		return false;
	});
	
	$('#voucherFilterShow a.filterSelected').livequery('click', function(event) {
		if (!$(this).hasClass('selected')) {
			$('#voucherFilterShow a').removeClass('selected');
			$(this).addClass('selected');
			$.each($('#voucherList li'), function() {
				if (!$(this).hasClass('selected')) {
					$(this).fadeOut(100);
				}
			});
		}
		return false;
	});
	
	$('#voucherList li').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
	
	$('#voucherSearch').keyup(function() {
		var input = $(this).val();
		$.each($('#voucherList li'), function() {
			var name = $(this).children('span.itemName').text();
			var query = new RegExp(input, "i");
			if (query.test(name)) {
				$(this).fadeIn(150);
			} else {
				$(this).fadeOut(150);
			}
		});	
	});
	
	/////////////////////////////////////////////////////////////////////////
	
	
	//groups filter subtypes
	$('#group_types a').livequery('click', function(event) {
		$('ul#group_types a').removeClass('active');
		$(this).addClass('active');
		actType = $(this).attr('rev');
	});
	
	//groups filter show from
	$('#show_from input').click(function() {
		var url = $('#show_from').attr('rel');
		
		//old: url = url + '/' + $('#searchGroups').attr('rel') + '/' + $('#searchGroups input.search').attr('value');
		if($('#searchGroups input.search').attr('value')==="") {
			var searchValue = "non";
		} else {
			var searchValue = $('#searchGroups input.search').attr('value');
		}
		url = url + '/' + $('#searchGroups').attr('rel') + '/' + searchValue;
		
		if ($('#category_type').val()>0) {
			url = url + '/category/'+$('#category_type').val();
		}
		
		if ($('#group_types a').not(':first').hasClass('active')) {
			url = url + '/type/' + $('#group_types a.active').attr('rev');
		}
		
		$('#show_from input:checked').each(function() {
			var name = $(this).attr('name');
			url = url + '/' + name + '/1';
		});
		
		url = url + '/event-time/' + $('#date_time_params input:checked').val();
		
		$('#listHolder').html('<img src="/images/css/photo-loader.gif" class="list_loading" alt="loading" />');
		
		$.ajax({
			type: "GET",
			url: url,
			success: function(html){
			  $('#listHolder').children('img').fadeOut();
			  $('#listHolder').hide().html(html).slideDown(300);
			}
		});
	});
	
	//groups filter date time
	$('#date_time_params input').not('input.date-pick').click(function() {
		var url = $('#date_time_params').attr('rel');
		
		$('#date_time_params label.choose_date').text('Choose date');
		
		//old: url = url + '/' + $('#searchGroups').attr('rel') + '/' + $('#searchGroups input.search').attr('value');
		if($('#searchGroups input.search').attr('value')==="") {
			var searchValue = "non";
		} else {
			var searchValue = $('#searchGroups input.search').attr('value');
		}
		url = url + '/' + $('#searchGroups').attr('rel') + '/' + searchValue;
		
		if ($('#category_type').val()>0) {
			url = url + '/category/'+$('#category_type').val();
		}
		
		if ($('#group_types a').not(':first').hasClass('active')) {
			url = url + '/type/' + $('#group_types a.active').attr('rev');
		}
		
		$('#show_from input:checked').each(function() {
			var name = $(this).attr('name');
			url = url + '/' + name + '/1';
		});
		
		var name = $(this).val();
		url = url + '/event-time/' + name;
		
		$('#listHolder').html('<img src="/images/css/photo-loader.gif" class="list_loading" alt="loading" />');
		
		$.ajax({
			type: "GET",
			url: url,
			success: function(html){
			  $('#listHolder').children('img').fadeOut();
			  $('#listHolder').hide().html(html).slideDown(300);
			}
		});
	});
	
	//groups filter date by specific date
	$.filterByDate = function() {
		var url = $('#date_time_params').attr('rel');
		if($('#searchGroups input.search').attr('value')==="") {
			var searchValue = "non";
		} else {
			var searchValue = $('#searchGroups input.search').attr('value');
		}
		url = url + '/' + $('#searchGroups').attr('rel') + '/' + searchValue;
		
		if ($('#category_type').val()>0) {
			url = url + '/category/'+$('#category_type').val();
		}
		
		if ($('#group_types a').not(':first').hasClass('active')) {
			url = url + '/type/' + $('#group_types a.active').attr('rev');
		}
		
		$('#show_from input:checked').each(function() {
			var name = $(this).attr('name');
			url = url + '/' + name + '/1';
		});
		
		var name = $('#date_time_params input.date-pick').val();
		url = url + '/event-time/' + name;
		
		$('#listHolder').html('<img src="/images/css/photo-loader.gif" class="list_loading" alt="loading" />');
		
		$.ajax({
			type: "GET",
			url: url,
			success: function(html){
			  $('#listHolder').children('img').fadeOut();
			  $('#listHolder').hide().html(html).slideDown(300);
			}
		});
	}
  

	//for invite list
	$('#inviteList li').livequery('click', function(event) {
		if ($(this).hasClass('invited')) {
			jAlert('This person is already invited','Information');
		} else if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
			$(this).children('input').removeAttr('checked');
		} else {
			$(this).addClass('selected');
			$(this).children('input').attr('checked','checked');
		}
	});
	
	$('div.filterShow a.filterAll').livequery('click', function(event) {
		if (!$(this).hasClass('selected')) {
			$('div.filterShow a').removeClass('selected');
			$(this).addClass('selected');
			$('div#inviteList li').fadeIn(100);
		}
		return false;
	});
	
	$('div.filterShow a.filterSelected').livequery('click', function(event) {
		if (!$(this).hasClass('selected')) {
			$('div.filterShow a').removeClass('selected');
			$(this).addClass('selected');
			$.each($('div#inviteList li'), function() {
				if (!$(this).hasClass('selected')) {
					$(this).fadeOut(100);
				}
			});
		}
		return false;
	});
	
	$('div#inviteList li').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
	
	$('input#inviteSearch').keyup(function() {
		var input = $(this).val();
		$.each($('div#inviteList li'), function() {
			var name = $(this).children('span.memberName').text();
			var query = new RegExp(input, "i");
			if (query.test(name)) {
				$(this).fadeIn(200);
			} else {
				$(this).fadeOut(200);
			}
		});	
	});
	
	/////////////////////////////////////////////////////////////////////////
	
	
	
	// Groups registration form /////////////////////////////////////////////
	
	$('input#customize_enable_photos').parent('div').next('div').hide();
	$('input#customize_enable_photos', this).click(function() {
		if (this.checked == true) {
			$(this).parent('div').next('div').slideDown();
		} else {
			$(this).parent('div').next('div').slideUp();
		}
	});
	
	$('input#customize_enable_videos').parent('div').next('div').hide();
	$('input#customize_enable_videos', this).click(function() {
		if (this.checked == true) {
			$(this).parent('div').next('div').slideDown();
		} else {
			$(this).parent('div').next('div').slideUp();
		}
	});
	
	$('input#customize_enable_links').parent('div').next('div').hide();
	$('input#customize_enable_links', this).click(function() {
		if (this.checked == true) {
			$(this).parent('div').next('div').slideDown();
		} else {
			$(this).parent('div').next('div').slideUp();
		}
	});
	
	$('form.group_form').submit(function() { 
		if (!$('input#managelogo_certify').attr('checked')) {
			return false; 
		}
	});
	
	
	
	//city autocomplete////////////////////////////////////////////////////////
	
	$('input.autocomplete_city').keyup(function () {
		var t = this;
		var $autocomplete_city = $(this);
		var $cityList = $(this).siblings('ul.cityList');
		
		if(this.value!='') { 
			$(this).css('background','#fff url(/images/css/city-loader.gif) right no-repeat'); 
		} else {
			$(this).css('background', '#fff');
		}
		if (this.value != this.lastValue) {
		  if (this.timer) clearTimeout(this.timer);
		  //validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
		  this.timer = setTimeout(function () {
			var url = $('input.autocomplete_city_url').val();
			$.ajax({
				type: "POST",
				url: url,
				data: 'city='+t.value,
				success: function(data) { 
					if (data=='') { 
						$cityList.fadeOut(150); 
						
					} else { 
						$cityList.html(data).slideDown(200);
					}
					$autocomplete_city.css('background', '#fff');
				}
			  });
		  }, 200);
		  this.lastValue = this.value;
		}
	});
		
	$('ul.cityList a').livequery('click', function(event) {
		var data = $(this).attr('title');
		var id = $(this).attr('id');
		$(this).parent('li').parent('ul.cityList').siblings('input.autocomplete_city').val(data);
		$(this).parent('li').parent('ul.cityList').siblings('input.autocomplete_city_id').val(id);
		$(this).parent('li').parent('ul.cityList').fadeOut(150);
		return false;
	});
	
	
	/////////////////////////////////////////////////////////////////////////
	
	$('form#post_link input:submit').click(function() {
		var postedLink = $('input#link_for_post').val(); 
		var protocol = postedLink.slice(0,7);
		//console.log(protocol);
		if (protocol!="http://") {
			postedLink = 'http://' + postedLink;
		}
		var actionUrl = $('form#post_link').attr('action');
		var url = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
		if (url.test(postedLink)) {	
			$('input#link_for_post').val(postedLink);
			jQuery.ajax({
				type: 'GET',
				url: actionUrl + '?link='+postedLink,
				success: function(isLive) {
					//console.log(isLive);
					if (isLive=="TRUE") {
						//console.log('live');
						$('form#post_link').submit();					
					} else {
						//console.log('dead');
						jAlert('Posted link is not live.','Information');
					}
				}
			});
			//console.log('send');
		} else {
			jAlert('Posted link is not valid.','Information');
			//console.log('invalid');
			return false;
		}
	});
	
	//Listing////////////////////////////////////////////////////////////////
	$('a.joinGroup').livequery('click', function(event) {
		var url = $(this).attr('href');
		if ($(this).hasClass('closed')) {
			jAlert('This is a closed group, you must wait for administrators permission in order to join.', 'Information');
		} else {
			jConfirm('Do you want to join this group?', 'Please Confirm', function(res) {
				if (res) {	window.location = url; } 
			});
		}
		return false;
	});
	$('a.leaveGroup').livequery('click', function(event) {
		var url = $(this).attr('href');
		jConfirm('Do you want to leave this group?', 'Please Confirm', function(res) {
			if (res) {	window.location = url; } 
		});
		return false;
	});
	/////////////////////////////////////////////////////////////////////////
						   
	
	$('ul#sidebar_navig li.my_profile').hover(
		function() { $(this).children('a.editProfile').show(); },
		function() { $(this).children('a.editProfile').hide(); }
	);
	
	$('a.sendMessage').livequery('click', function(event) {
		var recipientId = $(this).attr('id');
		var senderId = $(this).attr('rel');
		var userName = $(this).attr('title');
		var form = '<form name="sendMessageForm" class="sendMessage" action=""><label for="subject">Subject</label><input type="text" name="subject" class="txt" />'
		form = form + '<label for="message">Your message</label><textarea cols="10" rows="6" name="message"></textarea>'
		form = form + '<input type="hidden" name="recipientId" value="'+recipientId+'" /><input type="hidden" name="senderId" value="'+senderId+'" /></form>'
		jSendMessage(form,'Send message to '+userName);
		return false;
	});
	
	$('a.sendMessageToGroup').livequery('click', function(event) {
		var recipientId = $(this).attr('id');
		var senderId = $(this).attr('rel');
		var groupName = $(this).attr('title');
		var form = '<form name="sendMessageForm" class="sendMessage" action=""><label for="subject">Subject</label><input type="text" name="subject" class="txt" />'
		form = form + '<label for="message">Your message</label><textarea cols="10" rows="6" name="message"></textarea>'
		form = form + '<input type="hidden" name="recipientId" value="'+recipientId+'" /><input type="hidden" name="senderId" value="'+senderId+'" /></form>'
		jSendMessageToGroup(form,'Send message to '+groupName);
		return false;
	});
	
	$('a.sendMessageToEvent').livequery('click', function(event) {
		var recipientId = $(this).attr('id');
		var senderId = $(this).attr('rel');
		var eventName = $(this).attr('title');
		var form = '<form name="sendMessageForm" class="sendMessage" action=""><label for="subject">Subject</label><input type="text" name="subject" class="txt" />'
		form = form + '<label for="message">Your message</label><textarea cols="10" rows="6" name="message"></textarea>'
		form = form + '<input type="hidden" name="recipientId" value="'+recipientId+'" /><input type="hidden" name="senderId" value="'+senderId+'" /></form>'
		jSendMessageToEvent(form,'Send message to all members');
		return false;
	});
	
	
	$('a.addToFavorites').livequery('click', function(event) {
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		addToFavorites(url, title);
		return false;
	});
						   
	$('a.accountExpand').toggle(
		function() {
			$(this).parent('h2').next('div').slideDown();
			$(this).next('span').text('Click here to hide');
			$(this).css('background-image','url(../../images/css/add.png)');
		},
		function() {
			$(this).parent('h2').next('div').slideUp();
			$(this).next('span').text('Click here to show');
			$(this).css('background-image','url(../../images/css/delete.png)');
		}
	);
	$('a.accountExpand').hover(
		function() { $(this).next('span').show(); },
		function() { $(this).next('span').hide(); }
	);
						   
	$.each($('ul.user_data_ul li'), function() {  //hide user information if there is no data entered
		if ($(this).children('span:last').text()=='') { 
			$(this).hide();
		}
	});
						   
	$.each($('textarea'), function() {
		var height = $(this).height();
		$(this).css('min-height', height);
	});
//	$('textarea').autogrow();
	$('textarea').css('line-height', '14px').autogrow({ lineHeight: 14 });
	
	
	$('form#blogForm button.submitBlogForm').click(function() {
		if ($('form#blogForm input#title').val()=="") {
			jAlert('You must enter title of the post.','Information');
			return false;
		} else {
			$('form#blogForm').submit();
		}
	});
	
	$('form#importContactsForm').submit(function() {
		$('form#importContactsForm div.ctrlHolder:last').prepend("<img src='/images/css/ajax-loader-inverse.gif' class='import_contacts_loader' alt='loading' />");
		jQuery.ajax({type:'POST',dataType:'html',data:jQuery(this).serialize(),success:function(data, textStatus){jQuery('#invite_form_container').html(data); $('img.import_loader').fadeOut(150,function() {$(this).remove()})},url:'/index.php/contacts/importSelect'}); 
		return false;
	});
	
	$('a.removeContact').livequery('click', function(event) {
		$(this).parent('li').parent('ul').fadeOut(250, function() {$(this).remove()});
	});
														  
//	$.each($('div.public_blog_text img'), function() {
//		$(this).load(function() {
//			var width = $(this).width();
//			if (width>'340') {
//				$(this).attr('width','340');
//			}
//		});
//	});
//	
//	$.each($('div.blog_post img'), function() {
//		$(this).load(function() {
//			var width = $(this).width();
//			if (width>'420') {
//				$(this).attr('width','420');
//			}
//		});
//	});
	

	
	//clear default value of any input where class "clearValue" is used. Don't forget to use "alt" attribut too, for default value!
	$('input.clearValue').livequery('focus', function(event) {
		var oldValue = $(this).attr('alt');
		var curValue = $(this).val();
		if (oldValue==curValue) {
			$(this).val('');
		}
	});
	$('input.clearValue').livequery('blur', function(event) {
		var oldValue = $(this).attr('alt');
		var curValue = $(this).val();
		if (curValue=='') {
			$(this).val(oldValue);
		}
	});
						   
	$('li.show-balloon').hover(
		function () {
			var photoLi = $(this);
			balloonTimeOut = setTimeout(function() { photoLi.children('span.balloon-tip').animate({opacity: 'show', bottom: '138'}, 'slow'); }, 150);			
		}, 
		function () {
			if(balloonTimeOut) { clearTimeout(balloonTimeOut); }
			$(this).children('span.balloon-tip').animate({opacity: 'hide', bottom: '148'}, 150);
		}
	);
	
	//for gallery: single photo: prev and next photo
	$('div.photo_info a.prev').livequery('click', function(event) {
		if ($(this).attr('title')=='tagging') {
			jConfirm('If you leave now, tags will not be saved.<br />Do you want to leave?', 'Please Confirm', function(res) {
				if (res) {
					$('a.prev').attr('title','').click();
				} else {
					return false;
				}
			});
		} else {
			window.onbeforeunload = null;
			$('div.jcrop-holder').css('background-color','#fff');
			var url = $(this).attr('rel');
			$('div#single_photo_content div.photo_here img.photo').css('opacity','0.0');
			$('div#single_photo_content div.photo_here').append("<img class='photo_loading' src='/images/css/photo-loader.gif' alt='loading' />");
			$.ajax({ type: 'POST', url: url, success: function(data){ $('img.photo_loading').fadeOut(); $('div#single_photo').html(data); } });
		}
		return false;
	});
	$('div.photo_info a.next').livequery('click', function(event) {
		if ($(this).attr('title')=='tagging') {
			jConfirm('If you leave now, tags will not be saved.<br />Do you want to leave?', 'Please Confirm', function(res) {
				if (res) {
					$('a.next').attr('title','').click();
				} else {
					return false;
				}
			});
		} else {
			window.onbeforeunload = null;
			$('div.jcrop-holder').css('background-color','#fff');
			var url = $(this).attr('rel');
			$('div#single_photo_content div.photo_here img.photo').css('opacity','0.0');
			$('div#single_photo_content div.photo_here').append("<img class='photo_loading' src='/images/css/photo-loader.gif' alt='loading' />");
			$.ajax({ type: 'POST', url: url, success: function(data){ $('img.photo_loading').fadeOut(); $('div#single_photo').html(data); } });
		}
		return false;
	});
	
	//for member profile modal box: prev and next member
//	$('div#popup_message div.controls a.prev').livequery('click', function(event) {
//		var url = $(this).attr('href');
//		$.ajax({ type: 'POST', url: url, success: function(data){ $('img.photo_loading').fadeOut(); $('div#popup_container').html(data); } });
//		return false;
//	});
//	$('div#popup_message div.controls a.next').livequery('click', function(event) {
//		var url = $(this).attr('href');
//		$.ajax({ type: 'POST', url: url, success: function(data){ $('img.photo_loading').fadeOut(); $('div#popup_container').html(data); } });
//		return false;
//	});
	
	
	//comment sender for gallery
	$('input#submit_comment').livequery('click', function(event) {
		if ($('#multy_text_area').val()=="") {
			jAlert('You must write something.', 'Error');
		} else {
			var data = $('#comment_form').serialize();
			data = data.replace(/%0A/g,' '); //replace all enters and returns with space
			var url = $(this).attr('alt');
			url = url + data; //alert(url);
			$.ajax( {type: 'POST', url: url, success: function(html) { 
			if (html != 'false') { 
				var cont = $('#comment_area').html(); 
				$('#comment_area').html(html + cont); 
				var noComm = $('#comment_area').children().size()-1;
				if (noComm < 1) $('#comment_number').text('No comments so far'); 
				else if (noComm < 2) $('#comment_number').text('One comment so far');
			else $('#comment_number').text(noComm + ' comments so far'); } reportSuccess('Your comment has been added.'); 
				$.scrollTo('#comment_number', 800); $('#comment_form').clearForm(); } 
			}); 
		}
		$('#multy_text_area').val('');
		return false;
	});
	

	//for blog
	$('div.public_blog_sort_options select.filter_posts').change(function() {
		$('div.posts_holder').html("<img alt='loading posts' src='/images/css/photo-loader.gif' class='loading_posts' />");
		url = $(this).children('option:selected').val();
		$.ajax( {type: 'POST', url: url, success: function(html) { $('img.loading_posts').fadeOut(); $('div.posts_holder').html(html); } } );
	});
	
//	$('div.posts_holder span.prev_next_post a').livequery('click', function(event) {
//	   //alert('aa');
//		$('div.posts_holder').html("<img alt='loading posts' src='/images/css/photo-loader.gif' class='loading_posts' />");
//		var url = $(this).attr('href');
//		$.ajax({ type: 'GET', url: url, success: function(html){ $('img.loading_posts').fadeOut(); $('div.posts_holder').html(html); } });
//		return false;
//	});
	
	
  $('a.aMiniFeedAllCommentsAjax').toggle(
    function(){ 
      var more = $(this).children('span').length;
      if (more==1) { //check does more exists
	      var id = $(this).attr('rel');
	      var commentHolder_id = '#commentHolder_'+ id;
	      $(commentHolder_id).html('<img class=\'comments-loader\' src=\'/images/css/comments-loader.gif\' alt=\'loading\'/>');
	      $(commentHolder_id).load('/sfComment/commentListByAjax?model=MiniFeedLog&modelID='+id, function(){ $(this).children('img').fadeOut('fast'); $(this).children('div.comment_area').fadeIn(1000); });
	      $(this).children().text('(less)');
	      return false;
      }
    },
    function(){
	    var id = $(this).attr('rel');
	    var commentHolder_id = '#commentHolder_'+ id;
	    $(commentHolder_id).children('div').children('div').slice(1).fadeOut('slow');
	    $(this).children().text('(more)');
      return false;
    }
  );
  
  $('input#editlogo').change(function() {
    var a = $(this).val(); 
    var dot = a.lastIndexOf("."); 
    var b = a.substr(dot,a.length); 
    if (b==".gif" || b==".GIF" || b==".jpg" || b==".JPG" || b==".png" || b==".PNG") {
      $('form#EditLogoForm').submit();
    } else {
      jAlert("Not an image!","Error");
    }
  });
  
  $('div.photo_here img.photo').bind("mouseenter",function(){
    $('div.photo_here a').css('display','block');
    var photoHeight = $('div.photo_here img.photo').height();
    $('div.photo_here a').height(photoHeight);
    var photoWidth = $('div.photo_here img.photo').width();
    $('div.photo_here').width(photoWidth+15);
  });
	
	
	$('div#ajax_load_div').hide();
	
	$('ul#globalnav').droppy();
	$('ul#welcome').droppy();
	
	
	
	if ($('ul.photo_carousel').is('*')) { //if carousel exists begin
		
		function mycarousel_itemLoadCallback(carousel, state)
		{
			// Check if the requested items already exist
			if (carousel.has(carousel.first, carousel.last)) {
				return;
			}
		
			jQuery.get(
				'/js/carousel_dynamic_ajax.php',
				{
					first: carousel.first,
					last: carousel.last
				},
				function(xml) {
					mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
				},
				'xml'
			);
		};
		
		function mycarousel_itemAddCallback(carousel, first, last, xml)
		{
			// Set the size of the carousel
			carousel.size(parseInt(jQuery('total', xml).text()));
		
			jQuery('image', xml).each(function(i) {
				carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text()));
			});
		};
		
		/**
		 * Item html creation helper.
		 */
		function mycarousel_getItemHTML(url)
		{
			return '<a href=""><img src="' + url + '" alt="sunset" class="photos_li" /></a>';
		};

		
		$('ul.photo_carousel').jcarousel({
			//itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
			itemLoadCallback: mycarousel_itemLoadCallback,
			scroll: 5
		});
		
		$('div.jcarousel-container').hide();
		
		$('a.quick-view').toggle(
			function(){$(this).parent().parent().parent().siblings('div.jcarousel-container').slideDown();},
			function(){$(this).parent().parent().parent().siblings('div.jcarousel-container').slideUp();}
		);
			
	} //if carousel exists end
	

	$('#sel_all').click(function(){ $('li.photo_border input:checkbox').attr('checked','checked'); return false; });
	$('#desel_all').click(function(){$('li.photo_border input:checkbox').removeAttr('checked'); return false; });
	
	$('#profile_image').bind('mouseenter',function() {
		$('#profile_image').children('a.profile_image_edit').fadeIn();
	});
	$('#profile_image').bind('mouseleave',function() {
		var active = $('a.profile_image_edit').hasClass('profile_image_edit_active');
		if (!active) {
			$('#profile_image').children('a.profile_image_edit').hide();
		}
	});
	
	$('a.profile_image_edit').click(function() { 
		var active = $(this).hasClass('profile_image_edit_active');
		if (active) {
			$(this).removeClass('profile_image_edit_active').text('Change Picture'); 
			$('div.profile_image_edit').hide(); 
		} else {
			$(this).addClass('profile_image_edit_active').text('Edit Your Profile Picture'); 
			$('div.profile_image_edit').slideDown();
		}
		return false;
	});
	
	//first hide all icons for edit, then show only on mouse over
	$('span.edit_2').css('opacity','0');
	$('div.user_data_group').bind('mouseenter',function() {
		$(this).children('span.edit_2').animate({opacity: '1'},300);
	});
	$('div.user_data_group').bind('mouseleave',function() {
		$(this).children('span.edit_2').stop().css('opacity', '0');
	});
	
	$('div.public_blog_text img, div.blog_post img').css('opacity','0');

});

$(window).load(function() {
	$.each($('div.public_blog_text img'), function() {
		var width = $(this).width();
		if (width>'340') {
			$(this).attr('width','340');
		}
		$(this).css('opacity','1');
	});
	
	$.each($('div.blog_post img'), function() {
		var width = $(this).width();
		if (width>'420') {
			$(this).attr('width','420');
		}
		$(this).css('opacity','1');
	});
});


$.fn.droppy = function(options) {
    
  options = $.extend({speed: 200}, options || {});
  
  this.each(function() {
    
    var root = this, zIndex = 1000;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).fadeOut(options.speed);
        }
      }, 150);
    }
  
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).fadeIn(options.speed);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
    
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('active'); $('> a', this).addClass('active'); },
      function() { $(this).removeClass('active'); $('> a', this).removeClass('active'); }
    );
    
  });
  
};



(function(jQuery) {
		  
	var self = null;
 
	jQuery.fn.autogrow = function(o)
	{	
		return this.each(function() {
			new jQuery.autogrow(this, o);
		});
	};
	

    /**
     * The autogrow object.
     *
     * @constructor
     * @name jQuery.autogrow
     * @param Object e The textarea to create the autogrow for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/autogrow
     */
	
	jQuery.autogrow = function (e, o)
	{
		this.options		  	= o || {};
		this.dummy			  	= null;
		this.interval	 	  	= null;
		this.line_height	  	= this.options.lineHeight || parseInt(jQuery(e).css('line-height'));
		this.min_height		  	= this.options.minHeight || parseInt(jQuery(e).css('min-height'));
		this.max_height		  	= this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
		this.textarea		  	= jQuery(e);
		
		if(this.line_height == NaN)
		  this.line_height = 0;
		
		// Only one textarea activated at a time, the one being used
		this.init();
	};
	
	jQuery.autogrow.fn = jQuery.autogrow.prototype = {
    autogrow: '1.2.2'
  };
	
 	jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
	
	jQuery.autogrow.fn.extend({
						 
		init: function() {			
			var self = this;			
			this.textarea.css({overflow: 'hidden', display: 'block'});
			this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() });
			this.checkExpand();	
		},
						 
		startExpand: function() {				
		  var self = this;
			this.interval = window.setInterval(function() {self.checkExpand()}, 400);
		},
		
		stopExpand: function() {
			clearInterval(this.interval);	
		},
		
		checkExpand: function() {
			
			if (this.dummy == null)
			{
				this.dummy = jQuery('<div></div>');
				this.dummy.css({
												'font-size'  : this.textarea.css('font-size'),
												'font-family': this.textarea.css('font-family'),
												'width'      : this.textarea.css('width'),
												'padding'    : this.textarea.css('padding'),
												'line-height': this.line_height + 'px',
												'overflow-x' : 'hidden',
												'position'   : 'absolute',
												'top'        : 0,
												'left'		 : -9999
												}).appendTo('body');
			}
			
			// Strip HTML tags
			var html = this.textarea.val().replace(/(<|>)/g, '');
			
			// IE is different, as per usual
			if ($.browser.msie)
			{
				html = html.replace(/\n/g, '<BR>new');
			}
			else
			{
				html = html.replace(/\n/g, '<br>new');
			}
			
			if (this.dummy.html() != html)
			{
				this.dummy.html(html);	
				
				if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height))
				{
					this.textarea.css('overflow-y', 'auto');	
				}
				else
				{
					this.textarea.css('overflow-y', 'hidden');
					if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height()))
					{	
						this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);	
					}
				}
			}
		}
						 
	 });
})(jQuery);