$(document).ready(function() {

	// place maitani hero
	$(window).bind('resize', function() {
		var m = $('#maitani-hero');
		var h = $(document).height();
		var offset = (h - ($(m).height()));
		$(m).css('margin-top', offset + 'px').show();
	});
	$(window).resize();
	
	// do the ajax rating thing
	var initRating = function() {
		$('.rate').each(function() {
			var container = $(this);
			// get link id 
			$('.rateform').css('display', 'none');
			var link_id = $(container).parent().parent().parent().attr('rel');
			var url = $(this).parent().next().find('form').attr('action');
			$('a.ajaxrate', this).each(function() {
				var rating_value = $(this).attr('rel');
				$(this).unbind('click');
				$(this).click(function() {
					var link = $(this);
					$.post(url, { id: link_id, rating_value: rating_value, format: 'json' }, function(data) {
						if(data.code == 1) {
							// success
							$(container).next('.ratemessage:first').empty().removeClass('rateerror').append(data.message);
							// insert updated rate bar
							$(container).empty().append(data.updated_html);
							// re-init rating
							initRating();
						} else {
							// error
							$(container).next('.ratemessage:first').addClass('rateerror').empty().append(data.message);
						}
					}, 'json');
					return false;
				});
			});
		});
		return;
	};
	initRating();

	// provide captcha reload via ajax
	$('.captcha_reload_link').click(function() {
		var url = '/captcha/index?format=json';
		var link = $(this);
		$.getJSON(url, function(data) {
        	var html = data.html;
        	var id = data.id;
        	// set id to hidden field
        	$(link).parent().find('#secure-id').val(id);
        	// replace image
        	var img = $(link).parent().find('img');
        	$(img).next('br').remove();
        	$(img).replaceWith(html);
		});
		return false;
	});
	
	// handle "how it works"
	var origSliderOffset = $('.hdit-slider').css('margin-top');
	$('.hdit-slider').click(function() {
		var s = $(this);
		if($(s).is('.hidden')) {
			// show	
			$(s).css('z-index', '3').animate({"marginTop": "0px"}, "slow").removeClass('hidden');
		} else {
			// hide
			$(s).css('z-index', '1').animate({"marginTop": "" + origSliderOffset + ""}, "slow").addClass('hidden');
		}
	});
	
	// item carousel
	function initItemCarousel(carousel) {
	    jQuery('#item-carousel-next').bind('click', function() {
	        carousel.next();
	        return false;
	    });
	    jQuery('#item-carousel-prev').bind('click', function() {
	        carousel.prev();
	        return false;
	    });
	};
	jQuery('.item-carousel').jcarousel({
		scroll: 4,
		initCallback: initItemCarousel,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		auto: 4
	});
	
	// facebook carousel
	function initFacebookCarousel(carousel) {
	    jQuery('#facebook-carousel-next').bind('click', function() {
	        carousel.next();
	        return false;
	    });
	    jQuery('#facebook-carousel-prev').bind('click', function() {
	        carousel.prev();
	        return false;
	    });
	};
	jQuery('#facebook-carousel').jcarousel({
		scroll: 1,
		initCallback: initFacebookCarousel,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	// enable scrolling
	var loading = false;
	var loadContent = function(box, url, insert_style) {
		insert_style = insert_style || 'append';
		$(box).parent().prepend('<div class="scrollbox-loader"></div>');
		var content = $.get(url, {format: 'html'}, function(response) {
			if(insert_style == 'replace') {
				$(box).empty().append(response);
			} else {
				$(box).append(response);
			}
			$(box).parent().find('.scrollbox-loader').remove();
			loading = false;
			// re-init the rating
			initRating();
			// update number
			var num = $('.piclist', box).length;
			$(box).parent().find('.items-current').text(num);
			// fetch number of total items available to display in status bar
			var count_url = url + '/count/true';
			$.get(count_url, {format: 'html'}, function(response) {
				var number_total = response;
				$(box).parent().find('.items-total').text(number_total);
			}, 'html');
		}, 'html');
		return;
	}
	$('.scrollbox.ajaxscroll').each(function() {
		$(this).scroll(function() {
			var box = $(this);
			var scrollTop = $(box).scrollTop() + $(box).height();
			var height = box[0].scrollHeight;
	        if (scrollTop >= height && !loading) {
	        	loading = true;
	        	var base_url = $(box).attr('rel');
	        	var language_id = $(box).parent().find('select.language_selector').val();
		   		var sort_by = $(box).parent().find('select.scroll_content_sort_by').val();
				var url = base_url + '/offset/' + $('.piclist', box).length + '/language_id/' + language_id + '/sort_by/' + sort_by;
				loadContent(box, url);
	        }
		});
		// handle the language selection
		var b = $(this);
		$(this).parent().find('select.language_selector').bind('change', function() {
			// clear box and load initial data
		    var base_url = $(b).attr('rel');
		    var language_id = $(this).val();
		    var sort_by = $(b).parent().find('select.scroll_content_sort_by').val();
			var url = base_url + '/language_id/' + language_id + '/sort_by/' + sort_by;
			loadContent(b, url, 'replace');
		});
		$(this).parent().find('select.scroll_content_sort_by').bind('change', function() {
			// clear box and load initial data
		    var base_url = $(b).attr('rel');
		    var sort_by = $(this).val();
		    var language_id = $(b).parent().find('select.language_selector').val();
			var url = base_url + '/language_id/' + language_id + '/sort_by/' + sort_by;
			loadContent(b, url, 'replace');
		});
		// fill the current item count
		var num = $('.piclist', this).length;
		$(this).parent().find('.items-current').text(num);
	});

	// ie6 needs a mouseover to display images ... of course!
	$('.carousel-wrapper a').each(function() {
		$(this).mouseover().addClass('tooltip');
	});
	
	// handle tooltips
	$('.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		showBody: " | ",
		extraClass: "pretty fancy",
		top: -15,
		left: 5
	});
	
});


/* Indextools Helper Functions */

// handle actions
var trackAction = function(action_code, link, trackOutgoing) {
	trackOutgoing = trackOutgoing || false;
	link = link || false;
	// call indextools func
	_s_action(action_code);
	return true;
}
