//$().ajaxStop($('#tagcloud').unblock()); 

//var server = 'http://localhost/sumopix2/docs/';
var server = 'http://www.sumopix.com/';

function updateTagcloud(params) { 
	$.ajax({ 
		url: server + 'scripts/gallery/tagcloud.php',
		data: params, 
		cache: false, 
		success: function(html){
			$("#tagcloud").empty();
	    	$("#tagcloud").append(html);
	    	$('#tagcloud').unblock();
	    	resizePage();
	  	} 
	}); 
} 

function updateGallery(params) { 
	$.ajax({ 
		url: server + 'scripts/gallery/main.php',
		data: params, 
		cache: false, 
		success: function(html){
			$("#main_gallery").empty();
	    	$("#main_gallery").append(html);
	    	$('#main_gallery').unblock();
	    	//alert('cal height');
	    	//calcHeight();
	    	//alert('start main');
	    	mainmenu();
	  	} 
	}); 
} 

function updateImages(params) { 
	$.ajax({ 
		url: server + 'scripts/gallery/images.php',
		data: params, 
		cache: false, 
		success: function(html){
			// For some reason Jquery empties to much...
			document.getElementById("image_content").innerHTML = '';
			
	    	$("#image_content").append(html);
	    	$('#image_content').unblock();
	    	calcHeight();
	  	} 
	}); 
} 

function updateSearch(params){
	$.ajax({ 
		url: server + 'scripts/gallery/search.php',
		data: params, 
		cache: false, 
		success: function(html){
			// For some reason Jquery empties to much...
			document.getElementById("search_bar").innerHTML = '';
			
	    	$("#search_bar").append(html);
	    	$('#search_bar').unblock();
	    	calcHeight();
	  	} 
	}); 
}

function blockImages(msg){
	
	$('#image_content').block({ message: '<span style=\"background-color:#000000;\"><h1><img src="../images/icons/loading.gif" /> ' + msg + '</h1></span>', css: { border: 'none', margin: '0px', width: '600px' }  }); 
}

function loadImage(id){
	
	updateImages("imageid=" + id);
	blockImages("Loading image...");
	return false;
}

function updateSearchBar(results, q){
	updateSearch("nb_results=" + results + "&gq=" + q);
}

function updateGallerySearch(params, hash){
	blockImages("Please wait...");
	setNewHash(hash);
	updateImages(params);
}

function doGallerySearch(){
	var string = document.getElementById('q').value;
	blockImages("Searching...");
	document.getElementById('current_search').innerHTML = '<tr><td class="bold">Searching...</td></tr>';
	setNewHash("search," + string);
	updateImages("gq=" + string);
	updateGallery('');
	return false;
}

function searchByString(string, page){
	blockImages("Searching...");
	setNewHash("search," + string + "," + page);
	updateImages("gq=" + string + "&showr=" + page);
	updateGallery('');
	return false;
}

function searchByCategory(cat, page){
	blockImages("Searching...");
	updateImages("gcat=" + cat + "&showr=" + page);
	setNewHash("cat_search," + cat + "," + page);
	updateGallery('');
	return false;
}

function searchByCountry(countryid, page){
	blockImages("Searching...");
	updateImages("cid=" + countryid + "&showr=" + page);
	setNewHash("c_search," + countryid + "," + page);
	updateGallery('');
	return false;
}

function searchByCreator(creator, page){
	blockImages("Searching...");
	updateImages("creator=" + creator + "&showr=" + page);
	setNewHash("cr_search," + creator + "," + page);
	updateGallery('');
	return false;
}

function doGallerySearchByTag(tag, page){
	blockImages("Searching...");
	updateImages("tag=" + tag + "&showr=" + page);
	setNewHash("tag_search," + tag + "," + page);
	updateGallery('');
	return false;
}

function updateMenu(path, update_view, page){
	updateGallery("gcat=" + path + "&update_view=" + update_view + "&showr=" + page);
}

function updatePath(path){
	setNewHash(path);
}

var page_path = parent.location;
var can_load = true;

function refreshMain(params, path){
	$('#main_gallery').block({ message: '<span style=\"background-color:#000000;\"><h1><img src="../images/icons/loading.gif" /> Loading categories </h1></span>', css: { border: 'none', margin: '0px', width: '600px' }  }); 
	
	setNewHash(path);
	// location.replace("http://localhost/sumopix2/docs/Gallery/?path=" + path)
	updateGallery(params);
}

function setNewHash(path){
	can_load = false;
	parent.location.hash = path;
	last_hash = parent.location.hash;
	can_load = true;
}

var last_hash =  parent.location.hash;
function checkChange(){
	if( parent.location.hash != last_hash && can_load){
		loadNewHash();
	}
	last_hash =  parent.location.hash;
}

setInterval("checkChange()",500);

var exp = new Array();
function loadNewHash(){
	// Get anchor vars:
    var hash = parent.location.hash;
    
    // Select action
    if(hash != null && hash != ""){
    	exp = hash.split(',');
    	var page = 0;
    	if(exp[2] != null && exp[2] > 0){
    		page = exp[2];
    	}
    	
    	if(exp[0] == "#image"){
    		loadImage(exp[1]);
    	}else if(exp[0] == "#cat"){
    		updateMenu(exp[1],"false", page);
    		searchByCategory(exp[1], page);
    	}else if(exp[0] == "#search"){
    		searchByString(exp[1], page);
    	}else if(exp[0] == "#cat_search"){
    		searchByCategory(exp[1], page);
    	}else if(exp[0] == "#c_search"){
    		searchByCountry(exp[1], page);
    	}else if(exp[0] == "#cr_search"){
    		searchByCreator(exp[1], page);
    	}else if(exp[0] == "#tag_search"){
    		doGallerySearchByTag(exp[1], page);
    	}else{
    		updateGallery('');
    	}
    }else{
    	$('#main_gallery').block({ message: '<h1><img src="../images/icons/loading.gif" /> Loading categories </h1>', css: { border: 'none', margin: '0px', width: '600px' }  }); 
		blockImages("Loading...");
		updateGallery('');
		updateImages('');
	}
}

var times = 0;
var padding_top = 50;
function calcHeight()	{
	if(times < 10){
		times++;
		var frame = top.document.getElementById('image_content');

		if(frame != null){
			//var offset = frame.offsetParent.offsetHeight;
			var divHeight;
			if(frame.offsetHeight){
				divHeight = frame.offsetHeight;
			}else if(frame.style.pixelHeight){
				divHeight = frame.style.pixelHeight;
			}
			// calculate maximum height
			var height = Math.max(500, (divHeight-padding_top));
			
	  		var the_height = height + "px";
			//frame.style.height = the_height;
		}
		resizePage();
	}
	return;
}


$(document).ready(function() { 
	loadNewHash();
    
	$('#tagcloud').block({ message: '<h1><img src="../images/icons/loading.gif" /> Loading tags.. </h1>', css: { border: '1px solid #E74F37', margin: '2px', width: '296px' }  }); 
	updateTagcloud('');
	//calcHeight();
	
}); 