var galleryTagsSearch = "";
var gallerySearchPage = 1;
var curGalleryBoxPage = 1;
var maxGalleryBoxPage = 0;
var galleriesPP = 0;
var galleryBoxWidth = 0;
var galleryBoxFx1;
var galleryBoxFx2;
var topGalleryBoxFx;
var galleryTips;
var curTopFilmsPage = 1;
var maxTopFilmsPage = 0;
var topFilmsPP = 0;
var topFilmsWidth = 302;
var topFilmsFx1;
var topFilmsFx2;
var searchBest=false;
var searchBestF=true;



function nextGalleryBoxFx() {
	galleryBoxFx1.set({'opacity': 0,'width': 0});
	galleryBoxFx2.set({'opacity': 1,'width': galleryBoxWidth});
	galleryBoxFx1.start({'opacity': [0, 1],'width': [0, galleryBoxWidth]});
}
function prevGalleryBoxFx() {
	galleryBoxFx2.set({'opacity': 0,'width': 0});
	galleryBoxFx1.set({'opacity': 1,'width': galleryBoxWidth});
	galleryBoxFx2.start({'opacity': [0, 1],'width': [0, galleryBoxWidth]});
}


function nextGalleryBoxPage(){
	if(curGalleryBoxPage<maxGalleryBoxPage){
		galleryBoxFx2.start({'opacity': [1, 0],'width': [galleryBoxWidth, 0]}).chain(function(){
			var url = '/getGalleryList.php?p=' + (curGalleryBoxPage+1) + '&gpp=' + galleriesPP + '&best='+ searchBest;
			new Ajax(url, {
				method: 'get',
				evalScripts: true,
				update: $('listGalleryBox'),
				onComplete: function() {
					nextGalleryBoxFx();
					//galleryTips = new TipsX3 ($$('.galleryTipper'), {showDelay: 0});
				}
			}).request();
			curGalleryBoxPage++;
		});
	}
}

function prevGalleryBoxPage(){
	if(curGalleryBoxPage>1){
		galleryBoxFx1.start({'opacity': [1, 0],'width': [galleryBoxWidth, 0]}).chain(function(){
			var url = '/getGalleryList.php?p=' + (curGalleryBoxPage-1) + '&gpp=' + galleriesPP + '&best='+ searchBest;
			new Ajax(url, {
				method: 'get',
				evalScripts: true,
				update: $('listGalleryBox'),
				onComplete: function() {
					prevGalleryBoxFx();
					//galleryTips = new TipsX3 ($$('.galleryTipper'), {showDelay: 0});
				}
			}).request();
			if(curGalleryBoxPage>0){
				curGalleryBoxPage--;
			}
		});
	}
}

function updateVipGalleryBox() {
	galleryBoxFx1.start({'opacity': [1, 0]}).chain(function(){
		var url = '/getGalleryList.php?p=1&gpp=' + galleriesPP + '&best='+ searchBest;
		new Ajax(url, {
			method: 'get',
			evalScripts: true,
			update: $('listGalleryBox'),
			onComplete: function() {
				//nextGalleryBoxFx();
				//galleryTips = new TipsX3 ($$('.galleryTipper'), {showDelay: 0});
				galleryBoxFx1.start({'opacity': [0, 1]});
			}
		}).request();
		curGalleryBoxPage=1;
	});
}

function updateTopFilmsBox(best) {
	topGalleryBoxFx.start({'opacity': [1, 0]}).chain(function(){
		var url = '/getTopFilmsList.php?p=1&gpp=9&best='+ best;
		new Ajax(url, {
			method: 'get',
			evalScripts: true,
			update: $('listTopFilms'),
			onComplete: function() {
				//nextGalleryBoxFx();
				//galleryTips = new TipsX3 ($$('.galleryTipper'), {showDelay: 0});
				topGalleryBoxFx.start({'opacity': [0, 1]});
			}
		}).request();
	});
}


function updateGalleryTagLinks() {
	$$('.tagElement a').each(function(item){
		item.addEvent('click',function(e){
			new Event(e).stop();
			var catId = item.getProperty('name');
			var catName = item.getText();
			$('selTagsText').setStyle('display','inline');
			$('removeSearchTag').setStyle('display','inline');
			if($('selectedTagsId').getValue()=="") {
				$('selectedTagsId').setProperty('value',catId);
			} else {
				$('selectedTagsId').setProperty('value',$('selectedTagsId').getValue()+"-"+catId);
			}
			if($('selectedTags').getText()=="") {
				$('selectedTags').setText(catName);
			} else {
				$('selectedTags').setText($('selectedTags').getText()+", "+catName);
			}
			$('curPage').setProperty('value','1');
			gallerySearchPage = $('curPage').getValue();
			updateGallerySearch();
		});
	});	
}
function updateGallerySearchPages() {
	$('galleryOrderType').addEvent('change', function(e) {
		new Event(e).stop();
		$('orderType').setProperty('value',$('galleryOrderType').getValue());
		updateGallerySearch();
	});
	$$('.gallerySearchPage').each(function(item){
		item.addEvent('click',function(e){
			new Event(e).stop();
			$('curPage').setProperty('value',item.getProperty('name'));
			updateGallerySearch();
		});
	});	
}

function updateGallerySearch() {
	if($('mainTagsId').getValue()=="") {
		galleryTagsSearch = $('selectedTagsId').getValue();
	} else {
		if($('selectedTagsId').getValue()=="") {
			galleryTagsSearch = $('mainTagsId').getValue();
		} else {
			galleryTagsSearch = $('mainTagsId').getValue() + '-' + $('selectedTagsId').getValue();
		}
	}
	gallerySearchPage = $('curPage').getValue();
	var orderType = $('orderType').getValue();
	var featured = $('featured').getValue();
		
	var url = 'getGalleryTags.php?t=' + galleryTagsSearch;
	new Ajax(url, {
		method: 'get',
		update: $('tagListSearch'),
		onComplete: function() {
			updateGalleryTagLinks();
		}
	}).request();
	
	var url2 = 'getGalleryResults.php?t=' + galleryTagsSearch+ '&p=' + gallerySearchPage + '&order=' + orderType + '&feat=' + featured;
	new Ajax(url2, {
		method: 'get',
		update: $('gallerySearchResults'),
		onComplete: function() {
			updateGallerySearchPages();
			//galleryTips = new TipsX3 ($$('.galleryTipper'), {showDelay: 0});
		}
	}).request();
}


	
	

window.addEvent('domready', function(){
	//galleryTips = new TipsX3 ($$('.galleryTipper'), {showDelay: 0});
	
	if($defined($('sex_gallery'))){
		var thumbpalTips = new TipsX3 ($$('.thumbpal_tipper'), {showDelay: 0});
	}
	if($defined($('seeNewGalleries'))) {
		$('seeNewGalleries').addEvent('click',function(e){
			new Event(e).stop();
			searchBest = false;
                        $('seeNewGalleries').setStyle('cursor','default');
                        $('seeNewGalleries').setStyle('text-decoration','none');
			$('seeBestGalleries').setStyle('cursor','pointer');
			$('seeBestGalleries').setStyle('text-decoration','underline');
			updateVipGalleryBox();
		});
	}
	if($defined($('seeBestGalleries'))) {
		$('seeBestGalleries').setStyle('text-decoration','underline');
		$('seeBestGalleries').addEvent('click',function(e){
			new Event(e).stop();
			searchBest = true;
                        $('seeBestGalleries').setStyle('cursor','default');
                        $('seeBestGalleries').setStyle('text-decoration','none');
                        $('seeNewGalleries').setStyle('cursor','pointer');
                        $('seeNewGalleries').setStyle('text-decoration','underline');
			updateVipGalleryBox();
		});
	}
	
	if($defined($('seeNewFilms'))) {
		$('seeNewFilms').setStyle('text-decoration','underline');
		$('seeNewFilms').addEvent('click',function(e){
			new Event(e).stop();
                        $('seeNewFilms').setStyle('cursor','default');
                        $('seeNewFilms').setStyle('text-decoration','none');
			$('seeBestFilms').setStyle('cursor','pointer');
			$('seeBestFilms').setStyle('text-decoration','underline');
			updateTopFilmsBox('new');
		});
	}
	if($defined($('seeBestFilms'))) {
		$('seeBestFilms').addEvent('click',function(e){
			new Event(e).stop();
                        $('seeBestFilms').setStyle('cursor','default');
                        $('seeBestFilms').setStyle('text-decoration','none');
                        $('seeNewFilms').setStyle('cursor','pointer');
                        $('seeNewFilms').setStyle('text-decoration','underline');
			updateTopFilmsBox('best');
		});
	}
	
	if($defined($('listGalleryBoxContainer'))) {
		galleryBoxFx1 = new Fx.Styles('listGalleryBoxContainer', {duration: 500, transition: Fx.Transitions.linear});
		
	}
	
	if($defined($('topFilmsContainer'))) {
		topGalleryBoxFx = new Fx.Styles('topFilmsContainer', {duration: 500, transition: Fx.Transitions.linear});
	}
	
	if($defined($('listGalleryBox'))){
		galleryBoxWidth = $('listGalleryBox').getStyle("width").toInt();		
	}
	if($defined($('numGalleries'))){
		maxGalleryBoxPage = $('numGalleries').getValue();
	}
	if($defined($('galleriesPP'))){
		galleriesPP = $('galleriesPP').getValue();
	}
	
	if($defined($('nextGalleryBoxPage')) && $defined($('prevGalleryBoxPage'))){
		$('nextGalleryBoxPage').addEvent('click',function(e){
			new Event(e).stop();
			nextGalleryBoxPage();
		});
		$('prevGalleryBoxPage').addEvent('click',function(e){
			new Event(e).stop();
			prevGalleryBoxPage();
		});
	}

	
	if($defined($('gallerySearchBox'))){
		var s2 = $('selectedTags').getText();
		if(s2==""){
			$('selTagsText').setStyle('display','none');
			$('removeSearchTag').setStyle('display','none');
		}
		
		$('removeSearchTag').addEvent('click', function(e){
			new Event(e).stop();
			var s1 = $('selectedTagsId').getValue();
			var s2 = $('selectedTags').getText();
			var a1 = new Array();
			var a2 = new Array();
			a1 = s1.split("-");
			a2 = s2.split(", ");
			s1 = a1[0];
			s2 = a2[0];
			if (a1.length>1) {
				for(var i=1; i<a1.length-1; i++) {
					s1 += "-"+a1[i];
				}
			} else {
				s1 = "";
			}
			if (a2.length>1) {
				for(var i=1; i<a2.length-1; i++) {
					s2 += ", "+a2[i];
				}
			} else {
				s2 = "";
				$('selTagsText').setStyle('display','none');
				$('removeSearchTag').setStyle('display','none');
			}
			$('selectedTagsId').setProperty('value',s1);
			$('selectedTags').setText(s2);
			updateGallerySearch();
		});
		
		updateGalleryTagLinks();
		updateGallerySearch();
	}
	
	
	
	
});
