$(document).ready(function(){
	$('#secondary #search-form').enable_search_form();
	$('#secondary #search-form input#id_q').defaultValue('Search');
	$('form.make-busy').form_makes_busy();
	$('a[href=#]').click(function(){
		return false;
	});
	$('.click-to-print').click(function(){
		window.print();
	});
	$('li.has-submenu').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});	
	$('.submenu ul li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});	
	$('#products li a').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');	
	});
	$('.products-grid div').hover(function(){ 
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});	
	$('#account-summary li a').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});	
	$('.user-list li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});
	$('#browse-by-list li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});
	$('#browse-by-box').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});		
	$('ul.product-list li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});	
	$('ul.gamer-tag ul li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});	
	$('ul.gamer-tag li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});
	
	$('.user-list li:nth-child(8n)').addClass('eighth');
	$('.user-list li:nth-child(3n)').addClass('third');
	$('.user-list li:nth-child(2n)').addClass('second');
	
	$('.product-list li:nth-child(3n)').addClass('third');
	$('.product-list li:nth-child(2n)').addClass('second');	
	
	$('.products-list li:nth-child(3n)').addClass('third');
	$('.products-list li:nth-child(2n)').addClass('second');	
	
	$('#products li:nth-child(3n)').addClass('third');
	$('#products li:nth-child(2n)').addClass('second');		
	
	$('#content-footer p a').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});
	$('#content-header p a').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');		
	});	
	if ($('#sign-up-form').length > 0) $('#sign-up-form').registration_form();
	if ($('#registration-form').length > 0) $('#registration-form').registration_form();
	if ($('#status-text-form').length > 0) 	$('#status-text-form').status_form();
	if ($('#online-ids > ul > li').length > 0) 	$('#online-ids > ul > li').online_id_form();	
	if ($('.playing-form').length > 0) 	$('.playing-form').playing_form();	
	if ($('a.add-new-game-id').length > 0) 	$('a.add-new-game-id').add_online_id_form('#online-ids > ul');
	$('.product-images .image-right').forward_and_back_images();
	$('ul.photo-list li a').show_larger();
	$('a.add-friend, a.add-to-friends').has_dialog();
	$('form.blog-form').blog_form();
	$('a.show-mutual-friends-list').show_mutual_friends();
	$('input.id_for_photos').change(function(){
		if ($(this).attr('checked')) $('#sub-options-for-photos input').removeAttr('disabled');
		else $('#sub-options-for-photos input').attr('disabled', true);
	});
	$('input.id_for_photos').change(function(){
		if ($(this).attr('checked')) $('#sub-options-for-photos input').removeAttr('disabled');
		else $('#sub-options-for-photos input').attr('disabled', true);
	});
	$('input[name=access]').change(function(){
		if ($(this).val() == 1) $('select[name=required_level]').removeAttr('disabled');
		else $('select[name=required_level]').attr('disabled', true);
	});
	$('a.has-dialog').has_dialog();
	$('a.has-dangerous-dialog').has_dangerous_dialog();
	$('a.has-flag-dialog').has_dialog(function(data, new_ele, orig_ele){
		$(orig_ele).parent().addClass('flagged').html('Reported');
	});
	$('#newsletter-module form, .newsletter-signup form').form_has_appending(function(data){
		$('#newsletter-module').slideUp("slow");
	});
	$('form.has-appending').form_has_appending();
	$('#tell-friends input[name=friends_email]').defaultValue('Enter your friends email here');
	$('#newsletter-module input[name=email]').defaultValue('Enter your email here');
	$('#search-module input[name=query]').defaultValue('Enter your search terms here');
//	$('textarea#id_description, textarea#id_bio, textarea#id_comment, textarea#id_body, textarea#wall_body').autoResize();
	$(document).add_states();
});

$.fn.add_states = function(ele){
	return this.each(function(){
		$('.has-hover-state', this).hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
		if ($('.get_older_posts a', this).length) $('.get_older_posts a', this).fetch_older_posts();
		$('.has-up-and-down-states', this).enable_up_down_state();		
		$('a.end-block-ui', this).unblockeverything();		
	})
};

$.fn.unblockeverything = function(){
	return this.each(function(){
		$(this).click(function(){
			$.unblockUI();
			return false;
		});
	});
};

$.fn.unblock_body = function(){
	return this.each(function(){
		$(this).click(function(){
			$('body').unblock();
			return false;
		});
	});
};

$.fn.show_mutual_friends = function(){
	return this.each(function(){
		var ele = this;
		var td = $(this).parent().parent().parent();
		ele.shown = false;
		$(ele).click(function(){
			if (!ele.shown) {
				$('ul.inline-user-list', td).show('slow');				
				ele.shown = true;
			} else {
				$('ul.inline-user-list', td).hide('fast');				
				ele.shown = false;				
			}
			return false;
		})
	});
};

$.fn.enable_up_down_state = function(){
	return this.each(function(){
		var ele = this;
		ele.org_src = $(this).attr('src');
		ele.new_src = ele.org_src.replace(/down.gif$/gi, 'up.gif');
		if (ele.new_src == ele.org_src) {
			ele.new_src = ele.org_src.replace(/off.png$/gi, 'on.png');
		}
		$(this).hover(function(){
			$(this).attr('src', ele.new_src);
		}, function(){
			$(this).attr('src', ele.org_src);
		});
	});
};

$.fn.forward_and_back_images = function(){
	return this.each(function(){
		var imagesol = this;
		var imagelist = $('ol li', this);
		var current_image = 0;
		var max_images = imagelist.length-1;
		$('ul li.prev a', this).click(function(){
			current_image--;			
			if (current_image <= 0) current_image = max_images;
			$('ol li img', imagesol).css('display', 'none');			
			$('ol li:eq('+current_image+') img', imagesol).css('display', 'block');
			return false;
		});
		$('ul li.next a', this).click(function(){
			current_image++;			
			if (current_image >= max_images) current_image = 0;
			$('ol li img', imagesol).css('display', 'none');			
			$('ol li:eq('+current_image+') img', imagesol).css('display', 'block');
			return false;
		});		
	});
};

$.fn.fetch_older_posts = function(){
	return this.each(function(){
		var href = $(this).attr('href');
		var link = this;
		var parent = $(this).parent().parent();
		$(this).unbind('click').click(function(){
			var data = $.getJSON(href, function(data, textStatus){
				if (textStatus == "success") {
					if (data.html) {
						$('#wall-posts', parent).append(data.html);
						if (data.new_url) $(link).attr('href', data.new_url);
						else $(link).remove();
						$(link).fetch_older_posts();
						$('#wall-posts .item.first').removeClass('first');
						$('#wall-posts .item.last').removeClass('last');
						$('#wall-posts .item:first').addClass('first');
						$('#wall-posts .item:last').addClass('last');
					}
				}
			});
			return false;
		});
	});
};

$.fn.show_larger = function(){
	return this.each(function(){
		$(this).click(function(){
			var classes  = $(this).attr('class');
			var image_id = classes.split('-')[3];
			if ($('img#large-image-'+image_id)){
				$('div.photo-view img').css('display', 'none');
				$('img#large-image-'+image_id).css('display', 'block');
			}
		});
	});
};

$.blockUI.defaults.message = '<img src="http://static.highscoresociety.com/images/animations/image-registering.gif" />';
$.blockUI.defaults.css.width = '350';
$.blockUI.defaults.css.border = '3px solid #00a0ff';
$.blockUI.defaults.overlayCSS.fadeIn = 100;
$.blockUI.defaults.overlayCSS.fadeOut = 100;

function ajaxDeleteAlbum(id, slug) 
{
	$.ajax({
		type: 'GET',
		url: "/photos/"+id+"/"+slug+"/ajax_delete/",	
		success: function(msg){
			$('#album-'+id).hide('fast');
		},
		error: function(msg){
			alert('there was an error deleting the album: '+msg);
		}
	});
}

function ajaxMessageComments(id)
{
	post_data = $('#comment_form').serialize();
	$.post("/messages/ajax_replies/"+id+"/", post_data,
		function(data) {
			$('ol.thread').html(data);
			$('#id_body').val('');
		}
	);

}

(function ($) {
    $.fn.mousehold = function(timeout, f) {
    	if (timeout && typeof timeout == 'function') {
    		f = timeout;
    		timeout = 100;
    	}
    	if (f && typeof f == 'function') {
    		var timer = 0;
    		var fireStep = 0;

    		return this.each(function() {
    			var clearMousehold = function() {
    				clearInterval(timer);
    				if (fireStep == 1) f.call(this, 1);
    				fireStep = 0;
    			};

    			$(this).mousedown(function() {
    				fireStep = 1;
    				var ctr = 0;
    				var t = this;
    				timer = setInterval(function() {
    					ctr++;
    					f.call(t, ctr);
    					fireStep = 2;
    				}, timeout);
    			}).mouseout(clearMousehold).mouseup(clearMousehold);
    		});
    	}
    };    
})(jQuery);

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

