jQuery(document).ready(function() {
	aboutus(); 
    linkimgarticoli();
    formToggle('s', searchterm); checkform();
	//Shadowbox.init(opzioni);
	prettyPhoto();
	fixnextgen();
	jQuery('.commentbox').cornerz({radius:8});
	//tooltip('.recent-post a');
	if(iev == 6) {
		DD_belatedPNG.fix('.pp_left,.pp_right,a.pp_close,a.pp_arrow_next,a.pp_arrow_previous,.pp_content,.pp_middle');
	}
	footnotes('.post-content');
	anteprimacommento();
	enable_smooth_scroll();
	preloadimages();
});

var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent)); if(ievs) var iev= new Number(RegExp.$1);
jQuery.browser.msie6 = jQuery.browser.msie && (jQuery.browser.version && jQuery.browser.version < 7 || /6.0/.test(navigator.userAgent));


// attribuisco il link alla prima immagine del post prendendolo dal titolo
function linkimgarticoli(){
	jQuery("body.home .post, body.category .post, body.tag .post, body.search .post").each(function(i){
		var myURL = jQuery(this).find('h2 a').attr('href');
		jQuery(this).find('img:first').mouseover(function () {
			jQuery(this).css({"cursor":"pointer"});
		}).click(function () {	
			window.location = myURL;
			//alert(i+ " " + myURL);
		})
	});
}


function preloadimages(){
	jQuery("#colB img").lazyload({ 
	    placeholder : "http://www.chefcecio.it/wp-content/themes/chefcecio/images/pink.png",
	    effect : "fadeIn"
	});
}


function fixnextgen(){
	jQuery(".ngg-gallery-thumbnail").eq(3).css({"margin-right":"0"});
	jQuery(".ngg-gallery-thumbnail").eq(7).css({"margin-right":"0"});
	jQuery(".ngg-gallery-thumbnail").eq(11).css({"margin-right":"0"});
	jQuery(".ngg-gallery-thumbnail").eq(15).css({"margin-right":"0"});
	jQuery(".ngg-gallery-thumbnail").eq(19).css({"margin-right":"0"});
	}

// fumetto chisono sull'icona
function aboutus(){
//	jQuery('#footer').append('<div class="footer-about" style="display:none"><img src="'+myurlsite+'/images/chefcecio-about.jpg" alt="" /></div>');
	jQuery('#footer').append('<div class="footer-about" style="display:none"><img src="http://www.chefcecio.it/wp-content/themes/chefcecio/images/chefcecio-about.jpg" alt="" /></div>');
	jQuery('.footer-picture a').mouseover(function () {
		jQuery(".footer-about").fadeIn();
	}).mouseout(function () {
		jQuery(".footer-about").fadeOut();
	});	
}


function formToggle(p1, p2){
    jQuery('input'+'#'+p1).focus(function () {
        if (this.value == p2) {this.value = '';}
    });
    jQuery('input'+'#'+p1).blur(function () {
        if (this.value == '') {this.value = p2;}
    });
}

 
function checkform() {
jQuery("#navigation-search").submit(function() {
	if(jQuery("#s").val() == searchtermdefault || jQuery("#s").val() == '' ) {
		alert("Inserire almeno una parola per la ricerca");
		return false;
		}
	});
}

function tooltip(elemento){	
	xOffset = 2; yOffset = 10;
	//alert(jQuery("").)
	typeof elemento == "undefined" ? separatore = "" : separatore = ",";	
	jQuery(elemento+separatore+".tooltip").hover(function(e){											  
		this.t = this.title; this.title = "";									  

		jQuery("body").append("<p id='tooltip'>"+ this.t +"</p>");
		jQuery("#tooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").fadeIn("fast");
    },
	function(){
		this.title = this.t;		
		jQuery("#tooltip").remove();
    });	
	jQuery(elemento+separatore+".tooltip").mousemove(function(e){
		jQuery("#tooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
	});			
};

function prettyPhoto() {
	jQuery('a[rel^="prettyPhoto"]').prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 40, /* padding for each side of the picture */
			opacity: 0.65, /* Value betwee 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: ' di ', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
		});
	}


function anteprimacommento() {
  jQuery('#comment').one('focus',function() {
    jQuery('#comment').parent().after('<h3 class="comment-preview">Anteprima del commento &darr;</h3><ol class="commentlist"><li class="comment"><div class="commentbox corner8" id="live-preview"></div><div class="commentfooter">Inviato da <em>il tuo nome</em></div></li></ol>'); 
});
  var jQuerycomment = ''; // that's two single quotation-marks at the end
  jQuery('#comment').keyup(function() {
    jQuerycomment = jQuery(this).val();
    jQuerycomment = jQuerycomment.replace(/\n/g, "<br />").replace(/\n\n+/g, '</p><p>').replace(/(<\/?)script/g,"jQuery1noscript");
    jQuery('#live-preview').html( jQuerycomment );
  });
};





function footnotes(div){
	// logo per la stampa
	jQuery('#header h1').after('<span id="brand-print" class="hide"><img src="http://www.chefcecio.it/wp-content/themes/chefcecio/images/chefcecio-logo-print.jpg" alt="Chefcecio - Progetti al tegamino"/></span>');
	// footer per la stampa
	jQuery('#footer').after('<span id="footer-print" class="hide">L&rsquo;indirizzo di questa pagina web &egrave;: <strong>'+window.location.href+'</strong><ol id=\"footnotes\"></ol></span>');

	footnote = 1;
	jQuery(div+" a").addClass("footnote");
	jQuery(".footnote").each(function() {
		jQuery(this).append("<span class=\"sup hide\">"+footnote+"</span>");
			cite="<li><a href=\""+jQuery(this).attr("href")+"\">"+jQuery(this).attr("href")+"</a></li>";
		jQuery("#footnotes").append(cite);
		footnote++;
	});

}

/*-----------------------------------
  TOOLS: SMOOTH SCROLL 
// http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
// major changes by Paul Armstrong and Zachary Johnson
  -----------------------------------*/
function enable_smooth_scroll() {
    function filterPath(string) {
        return string
                .replace(/^\//,'')
                .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);

    var scrollElement = 'html, body';
    jQuery('html, body').each(function () {
        var initScrollTop = jQuery(this).attr('scrollTop');
        jQuery(this).attr('scrollTop', initScrollTop + 1);
        if (jQuery(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            jQuery(this).attr('scrollTop', initScrollTop);
            return false;
        }    
    });
    
    jQuery('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if  (   locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/, '')
            ) {
                if (jQuery(this.hash).length) {
                    jQuery(this).click(function(event) {
                        var targetOffset = jQuery(this.hash).offset().top;
                        var target = this.hash;
                        event.preventDefault();
                        jQuery(scrollElement).animate({scrollTop: targetOffset}, 500, function() {
                            location.hash = target;
                        });
                    });
                }
        }
    });
}