var docRoot = '';

/* SLIDER SETTINGS */
$(function() {
	
	$('.slider').mobilyslider({
		content: '.sliderContent',
		children: 'div',
		transition: 'fade',
		animationSpeed: 500,
		autoplay: true,
		autoplaySpeed: 5500,
		pauseOnHover: true,
		bullets: false,
		arrows: true,
		animationStart: function(){},
		animationComplete: function(){}
	});
	
});

/* SCORE SETTINGS */
$(function() {
	$('.score-bg').animate({width: 'toggle'},{duration: 2000});
});

/* BACK TO TOP */
$(function() {
	$('#back-top a').click(function(){
		$('html, body').animate({scrollTop: '0'}, 1000);
		return false;
	});
});

/* FANCYBOX */
$(document).ready(function() {
	$("a.grouped_elements").fancybox({
			'transitionIn'		: 'fade',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
});

$(document).ready(function() {	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div').width($('#mask').width());
	
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
	
		//Get the height of the sub-panel
		var panelheight = $($(this).attr('href')).height();
		
		//Set class for the selected item
		$('a[rel=panel]').removeClass('selected');
		$(this).addClass('selected');
		
		//Resize the height
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		
		//Discard the link default behavior
		return false;
	});
});

function login_init() {
	$('button#login_signup').button({ icons: {primary:'icon_key'}}).bind('click', function() {
		login_signup();
		return false;
	});
}

function login_signup() {
	hs.registerOverlay({
		html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
		position: 'top right',
		useOnHtml: true,
		fade: 2 // fading the semi-transparent overlay looks bad in IE
	});
	
	hs.overrides.push('onAfterExpand');
	hs.htmlExpand(null, {
		objectType: 'ajax',
		src: docRoot+'ajax/?do=login_signup',
		width: 700,
		height: 500,
		wrapperClassName: 'no-footer no-move custom-header',
		dimmingOpacity: 0.30,
		preserveContent: false,
		onAfterExpand: function(expander) {
			$('input.make_button').button();
			login_form_init();
		}
	});
}

function login_form_init() {
	$('input#login_go').bind('click', function() {
		
		var data = $('form#login').serialize();
		
		$.ajax({
			url: docRoot + 'ajax/?do=login',
			type: 'post',
			dataType: 'json',
			data: data,
			success: function(data) {
				if(data.response == 'ok') {
					// Done, reload
					window.location.reload();
				}
				else{
					alert("Error: "+data.message);
				}
			}
		});
		return false;
		
	});
	$('input#signup_go').bind('click', function() {
		
		var data = $('form#signup').serialize();
		
		$.ajax({
			url: docRoot + 'ajax/?do=signup',
			type: 'post',
			dataType: 'json',
			data: data,
			success: function(data) {
				if(data.response == 'ok') {
					// Done, reload
					window.location.reload();
				}
				else{
					alert("Error: "+data.message);
				}
			}
		});
		return false;
		
	});
}

function save_comment() {
	var data = $('form#comment-form').serialize();
	
	$('div#loading_mask')
		.width($('form#comment-form').width())
		.height($('form#comment-form').height())
		.show();
	
	$.ajax({
		url: docRoot + 'ajax/?do=post_comment',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.response == 'ok') {
				// Done, reload
				$('div#comment_report').html('<div class="noticebox"><p>Thanks! Comment saved.</p></div>');
				$('textarea#comment').val('');
				load_comments(1);
				$('div#loading_mask').fadeOut();
			}
			else{
				//alert("Error: "+data.message);
				$('div#comment_report').html('<div class="errorbox"><p><b>Error:</b> ' + data.message + '</p></div>');
				$('div#loading_mask').fadeOut();
			}
		}
	});
	return false;
}

function load_comments(page) {
	$('div#comments_mask')
		.width($('div#load_comments').width())
		.height($('div#load_comments').height())
		.show();
	
	var pid = $('input#pid').val();

	$.ajax({
		url: docRoot + 'ajax/?do=load_comments&page='+escape(page)+'&pid='+escape(pid),
		success: function(data) {
			$('div#load_comments').html(data);
			$('div#comments_mask').fadeOut();
			
			// Scroll to top of comments
			var div = $("div#load_comments");
			var offset = div.offset();
			$('html, body').animate({scrollTop: (offset.top - 200)}, 500);
		}
	});
	return false;
}

function comments_area_init() {
	// Bind pagination
	$('div#pagination a').live('click', function() {
		var thisid = $(this).attr('id').split('_');
		var page = thisid[1];
		load_comments(page);
		return false;
	});
	
	// Bind login link if it exists
	if($('a#login_from_comments').length > 0) {
		$('a#login_from_comments').button().bind('click', function() {
			login_signup();
			return false;
		});
	}
}
