﻿if($){
	$(document).ready(function(){  
  
      
    $("ul#nav li").mouseenter(function() {
        $sub_nav = $(this).find("ul.sub-menu");//.slideDown().show(); //Drop down the subnav on click  
		if ($sub_nav.length > 0){
			$('#sub-menu-shadow').slideDown('fast');
			$sub_nav.slideDown('fast');
			$sub_nav.find('li a').hover(function(){$(this).stop(true).animate({paddingLeft: '60px'}, {speed: 100, easing: 'easeOutBack'});},
									  function(){$(this).stop(true).animate({paddingLeft: '30px'}, {speed: 100, easing: 'easeOutBounce'});});
		}
	});
		
	$("ul#nav li").mouseleave(function() {
		$sub_nav = $(this).find("ul.sub-menu");//.slideDown('fast').show(); //Drop down the subnav on click  
		if ($sub_nav.length > 0){
			$sub_nav.slideUp('fast');
			$('#sub-menu-shadow').slideUp('fast');
		}
	});

	$("ul#contacts li").css( {marginTop: "32px", height: "32px"} )
		.mouseover(function(){
			$(this).stop().animate({marginTop:"0", height: "64px"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({marginTop: "32px", height: "32px"}, {duration:500})
		});
	
		
	// RIGHT TABS
		
	$('#search-bar').show();
	$('#tab-user,#tab-username').mouseenter(function(){$('#user-panel').show();}).mouseleave(function(){$('#user-panel').hide();});
	// $('#tab-search').click(function(){$('#login-form, #login-arrow').hide();$('#search-bar, #search-arrow').fadeIn();});
	// $('#tab-login,#tab-user,#tab-username').click(function(){$('#search-bar, #search-arrow').hide();$('#search-bar').find('#mini-arrow').hide();$('#login-form, #login-arrow').fadeIn();});
	
	// $('#user-text').val('Username');$('#passw-text').val('123456');
	// $('#user-text').focus(function(){if($(this).val()=="Username") $(this).val(''); });
	// $('#user-text').blur(function(){if($(this).val()=="") $(this).val('Username');});
	// $('#passw-text').focus(function(){if($(this).val()=="123456") $(this).val(''); });
	// $('#passw-text').blur(function(){if($(this).val()=="") $(this).val('123456');});
	
	// END RIGHT TABS
	
	// QTIP
	
	$('[title]').qtip({ 
	style: { name: 'green', tip: true }, 
	position: {corner: {target: 'topMiddle', tooltip: 'bottomMiddle'}}
	}); 
	
	$('.tip-left').qtip("destroy");$('.tip-right').qtip("destroy");
	$('.tip-left').qtip({content: "Home",style: { name: 'red', tip: true },position: {corner: {target: 'leftMiddle', tooltip: 'rightMiddle'}} });
	$('.tip-right').qtip({content: "Dove siamo",style: { name: 'green', tip: true },position: {corner: {target: 'rightMiddle', tooltip: 'leftMiddle'}} });
	
	// END QTIP
	
	// CARICAMENTO NOVITA-CONSIGLIATI-RECENSIONI
		$.get('services/book-control.php?type=novita', function(data){
			$('#novita').append(data);
			$("ul.book-list li img").reflect({height: '20'});
			
			$("ul.book-list li").mouseenter(function(){
			$(this).find('img').unreflect();
			$(this).stop().animate({paddingTop:"20px",marginLeft:"-30px"}, 500);
			$(this).find('img').stop().animate({height:"210px"}, 500);
			$(this).css('zIndex', 5);
			$(this).find('div.etichetta').show("fade",{}, 500);
		})
		.mouseleave(function(){
			$(this).find('img').unreflect();
			$(this).find('div.etichetta').hide("fade",{}, 200);
			$(this).stop().animate({paddingTop:"40px",marginLeft:"0px"}, 500);
			$(this).css('zIndex', 1);
			$(this).find('img').stop().animate({height:"150px"}, 500, function(){$(this).reflect({height: '20'});});
		});
		});
		
		$.get('services/book-control.php?type=consigliati', function(data){
			$('#consigliati').append(data);
			$("ul.book-list li img").reflect({height: '20'});
			
			$("ul.book-list li").mouseenter(function(){
			$(this).find('img').unreflect();
			$(this).stop().animate({paddingTop:"20px",marginLeft:"-30px"}, 500);
			$(this).find('img').stop().animate({height:"210px"}, 500);
			$(this).css('zIndex', 5);
			$(this).find('div.etichetta').show("fade",{}, 500);
		})
		.mouseleave(function(){
			$(this).find('img').unreflect();
			$(this).find('div.etichetta').hide("fade",{}, 200);
			$(this).stop().animate({paddingTop:"40px",marginLeft:"0px"}, 500);
			$(this).css('zIndex', 1);
			$(this).find('img').stop().animate({height:"150px"}, 500, function(){$(this).reflect({height: '20'});});
		});
		});
		
		$.get('services/book-control.php?type=recensiti', function(data){
			$('#ultime-recensioni').html(data);
			$('.stars').each(function(){
				$(this).stars({inputType: "select",disabled:true});
				$(this).stars("select",$(this).parent().find('.rate').text());
			});
		});
	
	
	// END CARICAMENTO NOVITA&CONSIGLIATI&RECENSITI
	
	$('#paging-container').pajinate({
		items_per_page:5,
		num_page_links_to_display: 5,
		nav_label_first: '<<',
		nav_label_prev: '<',
		nav_label_next: '>',
		nav_label_last: '>>'
	});
	
	var idrec=0; 
	// var getIdrec=$.getURLParam(".idrec");
	// if (getIdrec!=null) idrec=getIdrec;
	
	$(".idrec").each(function(i){
		if ($(this).val() == $.getURLParam("idrec"))
			idrec = i;
	});

	$('#middle-recensioni').pajinate({
		items_per_page:1,
		num_page_links_to_display: 5,
		start_page: idrec,
		nav_label_first: '<<',
		nav_label_prev: '<',
		nav_label_next: '>',
		nav_label_last: '>>'
	});
	
	$('.to-login').click(function(){$('#tab-login').trigger('click');});
	
	
	// SEARCH XHR
	$('input').keypress(function (e) {
            var code = null;
            code = (e.keyCode ? e.keyCode : e.which);
            return (code == 13) ? false : true;
        });
		
	$('#search-button').click(function (e) {
            q = $('#search-text').val();
			if (q != ""){
				window.location.href = "index.php?p=cerca&q="+q;
			}
			else
				return false;
        });

	var runningRequest = false;
    var request;
   //Identify the typing action
    $('input#search-text').keyup(function(e){
        e.preventDefault();
		
        var $q = $(this);

		if (e.keyCode != '13') {
		
        if($q.val() == ''){
            $('div#search-results').html('');
			$('#box-results div.top-list').hide();
			$('#search-results').slideUp();
			
            return false;
        }

        //Abort opened requests to speed it up
        if(runningRequest){
            request.abort();
        }
		$('#search-loading').ajaxStart(function(){
			$(this).show();
			$('#box-results div.top-list').hide();
			$('#search-results').slideUp();
			
		});

        runningRequest=true;
        request = $.get('services/search.php',{
            q:$q.val(),ckautore:'on',cktitolo:'on',ckeditore:'on',limit:5
        },function(data){           
            // showResults(data,$q.val());
			$('#search-loading').hide();
			$('#search-results').html(data);
			$('#box-results div.top-list').show();
			$('#search-results').slideDown();
			var q = $q.val().split(" ");
			// for (i=0;i<q.length;i++) $('#search-results').highlight(q[i]);
            runningRequest=false;
        });

        $('form').submit(function(e){
            e.preventDefault();
        });
		
		}else{
			e.preventDefault();
			if ($q.val() != ""){
				window.location.href = "index.php?p=cerca&q="+$q.val();
			}
		}
    });
	
	$('#user-delete-dialog').dialog({autoOpen: false,modal: true,resizable:false,draggable:false});
	$('#user-delete').click(function(e) {
		e.preventDefault();
		$('#user-delete-dialog').dialog({
			title: 'Conferma rimozione account',
			width: 500,
			buttons : {
				"Conferma" : function() {
					window.location.href = "services/user-control.php?action=delete-account";
				},
				"Annulla" : function() {
					$(this).dialog("close");
				}
			}
		});
		$("#user-delete-dialog").dialog("open");
	});
	
	$('#contact-dialog').dialog({autoOpen: false,modal: true,resizable:false,draggable:false,title: 'Contattaci',width: 500});
	$('.mail').click(function(e) {
		e.preventDefault();
		$('#contact-dialog').dialog('open');
		});
	
	$('#registration-dialog').dialog({autoOpen: false,modal: true,resizable:false,draggable:false,title: 'Registrazione',width: 500});
	$('.tab-register').click(function(e) {
		$("#registration-dialog").dialog("open");
	});
	$('.only-alpha-num').alphanumeric();
	$("#registration-form").validate({
		rules: {
			username: {
				required: true,
				minlength: 3
			},
			password: {
				required: true,
				minlength: 5
			},
			confirm_password: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			email: {
				required: true,
				email: true
			},
			captcha: {
				required: true,
				remote: "services/captcha/process.php"
			},
			agree: "required"
		},
		messages: {
			username: {
				required: "Inserire il nome utente",
				minlength: "Username di almeno 3 caratteri"
			},
			password: {
				required: "Inserire password",
				minlength: "Password di almeno 5 caratteri"
			},
			confirm_password: {
				required: "Inserire conferma password",
				minlength: "Password di almeno 5 caratteri",
				equalTo: "La password non coincide"
			},
			captcha: "Codice errato. Aggiornare cliccando sull'immagine.",
			email: "Email non valida",
			agree: "Accettare la policy"
		}
	});
	
	$('#error').dialog({autoOpen: true,modal: true,resizable:false,draggable:false,buttons : {"OK" : function() {$(this).dialog("close");}	}});
	
	$('#info').dialog({autoOpen: true,modal: true,resizable:false,draggable:false,buttons : {"OK" : function() {$(this).dialog("close");}	}});
	
	
	$('#login-dialog').dialog({autoOpen: false,modal: true,resizable:false,draggable:false});
	$('#tab-login').click(function(e) {
		e.preventDefault();
		$('#login-dialog').dialog({
			title: 'Login',
			width: 500
		});
		$("#login-dialog").dialog("open");
	});
	
	$("#login-form").validate({
		rules: {
			username: {
				required: true,
				minlength: 3
			},
			password: {
				required: true,
				minlength: 5
			}
		},
		messages: {
			username: {
				required: "Inserire il nome utente",
				minlength: "Username di almeno 3 caratteri"
			},
			password: {
				required: "Inserire password",
				minlength: "Password di almeno 5 caratteri"
			}
		}
	});
	
	$("#contact-form").validate({
		rules: {
			name: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			object: {
				required: true
			},
			captcha: {
				required: true,
				remote: "services/captcha/process.php"
			},
			message: {
				required: true
			}
		},
		messages: {
			name: {
				required: "Inserisci il tuo nome"
			},
			object: {
				required: "Inserire l'oggetto del messaggio"
			},
			message: {
				required: "Inserire il messaggio"
			},
			captcha: "Codice errato. Aggiornare cliccando sull'immagine.",
			email: "Email non valida"
		}
	});
	
	$("#rec-passw-form").validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: "Email non valida"
		}
	});
	
	$('#rec-passw-dialog').dialog({autoOpen: false,title:"Recupera password",width:500,modal: true,resizable:false,draggable:false});
	$('#rec-passw').click(function(){
		$('#login-dialog').dialog('close');
		$('#rec-passw-dialog').dialog('open');
	});
	
	$('#link-register').click(function(){
		$('#login-dialog').dialog('close');
		$('#registration-dialog').dialog('open');
	});
	
	// CAPTCHA
	$("#captchaimage").live('click',function(){
		$.post('services/captcha/newsession.php');
		$("#captchaimage").load('services/captcha/image_req.php');
		return false;
	});
	
	$("#c-captchaimage").live('click',function(){
		
		$.post('services/captcha/newsession.php');
		$("#c-captchaimage").load('services/captcha/image_req.php');
		return false;
	});
	
	$("#gmaps").gMap({ markers: [{ latitude: 40.794452, longitude: 16.921099, html: "<div style='line-height:25px;'><h3>Libreria Minerva</h3><p>via Carlo Soria, 25</p><p>70023, Gioia del Colle (BARI)</p></div>", popup: true  }], zoom: 16 });

	// if ($('.std-static').height()+30 < $('#right').height()+330) $('.page-description').height($('#right').height()-200+330);
	
	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'facebook',slideshow:5000, autoplay_slideshow:false});
});
}
