/******************************************************************************************************************/
// ITEMS HANDLE
/******************************************************************************************************************/

var CoverTimer;
var openb = false;

function handleItems(){
	// click on matches
	$('.matches a').click(function(){
	
		if (!$(this).hasClass('noballoon')){
		
			$('.balloon').remove();
			$('body').prepend('<div class="balloon"><div class="close">x</div><div class="block"><div class="title"><h3>Connections (<span class="num">0</span>)</h3></div><div class="cont"><div class="loading">loading...</div></div></div></div>');
			
			openb = true;
			clearTimeout(CoverTimer);
			
			mypos = $(this).offset();
			
			if ($(this).parents('.item').attr('id') == 'current') {
				bt = mypos.top - 90;
				bl = mypos.left - 12;
			}
			else {
				bt = mypos.top - 72;
				bl = mypos.left - 8;
			}
			
			$('.balloon').css({ top: bt, left: bl });
			$(window).scrollTop(bt - 20);
			
			// ajax loading
			myitem = $(this).parents('.item').find('.myid').html();
			mylink = '/wp-content/themes/filtersage/ajax/ajax-items.php?id=' + myitem;	
			
			jQuery.ajax({
				url: mylink,
				success: function(html){
					$('.balloon .cont').html(html);
					num = $('.balloon .item').size();
					$('.balloon .num').html(num);
				}
			});
			
			// close
			$('.balloon .close').click(function(){ 
				openb = false;
				$('.balloon').remove();
				clearTimeout(CoverTimer);
				//window.setTimeout('loopCovers()', 5000);
			});
			
			return false;
		}
	});
	
	// filter attachs (on iframe)
	$('#attach .frm-generic').hide();
	$('#attach .frm-generic:first').show();
	
	$('#attach .buttons a').click(function(){
		$('#attach .buttons a').removeClass('sel');
		$(this).addClass('sel');
		myid = $(this).attr('href');
		$('#attach .frm-generic').hide();
		$(myid).show();
		
		return false;
	});
}

/******************************************************************************************************************/
// HANDLE DROPDOWN FILTERS FUNCTIONS
/******************************************************************************************************************/

function refreshLatest(){
	mylink = $('#filter-additions:selectd').attr('value');

	if (mylink != undefined){// definito
		$('#latest .l-ajax').html('<div class="loading">loading...</div>').show();
		jQuery.ajax({
			url: mylink,
			success: function(html){			
				$('#latest .l-ajax').html(html);
			}
		});
	}
	
}

function handleDropdownFilters(){
	// latest additions (sidebar)
	refreshLatest();
	$('#filter-additions').change(function(){ refreshLatest(); });
	
	// archive
	myarchive = $('#filter-browse option:selected').attr('value');
	mygenre = $('#filter-genres option:selected').attr('value');
	
	$('#filter-browse').change(function(){
		myarchive = $('#filter-browse option:selected').attr('value');
		window.location = '?archive=' + myarchive;
	});
	
	$('#filter-genres').change(function(){
		mygenre = $('#filter-genres option:selected').attr('value');
		window.location = '?archive=' + myarchive + '&genre=' + mygenre;
	});
}

/******************************************************************************************************************/
// TABS HANDLE
/******************************************************************************************************************/

function handleTabs(){
	$('.tabs .tab').hide();
	
	$('.tabs .sel').each(function(){
		initid = $(this).find('A').attr('href');
		$(initid).show();
	});
	
	$('.tabs .labels A').click(function(){
		$(this).parents('.labels').find('li').removeClass('sel');
		$(this).parent().addClass('sel');
			
		if ($(this).hasClass('ajax')) {
			// load ajax tab
			mylink = $(this).attr('href');
			myblock = $(this).parents('.tabs');
			
			myblock.find('.tab').hide();
			myblock.find('.t-ajax').html('<div class="loading">loading...</div>').show();
			
			jQuery.ajax({
				url: mylink,
				success: function(html){			
					myblock.find('.t-ajax').html(html);	
					
					handleItems();
					
					adjustItemsHeight();
				}
			});
		}
		else {
			// show static tab
			myid = $(this).attr('href');
			
			$(this).parents('.tabs').find('.tab').hide();
			$(myid).show();
		}
		
		return false;
	});
}

/******************************************************************************************************************/
// SEARCH HANDLE
/******************************************************************************************************************/

function handleSearch(){
	$('#container').prepend('<div id="ajax-results"><!-- ajax suggestions here --></div>');
	$('#frm-search .frm-text').attr('autocomplete','off');
	
	// on submit
	//$('#frm-search').submit(function(){ return false; });
	
	// search box
	$('#frm-search .frm-text').focus(function(){ 
		$('#frm-search').css({ backgroundPosition: '0px -22px' });
		$(this).css({ color: '#000000' });
		this.value = "";
	});
	
	$('#frm-search .frm-text').blur(function(){ 
		$('#frm-search').css({ backgroundPosition: '0px 0px' });
		$(this).css({ color: '#7f7f7f' });
		if (this.value == "") {
			this.value = "Search";
			$('#ajax-results').hide();
		}
	});
	
	$('#ajax-results').blur(function(){
		$(this).hide();
	});
	
	// on key-up event
	
	/*
	var mysearch = '';
		
	$('#frm-search .frm-text').keyup(function() {
				
		mysearch = this.value;		

		if (mysearch != '') {
		
			if (this.value != this.lastValue) {				
				
				if(this.timer) clearTimeout(this.timer);
				
				this.timer = setTimeout(function() {
					$('#ajax-results').html('<div class="loading">loading...</div>').show();
					
					mylink = '/wp-content/themes/filtersage/ajax/ajax-items-search.php?key='+ mysearch;	
				
					jQuery.ajax({
						url: mylink,
						success: function(html){
							$('#ajax-results .loading').remove();
							
							$('#ajax-results').html(html);
							
							// suggested item click						
							$('#ajax-results .items-list a').click(function(){	
								
								$('#ajax-results').html('<div class="loading">loading...</div>');
								
								mylink = $(this).attr('href');							
									
								jQuery.ajax({
									url: mylink,
									success: function(html){
										// redirect to the created page
										window.location =  html;
									}
								});				
								
								return false;
							});
							
							// fancy boxes
							$('.fancybox').fancybox({
								'overlayShow': true,
								'frameWidth': 450,
								'frameHeight': 400
							});
							
							$('.btn-new').fancybox({
								'overlayShow': true, 
								'hideOnOverlayClick': 0, 
								'frameWidth': 960, 
								'frameHeight': 450,
								'callbackOnClose': function(){
									// redirect to the created page ??
								}
							});
						}
					});			
				}, 1000);
			
				this.lastValue = this.value;
			}
		}
		else {
			$('#ajax-results').hide();
		}
	});
	*/
}

/******************************************************************************************************************/
// GENERIC FORMS HANDLE
/******************************************************************************************************************/

function handleForms(){
	
	$('.frm-generic .frm-text').focus(function(){ 
		$(this).css({ backgroundPosition: '0px -30px', color: '#000000' });
	});
	
	$('.frm-generic .frm-text').blur(function(){ 
		$(this).css({ backgroundPosition: '0px 0px', color: '#7f7f7f' });
	});
	
	$('.frm-generic .frm-textarea').focus(function(){ 
		$(this).css({ backgroundPosition: '0px -180px', color: '#000000' });
	});
	
	$('.frm-generic .frm-textarea').blur(function(){ 
		$(this).css({ backgroundPosition: '0px 0px', color: '#7f7f7f' });
	});
	
	$('.frm-generic .frm-submit').hover(function(){ 
		$(this).css({ backgroundPosition: '0px -40px', color: '#ffffff' });
	}, function(){
		$(this).css({ backgroundPosition: '0px 0px', color: '#e3316f' });
	});
	
	// login
	$('#frm-login').validate({
		errorElement: 'small',
		submitHandler: function(form) {
			$('#frm-login .results').remove();
			$('#frm-login').prepend('<div class="loading">loading...</div>');
			log = $('#input-log').val();
			pwd = $('#input-pwd').val();
			
			$.ajax({
				type: "POST",
				url: form.action,
				data: "log="+log+"&pwd="+pwd,
				success: function(msg){
					if (msg.match('ERROR') != null) {
						$('#frm-login .loading').remove();
						$('#frm-login').prepend('<div class="results">'+msg+'</div>');
					}
					else {
						parent.window.location.reload();
					}
				}
			});
			return false;
		}
	});
	
	// forgot
	$('#frm-forgot').validate({
		errorElement: 'small',
		submitHandler: function(form) {
			$('#frm-forgot .results').remove();
			$('#frm-forgot').prepend('<div class="loading">loading...</div>');
			forgot = $('#input_forgot').val();
			
			$.ajax({
				type: "POST",
				url: form.action,
				data: "forgot="+forgot,
				success: function(msg){
					if (msg.match('ERROR') != null) {
						$('#frm-forgot .loading').remove();
						$('#frm-forgot').prepend('<div class="results">'+msg+'</div>');
					}
					else {
						$('#frm-forgot .loading').remove();
						$('#frm-forgot').prepend('<div class="results">'+msg+'</div>');
					}
				}
			});
			return false;
		}
	});
	
/* vecia	
	$('#frm-login').validate({
		errorElement: 'small',
		submitHandler: function(form) {
			var inputs = [];
			$(form).find('input, textarea').each(function() {
				inputs.push(this.name + '=' + this.value);
			});
			
			$('#frm-login .results').remove();
			$('#frm-login').prepend('<div class="loading">loading...</div>');
			
			jQuery.ajax({
				data: inputs.join('&'),
				url: form.action,
				type: "POST",
				timeout: 3000,
				success: function(data, textStatus) {
					if (data.match('ERROR') != null) {
						$('#frm-login .loading').remove();
						$('#frm-login').prepend('<div class="results">'+data+'</div>');
					}
					else {
						parent.window.location.reload();
					}
				}
			});
				  
			return false;
		}
	});
*/	
	// register
	$('#frm-register').validate({
		errorElement: 'small',
		submitHandler: function(form) {
			$('#frm-register .results').remove();
			$('#frm-register').prepend('<div class="loading">loading...</div>');
			user_email = $('#input_user_email').val();
			user_login = $('#input_user_login').val();
			pass = $('#input_pass').val();
			
			$.ajax({
				type: "POST",
				url: form.action,
				data: "user_login="+user_login+"&pass="+pass+"&user_email="+user_email,
				success: function(msg){
					if (msg.match('ERROR') != null) {
						$('#frm-register .loading').remove();
						$('#frm-register').prepend('<div class="results">'+msg+'</div>');
					}
					else {
						$('#frm-register .loading').remove();
						$('#frm-register').prepend('<div class="results">'+msg+'</div>');
						//parent.window.location.reload();
					}
				}
			});
			return false;
	/*	
			var inputs = [];
			$(form).find('input, textarea').each(function() {
				inputs.push(this.name + '=' + this.value);
			});
			
			$('#frm-register .results').remove();
			$('#frm-register').prepend('<div class="loading">loading...</div>');
			
			jQuery.ajax({
				data: inputs.join('&'),
				url: form.action,
				type: "POST",
				timeout: 3000,
				success: function(data, textStatus) {  
					if (data.match('ERROR') != null) {
						$('#frm-register .loading').remove();
						$('#frm-register').prepend('<div class="results">'+data+'</div>');
					}
					else {
						parent.window.location.reload();
					}
				}
			});
				  
			return false;
	*/
		}
	});
	
	// attach
	$('#frm-attach-comment').validate({
		errorElement: 'small'
	});
	
	$('#frm-attach-video').validate({
		errorElement: 'small'
	});
	
	$('#frm-attach-playlist').validate({
		errorElement: 'small'
	});
	
	// generic validation
	/*$('.frm-generic').validate({
		errorElement: 'small'
	});*/
}

/******************************************************************************************************************/
// EDIT FORM HANDLE
/******************************************************************************************************************/

function handleEdit(){

	// genres
	$('#tmp-genres').focus(function(){ 
		$('#item-genre').show();	
		$('#tip-genres').show(); 
	});
	
	var myStr = '';
	$('#item-genre option:selected').each(function () {
		myStr += $(this).text() + ', ';
	});
	myStr = myStr.slice(0, -2);
	$('#tmp-genres').attr('value', myStr);
		
	$('#item-genre').change(function () {
		var myStr = '';
		$('#item-genre option:selected').each(function () {
			myStr += $(this).text() + ', ';
		});
		myStr = myStr.slice(0, -2);
		$('#tmp-genres').attr('value', myStr);
	});
	
	$('#item-genre').blur(function(){ 
		$(this).hide(); 
		$('#tip-genres').hide(); 
	});
}

/******************************************************************************************************************/
// FANCY BOXES HANDLE
/******************************************************************************************************************/

function handleFancy(){
	$('.fancybox').fancybox({
		'overlayShow': true,
		'frameWidth': 450,
		'frameHeight': 400
	});
	
	$('.btn-play, .attach-vid').fancybox({
		'overlayShow': true,
		'frameWidth': 610,
		'frameHeight': 430
	});
	
	$('.link-attach').fancybox({
		'overlayShow': true,
		'hideOnOverlayClick': 0, 
		'frameWidth': 680,
		'frameHeight': 480,
		'callbackOnClose': function(){
			window.location.reload();
		}
	});
	
	$('.btn-edit').fancybox({
		'overlayShow': true, 
		'hideOnOverlayClick': 0, 
		'frameWidth': 750, 
		'frameHeight': 450,
		'callbackOnClose': function(){
			window.location.reload();
		}
	});
	
	$('.btn-new').fancybox({
		'overlayShow': true, 
		'hideOnOverlayClick': 0, 
		'frameWidth': 750, 
		'frameHeight': 450,
		'callbackOnClose': function(){
			window.location.reload();
		}
	});
}

/******************************************************************************************************************/
// ADJUST ITEMS HEIGHT
/******************************************************************************************************************/

function adjustItemsHeight(){
	$('#main .cols .items-list .item').css('height','auto');
	
	$('#main .cols .items-list').each(function(){
		max = 0;
		$(this).find('.item').each(function(){
			myh = $(this).height();
			if (myh > max) max = myh;
		});
		
		$(this).find('.item').height(max);	
		$(this).find('.item').css('marginBottom','20px');
	});
}

/******************************************************************************************************************/
// ADJUST ITEMS HEIGHT
/******************************************************************************************************************/

function handleHistory(){
	var hnum = $('#history .carousel li').size();
	var iw = 182;
	var ml = 0;
	var diff = (hnum*iw) - 910;
	if (hnum >= 1) {
		$('#history').show();
		/* PER EVITARE CHE SI METTANO SU DUE RIGHE QUANDO SONO 5 E UNO O PIU' ITEM HANNO IL TITOLO TROPPO LUNGO */
		if (hnum > 4){
			//$('#history .arrow').show();
			
			$('#history .carousel ul').width(hnum*iw);
			
			$('#history .carousel .right').click(function(){
				ml = ml + iw; 
				if (ml <= diff) {
					$('#history .carousel ul').stop().animate({ marginLeft: '-'+ ml +'px' }, 500);	
				}
				else ml = diff;		
			});
			
			$('#history .carousel .left').click(function(){
				ml = ml - iw;
				if (ml >= 0){
					$('#history .carousel ul').stop().animate({ marginLeft: '-'+ ml +'px' }, 500);
				}
				else ml = 0;
			});
		}
		/**/
		
		if (hnum > 5){
			$('#history .arrow').show();
			
			$('#history .carousel ul').width(hnum*iw);
			
			$('#history .carousel .right').click(function(){
				ml = ml + iw; 
				if (ml <= diff) {
					$('#history .carousel ul').stop().animate({ marginLeft: '-'+ ml +'px' }, 500);	
				}
				else ml = diff;		
			});
			
			$('#history .carousel .left').click(function(){
				ml = ml - iw;
				if (ml >= 0){
					$('#history .carousel ul').stop().animate({ marginLeft: '-'+ ml +'px' }, 500);
				}
				else ml = 0;
			});
		}
	}
}

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){
	// search
	handleSearch();
	
	// generic forms
	handleForms();
	
	// tabs
	handleTabs();

	// filters
	handleDropdownFilters();
	
	// items
	handleItems();
	
	// edit form
	handleEdit();
	
	// fancyboxs	
	handleFancy();
	
	// history
	handleHistory();
	
	// notice
	$('.notice').fadeOut(4000, function(){ $(this).remove(); });
	
	// adjust items height
	adjustItemsHeight();
});