// Global Javascript

$(document).ready(function() {

    // OVERLAY or MODAL WINDOW

//    $('#trigger-mies1').attr('rel', '#mies1');
//    $('#trigger-mies2').attr('rel', '#mies2');

    $('.trigger[href]').each(function() {
        $(this).attr('rel', $(this).attr('href'));
    });

    // Modal window
    var triggers = $(".trigger[rel]").overlay({
        // some mask tweaks suitable for modal dialogs
        mask: {
            color: '#fff',
            loadSpeed: 200,
            opacity: 0.9
        },
        closeOnClick: true
    });
})


// Tabs
$(function() {
    // Auto grow text area
    $('textarea').autogrow();

    var tabContainers = $('div.tabs > div.tab');
    tabContainers.hide().filter(':first').show().addClass('on');
    $('div.tabs ul.tabNav a').click(function() {
        // What's just been clicked?
        if ($(this).hasClass('on')) {
            // Do nothing
        } else {
            // Work out which tab we want
            $('div.tabs ul.tabNav a.selected').removeClass('selected').removeClass('on');
            var selected = $(this);
            var clicked = $(this).attr('class');
            $('.tabNav .on').removeClass('on');
            $(this).addClass('on').addClass('selected');
            var targetHash = this.hash;
            tabContainers.filter('.on').removeClass('on').fadeOut(200, function() {
                tabContainers.filter(targetHash).fadeIn(500).addClass('on');
            });
        }
        return false;
    }).filter(':first').click();
    // Opens links to external sites in a new window
    $('a[href^="http://"]').attr({ target: "_blank", title: "Opens in a new window" });
    // Cufon
    Cufon.replace('h1, h2, h4, h5, h6, .cufon');
    // Add a little bit of magic to the on-focus / on-blur of each .input-submit
    $(".input-text, .input-textarea").focus(function() {
        var input = $(this);
        input.toggleClass("focus");
    });
    $(".input-text, .input-textarea").blur(function() {
        var input = $(this);
        input.toggleClass("focus");
    });
    $(".input-submit").click(function() {
        $(".input-text, .input-textarea").each(function(index, value) {
            if ($(this).val() == "") {
                $(this).addClass("error");
            }
            else {
                $(this).removeClass("error");
            }
        });
        return false;
    });
    $(".screen").css("opacity", .7);
    $(".job").hover(function() {
        $(this).find(".screen").animate({ opacity: 1 }, 200)
    },
  function() {
      $(this).find(".screen").animate({ opacity: .7 }, 200)
  });
    // Contact Page Maps
    $(".map").hover(function() {
        $(this).find("img").stop().animate({ opacity: 0 }, 350)
    },
  function() {
      $(this).find("img").stop().animate({ opacity: 1 }, 200)
  });
});

//INTRO BANNER
function slideSwitch() {
    var $active = $('#intro div.active');
    if ($active.length == 0) $active = $('#intro div:last');
    var $next = $active.next().length ? $active.next()
        : $('#intro div:first');

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval("slideSwitch()", 5000);
});

//FADER FOR BUTTONS
$(function() {
    $(".btn").css("opacity", 1);
    $(".btn").hover(function() {
        $(this).stop().animate({ opacity: .9 }, 200);
    }, function() {
        $(this).stop().animate({ opacity: 1 }, 200);
    });
});

//FADER FOR OUR WORK IMAGES
$(function() {
    $(".work img").css("opacity", .8);
    $(".work").hover(function() {
        $(this).find('img').stop().animate({ opacity: 1 }, 200);
    }, function() {
        $(this).find('img').stop().animate({ opacity: .8 }, 200);
    });
});

//SHOW HIDE WORK EXAMPLES
$(function() {
    //INITIAL WORK SCREEN
    $("div.work-two").css("display", "none");
    $('.work-next1').click(function() {
        $("div.work-two").css("display", "block");
        $("div.work-one").css("display", "none");
    });
    //SECOND WORK SCREEN
    $('.work-next2').click(function() {
        $("div.work-one").css("display", "block");
        $("div.work-two").css("display", "none");
    });

    //LOGIN AREA
    $('.user-area').css("display", "none"); //HIDE LOGIN AREA ON LOAD
    $('.jhide').css("display", "none"); //HIDE ELEMENTS FOR USERS WITH JS ENABLED
    $('.btn-login').click(function() {
        $('.btn-login').addClass('login-off');
        $('.user-area').slideToggle('fast');
        $('.login-off').toggleClass("login-off").toggleClass('login-on');
    });
});

//WATERMARK
$(function() {
    // USERNAME WATERMARK
    $(".wm-username").Watermark("Username");
    $(".wm-password").Watermark("Password");

    // SWITCH PASSWORD FIELD
    $('.wm-password').blur(function() {
        if ($(this).val() === "") {
            $(this).hide();
            $('#password-watermark').show();
        }
    });
    $('#password-watermark').focus(function() {
        $(this).hide();
        $('.wm-password').show().focus();
    });
    if ($('.wm-password').val() === "") {
        $('.wm-password').hide();
        $('#password-watermark').show();
    }
});
