$(document).ready(function() {
	// message
	$('div.message').slideDown(400);
	
	// window
	$('body').append('<div id="window_background" onclick="closeWindow();"></div><div id="window"></div>');
	$('#window_background').css('width', $(document).width());
	$('#window_background').css('height', $(document).height()-27);
	
	// slider
	sliderSwitch();
	
	$('#slider div.content > div').hover(function() {
		clearInterval(play);
	}, function() {
		sliderSwitch();
	});	
	
	$('.paging a').click(function() {
		$active = $(this);
		clearInterval(play);
		slider();
		sliderSwitch();
		return false;
	});
	
	// main
	$('#main, #content').corner('10px');
	
	corrections();
});

/* ----- */
// corrections
function corrections() {
	// window
	$('#window').corner('10px');
	$('#window > div').corner('5px');
	
	// toolTip
	$('body').append('<div id="toolTip"></div>');
	
	$('*[title]').each(function() {
		if ($(this).attr('title') != '') {
			$(this).hover(function(e) {
				$('#toolTip').html($(this).attr('title'));
				
				$(this).mousemove(function(e) {
					tipY = ((e.pageY+32+$('#toolTip').height()) > $(document).height() ? (e.pageY-16-$('#toolTip').height()) : (e.pageY+16));
					tipX = ((e.pageX+32+$('#toolTip').width()) > $(document).width() ? (e.pageX-16-$('#toolTip').width()) : (e.pageX+16));
					
					$('#toolTip').css({ top: tipY, left: tipX });
				});
				
				$('#toolTip').stop(true,true).fadeIn('fast');
				
				$(this).removeAttr('title');
			}, function() {
				$('#toolTip').stop(true,true).fadeOut('fast');
				
				$(this).attr('title', $('#toolTip').html());
			});
		}
	});
	
	// button
	$('div.button').html(function() { return '<div>'+$(this).html()+'</div>'; });
	
	// corner
	$('input, select, textarea, fieldset, div.message, div.button, div.button > div, #toolTip').corner('5px');
	$("fieldset > div.title").corner("tl tr 5px");
	
	// corrections
	$('select').css('width', function() { return $(this).width()+36 });
	$('fieldset:last').css('margin-bottom', 0);
	
	$('#reference_details div.gallery, #window img').corner('5px');
}

/* ----- */
// goToUrl
function goToUrl(url, text) {
	if (text != null) {
		c = confirm(text);
		
		if (c == true) {
			location.href = url;
		}
	}
	else {
		location.href = url;
	}
}

/* ----- */
// getUrl
function getUrl(url) {
	current_url = window.location.href.split('//');
	protocol = current_url[0];
	
	current_url = current_url[1].split('/');
	domain = current_url[0];
	
	basepath = protocol == 'https:' ? '/'+current_url[1] : '';
	
	return protocol+'//'+domain+basepath+(typeof(url) != 'undefined' ? url: '');
}

/* ----- */
// slider
function slider() {
	image_reelPosition = ($active.attr('rel') - 1) * ($('#slider div.content').width());
	$('.paging a').removeClass('active');
	$active.addClass('active');
	$('#slider div.content > div').animate({ left: -image_reelPosition }, 500 );
}

function sliderSwitch() {
	play = setInterval(function() {
		$active = $('.paging a.active').next();
		
		if ($active.length === 0) {
			$active = $('.paging a:first');
		}
		
		slider();
	}, 10000);
}

function sliderPrev() {
	if ($('.paging a.active').prev().attr('rel') > 0) {
		$active = $('.paging a.active').prev();
	}
	else {
		$active = $('.paging a:last');
	}
	
	clearInterval(play);
	slider();
	sliderSwitch();
}

function sliderNext() {
	if ($('.paging a.active').next().attr('rel') < 6) {
		$active = $('.paging a.active').next();
	}
	else {
		$active = $('.paging a:first');
	}
	
	clearInterval(play);
	slider();
	sliderSwitch();
}

/* ----- */
// window
function showWindow(html) {
	$('#window').html(function() { return '<div>'+html+'</div>'; });
	
	$('#window').css('left', $(document).width()/2-$('#window').width()/2);
	
	if ($('#window').css('display') == 'none') {
		$('#window_background, #window').fadeIn(250);
	}
	
	corrections();
}

function closeWindow() {
	$('#window_background, #window').fadeOut(250);
}

/* ----- */
// gallery
function showImage(src) {
	showWindow('<img src="'+src+'" alt="" style="height: '+($(document).height()-114)+'px">', 1000);
}
