function getPage(page, type, pagination, cat){
	if ($('loading'))
		$('loading').style.display='block';		  
 	var data = '{"page":"' + page + '", "type":"' + type + '", "pagination":"' + pagination + '", "activeUser":"' + aUserId + '"';
 	if(typeof(rUserId) != 'undefined'){
 		data += ', "userId":"'+rUserId+'"';
 	}
  	if(cat){
 		data += ', "cat" : "' + cat + '"';
 	}
 	if(typeof(ownerId) != 'undefined'){
 		data += ', "owner" : "' + ownerId + '"';
 	}
 	if(typeof(aParent) != 'undefined'){
 		data += ', "parent" : "' + aParent + '"';
 	}
 	if(typeof(aProtected) != 'undefined'){
 		data += ', "protected" : "' + aParent + '"';
 	}
 	if(typeof(aAuthtoken) != 'undefined'){
 		data += ', "authtoken" : "' + aAuthtoken + '"';
 	}
	if(typeof(aColExtra) != 'undefined'){
 		data += ', "col_extra" : "' + aColExtra + '"';
	}
	if(typeof(aKeyword) != 'undefined'){
 		data += ', "keyword" : "' + aKeyword + '"';
	}
	if(typeof(aSearch) != 'undefined'){
 		data += ', "search" : "' + aSearch + '"';
	}	
 	data +='}';
	setCookie('data', data, 7, '/', window.location.hostname);
	jsonp('https://' + window.location.hostname + '/gallery/modules/getimages/ajaxHandler.php?images', 'imgJsonp');
}

function initUserData(data){
	setCookie('data', data, 7, '/', window.location.hostname);
	jsonp('https://' + window.location.hostname + '/gallery/modules/getimages/ajaxHandler.php?login', 'usrJsonp');
}

function processLogin(json, request){
	initUser(json);
	$('usrJsonp').remove();
	deleteCookie('data');
}

function processImages(json, request){
	if ($('loading'))
		$('loading').style.display='none';
	
    if($('page_title')){
		$('page_title').innerHTML = request['type'];
    }
    else if($('360Title') && $('popular_add')){
		$('360Title').innerHTML = request['type'];
		if(request['type'] == 'popular' || request['type'] == 'today\'s popular' || request['type'] == 'week\'s popular' || request['type'] == 'month\'s popular'){
			 $('popular_add').style.display = 'block';
		}
		else{
			$('popular_add').style.display = 'none';
		}    	
    }
    else if ($('360Title') && $('search') && $('search').value){
		$('360Title').innerHTML = $('search').value;
    }
  	$('tdataset').innerHTML = json['dataset'];
  	
  	if(json['pagination']){
 		$('Pagination-OuterHolder').innerHTML = json['pagination_dataset'];
  	}
  	else if(request['pagination'] == ''){
  		//we got to sort out our next/previous so it matches;
  		var prev = parseInt(request['page']) - 1;
		var next = parseInt(request['page']) + 1;
  		if(prev < parseInt($('first').value)){
  			prev = parseInt($('first').value);
  		}
  		if(next > parseInt($('last').value)){
  			next = parseInt($('last').value);
  		}
  		$('prev').stopObserving();
  		$('next').stopObserving();
  		var cat = typeof(request['cat']) != 'undefined' ? request['cat'] : '';
  		Event.observe('prev', 'click', function(event){Event.stop(event); getPage(prev, request['type'], 'pagination', cat);});
  		Event.observe('next', 'click', function(event){Event.stop(event); getPage(next, request['type'], 'pagination', cat);});
  	}
    $('imgJsonp').remove();
    deleteCookie('data');
 }

function processMgrImages(json, request)
{
    if ($('loading'))
    	$('loading').style.display='none';
    display360s(json, request);
    $('imgJsonp').remove();
    deleteCookie('data');
 }

function processMgrCategories(json, request)
{
	updateCategories(json, request);
	$('catJsonp').remove();
	deleteCookie('data');
}

function getCollection(page, type, pagination){
	if($('cloading')){
    	$('cloading').style.display='block';		
	}	
	else if($('loading')){
    	$('loading').style.display='block';		
	}
 	var data = '{"page":"' + page + '", "type":"' + type + '", "cpagination":"' + pagination + '", "activeUser":"' + aUserId + '"';
 	if(typeof(rUserId) != 'undefined'){
 		data += ', "userId":"'+rUserId+'"';
 	}
 	if(typeof(ownerId) != 'undefined'){
 		data += ', "owner" : "' + ownerId + '"';
 	}
 	data +='}';
	setCookie('cdata', data, 7);
	jsonp('https://' + window.location.hostname + '/gallery/modules/getimages/ajaxHandler.php?collections', 'colJsonp');
}

function processCollections(json, request){
	if($('loading')){
    	$('loading').style.display='none';		
	}
	if($('cloading')){
    	$('cloading').style.display='none';		
	}	
    if(request['userId']){
		$('page_title').innerHTML = request['type'];
    }
    else{
    	if($('collectionTitle')){
    		$('collectionTitle').innerHTML = request['type'];
    	}
    	else{
			$('360Title').innerHTML = request['type'];    		
    	}
    }
  	$('cdataset').innerHTML = json['dataset'];
  	if(json['pagination']){
 		$('Pagination-collection-OuterHolder').innerHTML = json['pagination_dataset'];
  	}
  	else if(request['cpagination'] == ''){
  		//we got to sort out our next/previous so it matches;
  		var prev = parseInt(request['page']) - 1;
		var next = parseInt(request['page']) + 1;
  		if(prev < parseInt($('cfirst').value)){
  			prev = parseInt($('cfirst').value);
  		}
  		if(next > parseInt($('clast').value)){
  			next = parseInt($('clast').value);
  		}
  		$('cprev').stopObserving();
  		$('cnext').stopObserving();
  		Event.observe('cprev', 'click', function(event){Event.stop(event); getCollection(prev, request['type'], 'cpagination');});
  		Event.observe('cnext', 'click', function(event){Event.stop(event); getCollection(next, request['type'], 'cpagination');});

  	}
    $('colJsonp').remove();
    deleteCookie('cdata');
}

function getCategories(){
	
}

function getWidgets(){
	
}

function getStats(page, type, pagination, sort){
	$('loading').style.display='block';		  
 	var data = '{"page":"' + page + '", "type":"' + type + '", "pppagination":"' + pagination + '", "sort":"' + sort + '"}';
	setCookie('data', data, 7);
	jsonp('https://' + window.location.hostname + '/gallery/modules/getimages/ajaxHandler.php?ppstats', 'ppStatsJsonp');
}

function processPPStats(json, request){
	if($('loading')){
    	$('loading').style.display='none';		
	}
  	$('tdataset').innerHTML = json['dataset'];
  	$('page_title').innerHTML = request['type'];
  	if(json['pagination']){
 		$('Pagination-OuterHolder').innerHTML = json['pagination_dataset'];
  	}
  	else if(request['pagination'] == ''){
  		//we got to sort out our next/previous so it matches;
  		var prev = parseInt(request['page']) - 1;
		var next = parseInt(request['page']) + 1;
  		if(prev < parseInt($('first').value)){
  			prev = parseInt($('first').value);
  		}
  		if(next > parseInt($('last').value)){
  			next = parseInt($('last').value);
  		}
  		$('prev').stopObserving();
  		$('next').stopObserving();
  		Event.observe('prev', 'click', function(event){Event.stop(event); getCollection(prev, request['type'], 'pagination', request['sort']);});
  		Event.observe('next', 'click', function(event){Event.stop(event); getCollection(next, request['type'], 'pagination', request['sort']);});

  	}
    $('ppStatsJsonp').remove();
    deleteCookie('cdata');
}

function updateCollectionStatus(){
	$('loading').style.display='block';
 	var data = '{"nstatus":"' + $('col_status').value + '", "colId":"' + aParent + '"}';
	setCookie('data', data, 70);
	jsonp('https://' + window.location.hostname + '/gallery/modules/getimages/ajaxHandler.php?updateCol', 'updColJsonp');
}

function processUpdateCollectionStatus(result, request){
	$('loading').style.display='none';	
	if(result == -99){
		$('colError').innerHTML = '<span class="negative-thin">It seems there was a problem processing your request. To protect your security, please <a href="main.php?g2_controller=core.Logout&g2_return=%2Fgallery%2Fmain.php%3Fg2_controller%3Duseralbum.UserAlbum%26g2_authToken%3D926c058c5bd1%26g2_GALLERYSID%3DTMP_SESSION_ID_DI_NOISSES_PMT%26g2_itemId%3D7&g2_returnName=album">relogin</a>, and try again. Only "Pro" users can edit collection\'s status.</span>';
	}
	else{
		$(result).selected = 'selected';
		$('curStatus').innerHTML = result;
	}
    $('updColJsonp').remove();
    deleteCookie('data');
}
