$(function() {

    // Ask the user for confirmation
	$("a.confirmation").click(function() {
		return confirm('Are you sure you want to: "'+ $(this).attr('title') +'"?');
	});

    // Controls active textfields and textareas
    $(".textField, .textField_large, .textArea, .textArea_large").focus(function() {
        $(this).addClass("formFocus");
    });

    $(".textField, .textField_large, .textArea, .textArea_large").blur(function() {
        $(this).removeClass("formFocus");
    });

    // Sucky MSIE requires extra click on formselects
    if( !$.browser.msie ) {
        $(".formSelect_large, .formSelect_medium, .formSelect_small, .formSelect").focus(function() {
            $(this).addClass("formFocus");
        });

        $(".formSelect_large, .formSelect_medium, .formSelect_small, .formSelect").blur(function() {
            $(this).removeClass("formFocus");
        });
    }

    // close button flash message
    $('div ul li span.dismiss').click(function() {
        $(this).parent().parent().parent().slideUp();
    });

    $('a.verisign').click(function() {
        var win = window.open("https://seal.verisign.com/splash?form_file=fdf/splash.fdf&dn=WWW.THEMEPARTNER.COM&lang=en", "win", "toolbar=0,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=560,height=480");
        win.focus();
        return false;
    });

    // Back to top
    $('a.backtotop').removeAttr("href").click(function(){
        // Opera supports both. So, prevent it from scrolling twice.
        var backToTop = ($.browser.opera) ? 'html' : 'html, body';
        $(backToTop).animate({scrollTop: 0}, 'slow');
    });
    
    // Template preview switcher
    $('ul.presets li').click(function() {
        var imageName = $(this).attr('title');
        var templateName = $(this).parent().parent().parent().attr('id');
        
        $(this).siblings().removeClass('active');
        $(this).parent().parent().find('a.preview-image img').attr('src', baseUrl() + '/images/templates/' + templateName + '/' + imageName + '.jpg');
        $(this).addClass('active');
    });
    
    // when focussing on text field, remove text when it is the same as title attribute.
    $('.hideTextOnFocus').focus(function() {
        if($(this).attr('title') == $(this).val()) {
            $(this).val('');
        }
    });
    // when losing focus, fill in text as in title attribute when value is empty
    $('.hideTextOnFocus').blur(function() {
        if($(this).val() == '') {
            $(this).val($(this).attr('title'));
        }
    });
    
    // tooltips
    $('.showTooltip').qtip({
        position: {
            my: 'left center', // Use the corner...
            at: 'right center' // ...and opposite corner
        }
    });
    
    // hide info message
    $('span#informationClose').click(function() {
        $(this).parent().slideUp();
        // the second class name should be the cookie name
        var classList = $(this).attr('class').split(/\s+/);
        $.cookie(classList[1], 1, { path: '/', expires: 14 });
    });
});


// Use load() instead of ready() because we need image sizes
jQuery(window).load(function() {

    // Loop through all tagged images
    jQuery('.captImg img').each(function() {
        // Wrap image in div and add an extra div inside that wrapper
        jQuery(this).wrap('<span class="caption"></span>').after('<span class="caption-inner">' + jQuery(this).attr('alt') + '</span>');

        // Set width of the captions and loop through the divs in it
        jQuery('.caption').children('.caption-inner').each(function() {
            // Set CSS width of caption based on size of the image
            jQuery(this).css({width: jQuery(this).siblings('img').width() - 10 + 'px', opacity: 0, display: 'block'});
        });
    });

    // Show captions on hover
    jQuery('.caption').hover(
        function() {jQuery(this).children('.caption-inner').stop().fadeTo(500, 0.7);},
        function() {jQuery(this).children('.caption-inner').stop().fadeOut(500);}
    );

});
