var fbAppId = '117048241684253';


$(document).ready(function (){
    var ieversion;
    if(jQuery.browser.msie){
       ieversion=parseInt(jQuery.browser.version.substr(0,3));
    }
    
    $('.gallery').gq_gallery();
    $('.addthis_button_tweet iframe, .addthis_button_facebook_like iframe').css({'width':100}).attr('width', 100);

    /* tabs */
    $('.panes>ul').css({'display':'none'});
    $('.tabs h4').click(function(){
        $('.tabs .sel').removeClass('sel');
        var index=$(".tabs h4").index(this);
        $(this).addClass('sel');
        $('.panes>ul:visible').hide();
        $('.panes>ul:eq('+index+')').show();
        return false;
    });
    $('.tabs h4:last').click();

    // __________________________________ 
    // centering submenu 
    // __________________________________ 

    var selW = $(".submenuContainer .submenuCenter ul").width() + (28 * 2);
    var otherW = (1000 - selW) / 2;
    //console.log('selW = ' + selW);

    $(".submenuContainer .submenuLeft, .submenuContainer .submenuRight").width(otherW);
    $(".submenuContainer .submenuCenter").width(selW);
    $(".submenuContainer").css("visibility", "visible");
    
    /* authors*/
    $('.widget_authors ul').wrap('<div id="authContainer" />').wrap('<div id="authWrapper" style="overflow:hidden;"/>');
    $('#authWrapper').before('<a class="prev" href="#">prev</a>').after('<a class="next" href="#">next</a>').serialScroll({
		items:'li',
		prev:'.widget_authors a.prev',
		next:'.widget_authors a.next',
		start:0,
		offset:-1,
		axis:'y',
		duration:600,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		step:6,
		onBefore:function( e, elem, $pane, $items, pos ){
            var $prev=$('.widget_authors a.prev');
            var $next=$('.widget_authors a.next');
            $prev.add($next).css('visibility','visible');
            if( pos == 0 )
                    $prev.css('visibility','hidden');
            else if( pos >= $items.length-7 )
                    $next.css('visibility','hidden');
        }
	});
	$('.widget_authors a.prev').css('visibility','hidden');
    /* authors*/

    
    /* lightbox */
    var url='';
    function boxClosed(){$('#inlinebox').remove();}
    function boxComplete(){$('#inlinebox').flash({swf:url, width:'100%', height:'100%'});}
    $(".lighbox").click(function(){
        var m=$(this).attr('class').match(/\d{3}/g),
            wd=900,
            hg=600;
        if(m){
            wd=m[0];
            hg=m[1];
        }
        closetxt='<a class="close" href="#">Chiudi</a>';
        var type=$(this).attr('href').match(/\.\w{3}$/i);
        if(type && '.gif.png.jpg.swf'.indexOf(type[0].toLowerCase())!=-1){
            if(type[0].toLowerCase()=='.swf'){
                $('body').append('<div id="inlinebox" style="width:'+wd+'px; height:'+hg+'px;" ></div>');
                url=$(this).attr('href');
                $.colorbox({href:'#inlinebox', scrolling:false, inline:true, opacity:0.7, onComplete:boxComplete, onClosed:boxClosed, close:closetxt});
            }else{
                $.colorbox({href:$(this).attr('href'), inline:true, opacity:0.7, maxWidth:wd, maxHeight:hg, close:closetxt});
            }
        }else{
            $.colorbox({href:$(this).attr('href'), iframe:true, scrolling:$(this).hasClass('scrolling'), opacity:0.7, width:wd, height:hg, close:closetxt});
        }
        if((ieversion>6) || (!ieversion)){
            $('#colorbox').addClass('fixed');
            $('#colorbox').css({'margin-left':-parseInt(wd/2), 'margin-top':-parseInt(hg/2)});
        }
        return false;
    });
    /* lightbox */

    // __________________________________ 
    // Right Sidebar: Box Horoscope 
    // __________________________________ 

    $(".horoscMenu a").bind("click", function (e) {
        e.preventDefault();
        var whichSign = $(this).attr("href");

        $(".horoscContent li").hide();
        $(whichSign).fadeIn();
        $(this).parents("ul").find("li").removeClass("sel");
        $(this).parent("li").addClass("sel");


    });



    // __________________________________ 
    // LOGIN
    // __________________________________ 

    $("#floatAdv0").show().find(".floatAdvBg").fadeTo('slow', 0.7);

    $("#floatAdv0 .close").bind("click", function (e) {
        $("#floatAdv0").fadeOut();
    });



    $("#loginLink").bind('click', function (e) {
        e.preventDefault();
        if ($("#loginPopup:visible").length == 0) {
            $("#loginPopup").fadeIn();
            $(this).addClass('active');
        }
        else {
            $("#loginPopup").fadeOut();
            $(this).removeClass('active');
        }
    });

    $("#loginPopup input:text").bind('focus', function (e) {
        thisValue = $(this).attr("value");
        if (thisValue == "Username") $(this).attr("value", "");
    });

    $("#loginPopup input:password").bind('focus', function (e) {
        thisValue = $(this).attr("value");
        if (thisValue == "Password") $(this).attr("value", "");
    });

    $("#loginPopup .close").bind('click', function (e) {
        $("#loginPopup").fadeOut();
        $("#loginLink").removeClass('active');
    });

    // __________________________________ 
    // SEARCH
    // __________________________________ 

    $('.searchBox .input0').bind('focus', function (e) {
        thisValue = $(this).attr("value");
        if (thisValue == "Cerca in Vanity Fair") $(this).attr("value", "");
    });




    var webservice=false;
    
    /* riconoscimento utente style.it*/
    if (webservice){
        $.ajax({
        type: "GET",
        url: 'index.php',
        data : { user_handler : "get" },
        dataType: "html",
        error: function(request,error, errorThrown){alert("ERROR: " + error+"  "+errorThrown);},
        success: function(data){
            if(data!=''){
                $('.personalarea').html(data);
                $(".saveItem").bind("click", function (e) {
                    e.preventDefault();
                    //$(this).text("SALVATO").addClass("saveItemSaved1");
                    _vanitySaveBtn = $(this);
                    var t_data = $(this).parent().serialize();
                    //$.getJSON('http://www.geonames.org/postalCodeLookupJSON?postalcode=10504&country=US&callback=?', t_data, function (data) {
                    $.get(vmsave, t_data, function (data) {
                        //Code = 0 ok
                        //Code = 10 Duplicate PK                   
                        if (data.Code == 10)
                            $(_vanitySaveBtn).text("GIA' SALVATO").addClass("saveItemSaved1");
                        if (data.Code == 0)
                            $(_vanitySaveBtn).text("SALVATO").addClass("saveItemSaved1");   
                    });
                });
    
                $(".saveItem").bind("mouseenter", function (e) {
                    e.preventDefault();
    
                    if ($(this).hasClass("saveItemSaved")) $(this).text("GIA' SALVATO");
                    else $(this).text("SALVA");
                });
    
                $(".saveItem").bind("mouseleave", function (e) {
                    e.preventDefault();
                    if ($(this).hasClass("saveItemSaved1")) $(this).removeClass("saveItemSaved1").addClass("saveItemSaved");
                });
            }else{
                var t = false;
                $(".article .tooltip").live("mouseleave", function (e) {
                    var $this = $(this);
                    t = setTimeout(function () {
                        $this.fadeOut(function (e) { $this.remove(); });
                        t = false;
                    }, 250);
                }).live("mouseenter", function (e) {
                    if (t)
                        clearTimeout(t);
                });
    
                $(".saveItem").click(function (e) {
                    $("<div/>", {
                        "class": "tooltip",
                        html: "<div class='padd'><b>ATTENZIONE</b><p>Per poter salvare l'articolo<br />devi effettuare la <a href='#' onclick='javascript:$(\"#loginLink\").click();'>login</a> oppure <br /><a href='http://life.vanityfair.it/registra/'>registrarti</a></p></div>"
    
                    }).appendTo($(this).parents("li.article"));
                });

            }
            $('.personalarea').show();
            if(typeof window.loadComments == 'function') {
               loadComments();
            }
        }
        });
    }else{
        if(typeof window.loadComments == 'function') {
           loadComments();
        }
    }
});

