/* 
 *
 *	Main JS logic for halogen.no
 *	@author Sigrid Bygdås
 *
 */

var halo = {
    
        ID: 1, 
        name: "halogen", 

        init: function () {                
                this.styleXForms();                
                this.addKeepDefaults();                
                this.addEventHandlers();                
               // this.preloadImages();
                this.animationCheck();
        }, 

        animationCheck: function() {
            // Usikker på om dette virker!
            var flashSupport = false;
            try {
              var dmy = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
              if (dmy) {
                  flashSupport = true;
                 // console.log("dmy ok");
              }
            }
            catch(e){
                //console.log("exception");
              if(Navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) {
                  flashSupport = true;
                 // console.log("support ok");
              }
            }
            if (!flashSupport) {
               // console.log("no support");
                $("#animasjon").hide();
            }
        },

        addEventHandlers: function() {            

            $('a.Skip').keydown(function(e) {
                if (e.which == 13) {                   
                   e.preventDefault();
                   $('#MainContent a:first').focus();
                }
            });

            $('.NewsLetterLink a').live("click", function(e) {
                e.preventDefault();
                if ($('.LightBox').is(':visible')) {
                    $('.LightBox').hide('fast', function() {
                        // Animation complete.
                      });                   
                }
                else {
                    $('.LightBox').show('fast', function() {
                        // Animation complete.
                      });                    
                }
            });
        }, 

        styleXForms: function() {
            var form = $(".XFormSchema");
            form.find('input[type=text]').each( function() {
                var label = $(this).siblings('label');
                var defaultValue = label.text();
                $(this).val(defaultValue);
                $(this).addClass("KeepDefault");
                label.addClass("Hide");
            });
            form.find('textarea').each( function() {                
                var label = $(this).siblings('label');
                var defaultValue = label.text();
                $(this).val(defaultValue);
                $(this).addClass("KeepDefault");
                label.addClass("Hide");
            });            
        },

        addKeepDefaults: function() {
            $(".KeepDefault").each(function() {
                var defaultValue = $(this).val();
                $(this).focus(function() {       
                    if (this.value == defaultValue) {
                        $(this).css("color", "#222222");
                        $(this).val('');
                    }
                });
                $(this).blur(function() {
                    if (this.value == '') {
                        $(this).css("color", "#707070");
                        $(this).val(defaultValue);
                    }
                })
            })

        },

        preloadImages: function() {
//            var pngs = ['arrow-blue', 'arrow-double', 'arrow-double-blue', 'arrow-grey', 'btn_background', 'facebook',
//                        'gallery_indicator', 'gallery_indicator_active', 'hovermark', 'linkendin', 'logo_new',
//                        'mail_closedialog', 'mainmenu_background', 'newsletter', 'pil_hoyre', 'pil_venstre', 'square', 'twitter'];
//            $.preload( pngs, {
//                base: 'img/',
//                ext: '.png'
//            });
//
//            var jpgs = ['icon_mail', 'icon_mail_hover', 'icon_phone', 'icon_twitter', 'icon_twitter_hover', 'separator' ];
//            $.preload( jpgs, {
//                base: 'img/',
//                ext: '.jpg'
//            });
        }
};

$(document).ready(function(){
	halo.init();
});

