/******************************************************************************************************************/
// SINGLE ITEM HANDLE
/******************************************************************************************************************/

function singleConnects(){
	$('#connections .ajax').html('<div class="loading">loading...</div>');
	
	// qui va gestita anche la select! al change vanno ricaricate le connections...
	
	// ajax loading
	var myitem = $('#single .myid').html();
	//console.log('myitem: ' + myitem);
	if( myitem != '' ){
		//console.log(myitem);
		mylink = '/wp-content/themes/filtersage/ajax/ajax-items-single.php?id=' + myitem;
		
		jQuery.ajax({
			url: mylink,
			success: function(html){
				$('#connections .ajax').html(html);
				num = $('#connections .item').size();
				$('#connections .title h3 .num').html(num);
				
				// score
				$('.score').css({ opacity: 0.3 });
				$('.tip').css({ opacity: 0.9, margin: 0 });
				
				$('.item .score').hover(function(){
					$(this).css({ opacity: 1 });
					$(this).find('.tip').show().animate({ margin: '50px 0px 0px -60px' }, 200);		
				}, function(){
					$(this).css({ opacity: 0.3 });
					$(this).find('.tip').hide().css({ margin: 0 });;
				});
				
				$('.link-score').fancybox({
					'overlayShow': true,
					'hideOnOverlayClick': 0, 
					'frameWidth': 680,
					'frameHeight': 480,
					'callbackOnClose': function(){
						window.location.reload();
					}
				});
				
				$('.btn-play').fancybox({
					'overlayShow': true,
					'frameWidth': 610,
					'frameHeight': 430
				});
				
				// delete connection
				handleDeleteConnection();
			}
		});
	}
}

function handleSearchConnect(){
	// connections search box
	$('#frm-search-connect .frm-text').focus(function(){ 
		$('#frm-search-connect').css({ backgroundPosition: '0px -40px' });
		$(this).css({ color: '#000000' });
		this.value = "";
	});
	
	$('#frm-search-connect .frm-text').blur(function(){ 
		$('#frm-search-connect').css({ backgroundPosition: '0px 0px' });
		$(this).css({ color: '#7f7f7f' });
		if (this.value == "") {
			this.value = "Tell us...";
			$('#suggestions').html('').hide();
			$('.nofind').hide();
		}
	});
	
	// on key-up event
	var mykey = '';
	
	$('#frm-search-connect .frm-text').keyup(function() {
		$('#btn-publish, #step2 .right small').show();
		$('#suggestions').html('<div class="loading">loading...</div>').show();
		
		mypos = $('#suggestions').position();
		$(window).scrollTop(mypos.top - 110);
		
		mykey = this.value;		

		if (mykey != '') {
		
			if (this.value != this.lastValue) {	
			
				if(this.timer) clearTimeout(this.timer);
				
				this.timer = setTimeout(function() {	
					mycat = $('#single .mycat').html();
					mylink = '/wp-content/themes/filtersage/ajax/ajax-items-suggest.php?key='+ mykey + '&cat=' + mycat;	
				
					jQuery.ajax({
						url: mylink,
						success: function(html){
							$('#suggestions').html(html);
							$('#suggestions').prepend('<div class="msg">Select an item from the list:</div>');
							
							// suggested item click						
							$('#suggestions .item').click(function(){	
								$('#step1').hide();
								$('#step2').show();
								
								$('#step2 .item').html($(this).html());
								$('#step2 .name').html($('#step2 .item h1').html());
								
								myglue = $(this).find('.myglue').html();
								tmp = $('#btn-publish').attr('href');
								mylink = tmp + '&myglue=' + myglue;
								
								$('#btn-publish').attr('href', mylink);
								
								// connect
								$('#btn-publish').click(function(){
									$('#btn-publish, #step2 .right small').hide();
									$('#step2 .right').prepend('<div class="loading">loading...</div>');
									
									jQuery.ajax({
										url: mylink,
										success: function(html){
											// go back to search
											$('#step2').hide();
											$('#step1').show();
											
											$('#step2 .loading').remove();
											
											$('#suggestions').html('').hide();
											$('.nofind').hide();
											$('#frm-search-connect .frm-text').val('');
											
											// reload connections
											singleConnects();
											
											mypos = $('#connections').offset();			
											$(window).scrollTop(mypos.top - 20);
										}
									});
								
									return false;
								});
								
								// go back to search
								$('#step2 small a').click(function(){
									$('#step2').hide();
									$('#step1').show();
								
									return false;
								});							
							});
							
							$('#step1 .nofind').show();
							$('.stepCreate').hide();
						}
					});			
				}, 1000);
				
				this.lastValue = this.value;
			}
		}
		else {
			$('#suggestions').html('').hide();
		}
	});
}

function handleCoverUpload(){
	$('#frm-upload').hide();
	
	$('.upload-link').toggle(function(){
		$('#frm-upload').show();
	}, function(){
		$('#frm-upload').hide();
	});
	
	$('.upload-link').click(function() { return false; });
}

function handleDeleteConnection(){
	$('.connection-delete').click(function() {
		var connection_id = $(this).attr('rel');
		if( connection_id != '' ){
			$(this).hide();
			$('#div-connection-delete-'+connection_id).html('<div class="loading">loading...</div>').show();
			mylink = '/wp-content/themes/filtersage/ajax/ajax-items-connection-delete.php?connection_id='+ connection_id;
			jQuery.ajax({
				url: mylink,
				success: function(html){
					$('#div-connection-delete-'+connection_id+' .loading').remove();
					if( html ){
						// reload connections
						singleConnects();
					}else{
						$('#div-connection-delete-'+connection_id).html('Not delete');
					}
				}
			});
			return false;
		}
	});
}

function searchAmazon(){
	$('.gotostepCreate').click(function(){
		$('.stepAmazon').hide();
		// loading amazon search
		
		$('#suggestions').html('');
		$('#suggestions').html('<div class="loading">loading...</div>').show();
		mykey = $('#frm-search-connect .frm-text').val();
		mycat = $('#single .mycat').html();
		
		mylink = '/wp-content/themes/filtersage/ajax/ajax-items-suggest.php?key='+ mykey + '&cat=' + mycat + '&engine=most';
		if( (mykey != '') && (mycat != '') ){
			jQuery.ajax({
				url: mylink,
				success: function(html){
					$('#suggestions').html(html);
					$('#suggestions').prepend('<div class="msg">Select an item from the list:</div>');
			/*	
			*/
					
					// suggested item click						
					$('#suggestions .item').click(function(){
						$('#step1').hide();
						$('#step2').show();
						
						$('#step2 .item').html($(this).html());
						$('#step2 .name').html($('#step2 .item h1').html());
						
						//myglue = $(this).find('.myglue').html();
						myasin = $(this).find('.myasin').html();
						myurl = $(this).find('.myurl').html();
						tmp = $('#btn-publish').attr('href');
						mylink = tmp + '&myasin=' + myasin + '&myurl=' + myurl;
						
						
						$('#btn-publish').attr('href', mylink);
						
						// connect
						$('#btn-publish').click(function(){
							$('#btn-publish, #step2 .right small').hide();
							$('#step2 .right').prepend('<div class="loading">loading...</div>');
							
							jQuery.ajax({
								url: mylink,
								success: function(html){
									// go back to search
									$('#step2').hide();
									$('#step1').show();
												
									$('#suggestions').html('').hide();
									$('.nofind').hide();
									$('#frm-search-connect .frm-text').val('');
												
									
									$('#step2 .loading').remove();
									
									// reload connections
									singleConnects();
									
									mypos = $('#connections').offset();			
									$(window).scrollTop(mypos.top - 20);
								}
							});
						
							return false;
						});
						
						// go back to search
						$('#step2 small a').click(function(){
							$('#step2').hide();
							$('#step1').show();
						
							return false;
						});							
					});
					
				
					
					
					$('#step1 .nofind').show();
			
					$('.stepCreate').show();
					$('.stepAmazon').hide();
					//$('.stepCreate').hide();
				}
			});
		}
		
	});
}

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

$(function(){
	// single connections
	singleConnects();
	
	// search connections
	handleSearchConnect();
	
	// upload cover
	handleCoverUpload();
	
	// search on amazon
	searchAmazon();
	
	// filter attachs
	$('#contributes .frm-radio input:first').attr('checked','checked');
	
	$('#contributes .frm-radio input').click(function(){
		if (this.value == 'all') $('#contributes li').show();
		else {
			myclass = '.'+this.value;
			$('#contributes li').hide();
			$('#contributes').find(myclass).parents('li').show();
		}
	});	
});