﻿
App = function(){

    /* Private */
        
    /* Properties */
    
    var cmp = {};
    
    
    
    
    
    /* Defaults */
    
    TVI.debug = true;
    
    
    
    
    
    /* Methods */
    
    var init = function(){

		// Master 100% height
			if($('#master').outerHeight() < $(document).height() ){
				$('#master').height($(document).height());
			}

		// Top nav dropdowns
			$('#topNav .menu LI').mouseenter( function(){
		
				$(this).find('A:first').addClass('selected').parent().find('UL').fadeIn('fast');

		
			});
			$('#topNav .menu LI:not(LI LI)').mouseleave( function(){
		
				$(this).find('A:first').removeClass('selected').parent().find('UL').fadeOut('fast');
		
			});

		// Left menu slide up/down
			$('#leftNav LI UL').parent().find('A:first').click(function(){
			
				// If sub nav not visible slide down, else slide up
				if( $(this).parent().find('UL:first').is(':hidden') ){
					$(this).addClass('selected').parent().find('UL:first').slideDown();
				} else {
					$(this).parent().find('.selected').next('UL').slideUp(function(){
						$(this).parent().find('.selected').removeClass('selected');
					});
				}

				return false;			
			});

		// GALLERY
    
			// Append extra <a> tags for lightbox
			$('.gallery .thumbnail').each(function(){
        
				var largeImage = $(this).attr('data-largeImage');
            
				if( largeImage != $('.gallery .mainImgContainer A:first').attr('href') ){
					 $('<a class="lightbox" href="'+ largeImage +'"></a>').appendTo('.gallery .mainImgContainer');
				}
            
			});

			// Add lightbox
			$('A.lightbox').lightBox();	    

			// Functionality
			$('.gallery .thumbnail').click(function() {
	        
				// Change main image
				$('.gallery .mainImgContainer IMG').attr('src', $(this).attr('data-mainImage'));
	        
				// Move correct href to top of stack
				$('.gallery .mainImgContainer A[href*="' + $(this).attr('data-largeImage') + '"]').attr('href', $('.gallery .mainImgContainer A:first').attr('href'));
	        
				$('.gallery .mainImgContainer A:first').attr('href', $(this).attr('data-largeImage'));
	        
				return false;

			});

		// FAQ

			// Open/close correct FAQ
			$('.faq .question').click( function(){

				// If open, close and remove 'open' class, If not open, open and add 'open' class
				if( $(this).parent().hasClass('open') ){
	        		$(this).parent().removeClass('open').find('.answer').slideUp();
				} else {
					// Close all others
					$('.faq .answer').slideUp();
					$('.faq').removeClass('open');

					$(this).parent().addClass('open').find('.answer').slideDown();
				}

				return false;
			});
			





    };
    
    
    
    
    
    /* Public */
    
    TVI.apply(cmp, {

        setFocusBlur: function (inputs) {

            // Function to set focus-blur on textboxes
            inputs.each(function () {

                // For password boxes remove the background on focus and restore it on blur if its empty
                if ($(this).attr('type') === 'password') {

                    $(this).focus(function () {
                        if ($(this).val() === '') {
                            $(this).data('background', $(this).css('background-image'));
                            $(this).css('background-image', $(this).data('background').replace("-password", ""));
                        }
                    });
                    $(this).blur(function () {
                        if ($(this).val() === '') {
                            $(this).css('background-image', $(this).data('background'));
                        }
                    });
                }
                else {
                    // For normal
                    $(this).data('original', $(this).val());

                    $(this).focus(function () {
                        if ($(this).val() === $(this).data('original')) {
                            $(this).val('');
                        }
                    });

                    $(this).blur(function () {
                        if ($(this).val() === '') {
                            $(this).val($(this).data('original'));
                        }
                    });
                }
            });

        }

    
    });
    
    
    TVI.ready(init);    
    
    
    return cmp;


} ();

function pageLoad() {
    App.setFocusBlur($('.searchForm input, .newsletterForm input'));
    setForm('.searchForm');
    setForm('#newsletterForm');
    setForm('.narrowForm');

}

function resetInputs() {
    $('input[type=text]').css("background-color", "#f1f6ff");
    $('input[type=password]').css("background-color", "#f1f6ff");
}

function clear_checkboxes(myID) {
    var frm = document.forms[0];
    for (i = 0; i < frm.length; i++) {
        if (frm.elements[i].id != myID) {
            frm.elements[i].checked = false;
            $('input').next().removeClass('selected');
        }
    }
}

function setForm(formname) {

    //clear events
    $(formname).unbind();

    $(formname).live('keypress', function (e) {


        //check return key is pressed
        if (returnKeyPressed(e)) {
            console.log("return key");
            //check input is not textarea
            if (e.target.type != 'textarea') {

                //get postback arguments from button
                var postback = $(this).find("a:first").attr("href");
                if (postback != undefined) {

                    postback = postback.replace("javascript:__doPostBack('", "");
                    postback = postback.replace("','')", "");

                    //call postback
                    __doPostBack(postback, '');

                }

            }

        }

    });

}

function returnKeyPressed(e) {

    //check if event has been passed in, otherwise set it to the window event
    if (!e) e = event;

    //check return key has been pressed
    if ((e.which == 13) || (e.keyCode == 13)) {

        //check input is not textarea
        if (e.target.type != 'textarea') {

            //stop postback
            e.returnValue = false;
            if (!$.browser.mozilla) {
                event.returnValue = false;
            }

        }

        return true;

    } else {
        return false;
    }

}
