$(document).ready(function() {

    // navbar dropdown menu
    $('ul.sf-menu').superfish({
        delay: 0,
        speed: 50
	});

    // Detetes default value in input fielsd but not user inserted text
    var default_values = {};

    $('input#email').each(function(i) {
      var index = $(this).attr('name');
      var value = $(this).val();
      default_values[index] = value;
    });

    $('input#email').focus(function() {
      if ($(this).val() === default_values[$(this).attr('name')]) {
        $(this).val('');
      };
    });

    // jQuery Tools

    // Tooltip
    $("#codes-form :input").tooltip({
        offset: [-10, 0],
        effect: "fade",
        opacity: 0.7
    });

    $("#Products .download-btn, #Products .buy-btn").tooltip({
        position: "bottom center",
        effect: "fade",
        opacity: 0.7
    })

    $(".latest_tweets h5 a").tooltip({
        position: "top center",
        effect: "fade",
        opacity: 0.7
    })

    // Screenshots tabs
    $("#screenshots li").css({"display": "none", "position": "absolute", "top": "0", "left": "0"} );
    $("#screenshots_thumbs").tabs("#screenshots > li", {
        effect: 'fade',
        fadeInSpeed: 400,
        fadeOutSpeed: 400
    });

    // Fancybox
    $(".screenshot").fancybox({
            "overlayColor": "#ffffff",
            "titlePosition": "inside"
            });

    // Tabs
    $('.tab_header').css({visibility: 'hidden', height: 0, width: 0});

    $("ul.tabs").tabs("div.panes > div", {
        effect: 'fade',
        fadeInSpeed: 400,
        fadeOutSpeed: 400
    }).click(function(){
        $.scrollTo($(this), 500, {offset:-50});
        }
    );

    $(".submit-button").overlay({
        // some mask tweaks suitable for modal dialogs
        mask: {
            color: '#ffffff',
            loadSpeed: 200,
            opacity: 0.8
        },
        closeOnClick: false,
        speed: 'slow',
        top: 'center',
        onLoad: function() {
            $.ajaxSetup({
                cache:false
                });
            $("#result").html('<img src="/images/ajax-loader.gif"/>');
            $(".close").focus();
            $.post(
                '/store/ajax_lostcode.php',
                {
                    email: $("#email").val()
                },
                function(responseText){
                    $("#result").html(responseText);
                },
                "html"
            );
            return false;
        }
    });

    // Cycle on quotes
    $('#product_quotes li').css('opacity', '0');
    $('#product_quotes').cycle({
        timeout: 6000,  // milliseconds between slide transitions (0 to disable auto advance)
        speed:   1000  // speed of the transition (any valid fx speed value)
        });
    // Cycle on home - blog teaser
    $('#blog_teaser_list').cycle({
            fx: 'custom', 
            sync: 0, 
            cssBefore: {  
                top:  0, 
                left: 700, 
                display: 'block' 
            }, 
            animIn:  { 
                left: 0,
            }, 
            animOut: {  
                top: 36 
            }, 
            pause: 1
        });

    $("#template #navbar li:contains('Store') a").addClass('current');

    //var currentClass = $('#footerContainer').attr('class').split(' ');
    //$('#style_switcher ul li a').click(function(){
            //$('#footerContainer').removeClass().addClass(currentClass[0] + ' ' + $(this).text());
            //$(this).parent().parent().find('.current').removeClass();
            //$(this).addClass('current');
            //return false;
        //});

    //$('#style_switcher ul li a').click(function(){
            //$('#footerContainer').toggleClass($(this).text());
            //$(this).toggleClass('current');
            //return false;
        //});

    $(".back_to_top a").click(function(){
        $.scrollTo("body",1800);
        return false;
        }
    );

    // Changelog accordion
    //
    //$('.sectionheading').hide();
    //$('.headingtext_normal, .headingtext_highlight').addClass('closed').append(' <span class="status">+</span>');
    //
    // on click, if the heading has a class closed, remove the class, add the
    // class open and slidedown the first next ul.sectionheading
    // if the heading has the class open, remove the class, add the class
    // closed and slideup the first next ul.sectionheading
    //
    //$('.headingtext_normal, .headingtext_highlight').click(function(){
        //if($(this).hasClass('closed')){
            //$(this).removeClass('closed').addClass('open').find('span').text('-').parent().next('ul.sectionheading').slideDown('slow');
        //}else{
            //$(this).removeClass('open').addClass('closed').find('span').text('+').parent().next('ul.sectionheading').slideUp('slow');
        //}
    //});

    var $last_ul = $("div.panes > div:has('#Changelog')").children(".version").eq(3).index() - 1;
    $("div.panes > div:has('#Changelog')").children().eq($last_ul).nextAll().wrapAll('<div id="older_changelogs">').parent().hide();
    $('#older_changelogs').before('<p id="show_older_changelogs" class="closed"><a href="" title="Show older changelogs">Show older changelogs</a></p>');
    $('#show_older_changelogs').click(function () {
        $('#older_changelogs').slideToggle("slow", function () {
            if($('#show_older_changelogs').hasClass('closed')) {
                $('#show_older_changelogs').removeClass('closed').addClass('open');
                $('#show_older_changelogs a').text('Hide older changelogs').attr("title","Hide older changelogs");
            }else{
                $('#show_older_changelogs').removeClass('open').addClass('closed');
                $('#show_older_changelogs a').text('Show older changelogs').attr("title","Show older changelogs");
            };
        });
        return false;
    });

    $('#template .sidebar .heading:eq(0)').addClass("account_header");
    $('#template .sidebar .heading:eq(1)').addClass("receipt");
    $('#template .instruction h2:eq(0)').addClass("unlock");
    $('#template .instruction h2:eq(1)').addClass("balance");

}); //end of $(document).ready(function(){});

