// JavaScript Document
function toggleDiv(objectID) {
	if (document.getElementById(objectID).style.display == 'block') {
		document.getElementById(objectID).style.display = 'none';
	} else {
		document.getElementById(objectID).style.display = 'block';
	}
	return
}

var tot_photos;

//image rotator
function rotateImages(photos){
	var num = 0;
	tot_photos = photos.length;
	$('<img/>').attr('src', 'scale_image.php?f=_gallery/'+photos[num]+'&h=390&w=520').load(function() {
		$('.rotatorimage').css({
			'background':'url(scale_image.php?f=_gallery/'+photos[num]+'&h=390&w=520) no-repeat center'
		}).hide().fadeIn(500);
		if(tot_photos > 1) {
			$('#front_gallery').append('<div class="rotatorimage"></div>');
			$('.rotatorimage:last').css({
				'background':'url(scale_image.php?f=_gallery/'+photos[num+1]+'&h=390&w=520) no-repeat center',
				'display':'none'
			});
			setTimeout("rotator("+num+")",6000);
		}
	});
}

function rotator(num) {
	num += 1;
	if(num >= tot_photos) num = 0;
	$('#front_gallery .rotatorimage:first').show().fadeOut(500,function(){$(this).remove()});
	$('#front_gallery .rotatorimage:last').hide().fadeIn(500);
	$('#front_gallery').append('<div class="rotatorimage"></div>');
	if(num+1 >= tot_photos) var next = 0;
	else var next = num+1;
	$('.rotatorimage:last').css({
		'background':'url(scale_image.php?f=_gallery/'+encodeURIComponent(photos[next])+'&h=390&w=520) no-repeat center',
		'display':'none'
	});
	setTimeout("rotator("+num+")",6000);
}

function updateGallery(id,start_pos,events,prog) {
	if (ajax) {	
		var q = '_ajax/gallery.php?id=' + encodeURIComponent(id) + '&start_pos=' + encodeURIComponent(start_pos) + '&event=' + encodeURIComponent(events) + '&prog=' + encodeURIComponent(prog);
		ajax.open('get',q,true);
		ajax.onreadystatechange = handle_updateGallery;
		ajax.send(null);
	} 
}
function handle_updateGallery() {
	if ((ajax.readyState == 4) && (ajax.status == 200)) {
		document.getElementById('gallery').innerHTML = ajax.responseText;
	}
}

function updateThumbs(start_pos,page_limit,events,prog,type) {
	if(type == 'gal') var t = 'gal';
	else var t = '';
	if (ajax) {	
		var q = '_ajax/thumbnails.php?start_pos=' + encodeURIComponent(start_pos) + '&page_limit=' + encodeURIComponent(page_limit) + '&t=' + encodeURIComponent(t) + '&prog=' + encodeURIComponent(prog) + '&event=' + encodeURIComponent(events);
		ajax.open('get',q,true);
		if(t == 'gal') {
			ajax.onreadystatechange = handle_updateGallery;
		} else {
			ajax.onreadystatechange = handle_updateThumbnails;
		}
		ajax.send(null);
	} 
}
function handle_updateThumbnails() {
	if ((ajax.readyState == 4) && (ajax.status == 200)) {
		document.getElementById('thumbnails').innerHTML = ajax.responseText;
	}
}

function updateImage(id) {
	$('#gallery_image').removeAttr('style');
	var q = '_ajax/image.php?id='+encodeURIComponent(id);
	ajax.open('get',q,true);
	ajax.onreadystatechange = handle_updateImage;
	ajax.send(null);
}

function handle_updateImage() {
	if ((ajax.readyState == 4) && (ajax.status == 200)) {
		document.getElementById('gallery_holder').innerHTML = ajax.responseText;
	}
}


$(document).ready(function(){
						   
	//universal ajax function
	var ajaxFunction = function(dataString,file,replaceID,f) {
		$.ajax({  
		  type: "POST",  
		  url: file,  
		  data: dataString,  
		  success: function(msg) {  
			$('#'+replaceID).html(msg);
			if (typeof f == "function") f(msg,replaceID);
		  }  
		});
	}	
	//center div function
	jQuery.fn.center = function (position,zIndex) {
		this.css("position",position);
		this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
		this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
		this.css("zIndex",zIndex);
		return this;
	}
	
	//login function
	$('.login-btn').click(function(){
		var h = $(this).attr('href');
		if($('#login-form').length == 0) {
			$('body').prepend('<div id="login-form" style="display:none"></div>');
		}
		var dataString = 'h='+encodeURIComponent(h);
		ajaxFunction(dataString,'_ajax/login.php','login-form',callback);
		return false;
	});
	
	
	var callback = function(msg,divID){
		$('#'+divID).center('absolute',100).show();
		
		$('.user-login').click(function(){
			var e = $('input[name="email"]').val(),
				p = $('input[name="pass"]').val(),
				h = $('input[name="h"]').val();
			var dataString = 'e='+encodeURIComponent(e)+'&p='+encodeURIComponent(p)+'&h='+encodeURIComponent(h);
			ajaxFunction(dataString,'_ajax/login.php','login-form',callback);
			return false;
		});
		
		$('.closeLogin').click(function(){
			$('#login-form').remove();
		});
	}
	
	// gallery functions
	/*$('.eventchange').change(function(){
		var dataString = 'e='+encodeURIComponent($(this).val());
		ajaxFunction(dataString,'_ajax/thumbnails.php','big-thumbnails',clickStart);
	});
	
	$('.gallery-image').click(function(){
		var id = $(this).attr('rel');
		var dataString = 'id='+encodeURIComponent(id);
		ajaxFunction(dataString,'_ajax/gallery.php','content',galleryCallback);
	});
	
	var galleryCallback = function() {
		$('.eventchange').change(function(){
			var dataString = 'e='+encodeURIComponent($(this).val())+'&sm=true';
			ajaxFunction(dataString,'_ajax/thumbnails.php','thumbnails',clickStart);
		});
		clickStart();
	}
	
	var clickStart = function() {
		$('.gallery-image').click(function(){
			var id = $(this).attr('rel');
			var dataString = 'id='+encodeURIComponent(id);
			ajaxFunction(dataString,'_ajax/gallery.php','content',galleryCallback);
		});
		
		$('.gallery-image-sm').click(function(){
			var bg = $(this).attr('style');
			bg = bg.split('url(')[1].split('&')[0];
			$('#main-image').css('background','url('+bg+'&w=700&h=450) no-repeat center');
		});
	}*/
	
});
