/**
 * Globally used javascript
 */

/**
 * Settings
 */
var externalLinksInNewWindow    = true; // Show external links in a new window (Emulate target="_blank")
var externalLinksConfirmation   = true; // Ask user for confirmation of new window?

// No conflict mode
//$.noConflict();

// Indicator that javascript is supported by client
jQuery('html').addClass('js');

jQuery(document).ready(function($) {

    // PNG-Transparency for Windows IE 5.5 & 6
    $(document).pngFix();

    // Pretty photo links
    $("a[rel^='prettyPhoto']").prettyPhoto({
        modal: true,
        title: false,
        opacity: 0.20,
        theme: 'facebook'
    });

    // Form defaults toggler

    var defaultTexts = new Array;
    $('input:text, textarea').each(function(e) {

        if($(this).val() != '') {

            defaultTexts[$(this).attr('id')] = $(this).val();

            $(this).focus(function() {

                if($(this).val() == defaultTexts[$(this).attr('id')]) {
                    $(this).val('');
                }

            });

        }

        $(this).blur(function() {

            if($(this).hasClass('required') && $(this).val() == '') {
                $(this).val(defaultTexts[$(this).attr('id')]);
            }

        });

    });

    // Newsletter hide after signup (by cookie) NOTE: This may well want to be handled via a php cookie.
    var signedUpHtml = '\
    <h3>Newsletter</h3>\
    <p>\
        <strong>You are currently subscribed to our news letter.</strong>\
        <br />\
        To unsubscribe, please follow the instructions in the newsletter.\
    </p>';

    if($.cookie('mylor_newsletter_signed_up') == 1) {
        $('form#form-newsletter').hide();
        $('#footer-column-3').html(signedUpHtml);
    }

    // XHTML Strict new windows
    // NOTE: For windows to open in new tab/window add rel="external" to the link
    if(externalLinksInNewWindow) {
        $('a[rel*=external]').live('click', function(e){
            e.preventDefault();
            if(externalLinksConfirmation) {

                //if($(this).attr('title') != '') {
                //    msg = $(this).attr('title');
                //}
                //else {
                    msg = $(this).attr('href');
                //}

                if(confirm(msg + "\nwill open in a new window/tab.\n\nDo you want to continue?")) {
                    newWindow = window.open($(this).attr('href'), 'mylorWindow');
                    newWindow.focus();
                }
            }
            else {
                newWindow = window.open($(this).attr('href'), 'mylorWindow');
                newWindow.focus();
            }
        });
    }

    // Slideshow
    $(".scrollable").scrollable({circular: true});

    // Gallery scroller
    $("div.gallery-thumbs").scrollable({circular: true});

    $("div.gallery-thumbs div.items a").click(function(e) {

        // Prevent default
        e.preventDefault();

        // see if same thumb is being clicked
        if($(this).hasClass("active")) {
            return;
        }

        // calclulate large image's URL based on the thumbnail URL (flickr specific)
        var url = $(this).attr("href");

        // get handle to element that wraps the image and make it semi-transparent
        var wrap = $("#gallery-viewer").hide().fadeTo("1000", 0);

        // the large image
        var img = new Image();

        // call this function after it's loaded
        img.onload = function() {

            // make wrapper fully visible
            wrap.fadeTo(600, 1);

            // change the image
            wrap.find("img").attr("src", url).reflect({height:18, opacity:0.6});

        };

        // begin loading the image
        img.src = url;

        // activate item
        $("div.gallery-thumbs div.items a").removeClass("active");
        $(this).addClass("active");

    // when page loads simulate a "click" on the first image
    }).filter(":first").click();

    // Autologin confirm
    $('#aside-login-autologin, #double-width-login-autologin').change(function() {
        if($(this).is(':checked')) {
            if(!confirm("Using autologin will save your login information for 30 Days.\nYour login information will be stored encrypted in a cookie on you machine, however if you are using a shared machine, this is not recommended.\n\nDo you want to enable autologin?")) {
                $(this).attr('checked', false);
            }
        }
    });

    // Hide / show content
    $('div.accordion div.showhide').hide();

    $('.reveal').click(function(evnt) {
        evnt.preventDefault();
        $(this).toggleClass('minus');
        $(this).closest('div.accordion').find('div.showhide').slideToggle();
    });

    // Reflection
    $(".reflection").reflect({height:18, opacity:0.6});

    // Set all features to the same height
    var feature_max_height = 0;
    $('.sameheight').each(function(){
        var myheight = $(this).height();
        if (myheight > feature_max_height) {
            feature_max_height = myheight;
        }
    });
    $('.sameheight').height(feature_max_height);

});

