var BannerOpen = false;
var nav_style;

$(document).ready(function() {

    //add Class=Last to last child of LIs
    $("li:last-child").addClass('last');


    //-------------------------------------------------------------------
    //----------- STUPID BROWSER DETECTION (IE6) ------------ (I HATE IE)

    //detect the sucker
    function badBrowser() {
        if ($.browser.msie && parseInt($.browser.version) <= 6) {
            return true;
        } else {
            //if($.browser.mozilla || $.browser.safari){ return true;} //test Firefox or Safari
            return false;
        }
    }

    //plant cookie
    function setBadBrowser(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
	        ((expiredays == null) ? "" : ";expires=" +
	        exdate.toGMTString());
    }

    //get cookie
    function getBadBrowser(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }

    //handle the sucker
    if (badBrowser() && getBadBrowser('browserWarning') != 'seen') {
        $("<div id='browserWarning'><h1>Warning!</h1><p>The web browser that you are currently using can not support many enhanced features this website and many others provide.<br />Please switch to <a href='http://getfirefox.com'>FireFox</a>, <a href='http://www.apple.com/safari/'>Safari</a> or <a href='http://www.microsoft.com/windows/downloads/ie/getitnow.mspx'>Internet Explorer 7</a>. Thanks!</p><a id='warningClose' href='#'>close</a></div>").prependTo("body");
    }

    $('div#browserWarning a#warningClose').click(function() {
        setBadBrowser('browserWarning', 'seen');
        $('#browserWarning').remove();
        $("div#warningBg").remove();
        return false;
    });


    //-------------------------------------------------------------------	
    //-------------------------- TOP BAR --------------------------------

    $('#topbar #utilsleft li, #topbar #utilsright li')
	.removeClass('highlight')
	.find('a')
	.append('<span class="hover" />').each(function() {

	    var $span = $('> span.hover', this).css('opacity', 0);

	    $(this).hover(function() {
	        $span.stop().fadeTo(300, 1);
	    }, function() {
	        $span.stop().fadeTo(500, 0);
	    });
	});

    //----- Promo Banner Open Close -----

    var $cropbox = $('#promobanner div#crop');
    $cropbox.after('<div id="openclose"><span id="close"><img src="images/promo_banner_arrow_up.jpg" alt="Chiudi slider" /></span><span id="open"><img src="images/promo_banner_arrow_down.jpg" alt="Apri slider" /></span></div>');
	
	if (BannerOpen){
		$('div#openclose').toggle(
			function() {
				$cropbox.fadeOut(400);
				$('div#promobanner img.slideimg').fadeOut(400);
				$('a#backbtn').fadeOut(400);
				$('a#nextbtn').fadeOut(400);
				$('div#navi').fadeOut(400);
	
				setTimeout(function() {
					$('div#promobanner').animate({ height: "26px" }, 500);
					$('span#open').fadeIn('medium');
					$('span#close').fadeOut('medium');
				}, 410);
				//alert("open-close");
			},
	
			function() {
				$('div#promobanner').animate({ height: "365px" }, 500);
				$('span#close').fadeIn('medium');
				$('span#open').fadeOut('medium');
	
				setTimeout(function() {
					$cropbox.fadeIn(600);
					$('div#promobanner img.slideimg').fadeIn(300);
					$('a#backbtn').fadeIn(450);
					$('a#nextbtn').fadeIn(450);
					$('div#navi').fadeIn(450);
				}, 650);
				//alert("open-open");
			}
		);
	}else{
		nav_style = ' style="display:none;"';
		$('div#openclose').toggle(
			function() {
				$('div#promobanner').animate({ height: "365px" }, 500);
				$('span#close').fadeIn('medium');
				$('span#open').fadeOut('medium');
	
				setTimeout(function() {
					$cropbox.fadeIn(600);
					$('div#promobanner img.slideimg').fadeIn(300);
					$('a#backbtn').fadeIn(450);
					$('a#nextbtn').fadeIn(450);
					$('div#navi').fadeIn(450);
				}, 650);
			},
			function() {
				$cropbox.fadeOut(400);
				$('div#promobanner img.slideimg').fadeOut(400);
				$('a#backbtn').fadeOut(400);
				$('a#nextbtn').fadeOut(400);
				$('div#navi').fadeOut(400);
	
				setTimeout(function() {
					$('div#promobanner').animate({ height: "26px" }, 500);
					$('span#open').fadeIn('medium');
					$('span#close').fadeOut('medium');
				}, 410);
			}
		);
	}

    //----- Promo Banner Slider -----

    if (!$("#promobanner div.slide").hasClass("dberror")) {
        //Add Navi and Next/Prev Buttons for navigation
        $("#promobanner #crop").before('<div id="navi" '+nav_style+'></div>');
        $("#promobanner #crop").after('<a id="backbtn" '+nav_style+'>Back</a>');
        $("#promobanner #crop").after('<a id="nextbtn" '+nav_style+'>Next</a>');

        // initialize scrollable  
        $('div#crop').scrollable({
            size: 1,
            items: 'div#widearea',
            next: 'a#nextbtn',
            prev: 'a#backbtn',
            hoverClass: 'hover',
            speed: 500
        }).circular().autoscroll({
            interval: 5000
        }).navigator({
            navi: "#navi",
            activeClass: 'current'
        });
    }


    //--------------------- COLLAPSABLE ITEMS ---------------------------

    // Capture the original height of each collapsable element
    // Set the height of the collapsable element to 0px
    $('.collapsable').each(function() {
        $(this).attr({ name: $(this).css('height') });
        $(this).css({ height: '0px' });
    });

    // Add View More/Less Button	
    $('.collapsable').after('<a class="viewmore down" href="#">View More</a>');

    // The Button Functionality
    $("a.viewmore").toggle(

		function() {
		    // Properties for the View More/Less Button
		    $(this).removeClass('down');
		    $(this).addClass('up');
		    $(this).html('View Less');

		    // the collapsable div
		    var boxHeight = $(this).prev('.collapsable').attr('name');
		    $(this).prev('.collapsable').animate({ height: boxHeight }, 500);
		},

		function() {
		    // Properties for the View More/Less Button
		    $(this).removeClass('up');
		    $(this).addClass('down');
		    $(this).html('View More');

		    // the collapsable div
		    $(this).prev('.collapsable').animate({ height: "0px" }, 500);
		}
	);


    // Networks Flickr Photos
    $("#content div#flickr ul#photostream li a").fancybox({
        'zoomSpeedIn': 400,
        'zoomSpeedOut': 400,
        'overlayOpacity': 0.6,
        'overlayShow': true,
        'frameHeight': 500,
        'frameWidth': 800
    });

    //-------------------------------------------------------------------
    //---------------- EXTERNAL LINKS - Parent Window -------------------

    // To make links external add the external class to them - class="external"
    $("a.external").attr({ target: "_blank" });

    $(".external a").attr({ target: "_blank" });

    // ---------------------
    // Other Links that need to be accessed differently

    // Footer Links
    $("div#footer div#flickrbox a, div#footer div#twitter_container a").attr({ target: "_blank" });

    // Networks Page Links
    $("div#twitter a, div#flickr a").attr({ target: "_blank" });

    // Portfolio Page Links
    $("body#portfolio.web a.plaunch").attr({ target: "_blank" });

    //-------------------------------------------------------------------
    //-------------------------------------------------------------------	

});