jQuery.noConflict();

 (function($j) {

    Global = {

        settings: {
            var1: '.var1',
            var2: '.var2'
        },

        init: function() {

            var self = this;

            $j('body').addClass('js-ready');

            self.initAchievementList();
            self.initFormHint();
            self.initHomeScroller();
            self.initNewsItems();
            self.initBannerPopOut();
            self.styleSwitcher();
            self.emailFriends();

        },

        initAchievementList: function() {

            var self = this;

            var list = '.achievements dl';

            $j(list).each(

            function() {

                var achievementcopy = 'div.blurb';

                /* Grab the whole copy and extract the first sentence */
                var achievement = $j(achievementcopy, this);

                var fulltext = $j(achievement).html();
                var snippet = $j(achievement).text();

                var intro = snippet.substring(0, 60);
                intro = intro.substring(0, (intro.lastIndexOf(" ")));

                $j(achievementcopy, this).empty();
                $j(achievementcopy, this).append("<span class='js-blurb'>" + intro + "<a class='js-more' title='Read more'> ...Read more</a></span>");
                $j(achievementcopy, this).show();
                $j(achievementcopy, this).append("<div class='js-content'>" + fulltext + "<span><a class='js-less' title='Close'> ...Close</a></span></div>");

            });

            /* add show/hide events */
            $j('.js-more').click(function() {
                var p = $j(this).parent().parent();
                $j('.js-content', p).show();
                $j('.js-blurb', p).hide();
            });

            $j('.js-less').click(function() {
                var p = $j(this).parent().parent().parent();
                $j('.js-content', p).hide();
                $j('.js-blurb', p).show();
            });

            $j('.js-less').trigger('click');

            $j(list).show();

        },

        initFormHint: function() {

            if ($j("form").length) {

                $j("form input:text").each(function() {
                    var d = $j(this).attr("title");
                    if ($j(this).val() === "") $j(this).val(d);

                    $j(this).focus(function() {
                        if ($j(this).val() === d) $j(this).val("");
                    });
                    $j(this).blur(function() {
                        if ($j(this).val() === "") $j(this).val(d);
                    });
                });
            }
        },

        initHomeScroller: function() {

            if ($j("body.home .scrollable").length) {

                $j(".scrollable").scrollable({
                    circular: true
                }).navigator().autoscroll({
                    interval: 8000
                });

            }
        },

        initNewsItems: function() {

            if ($j("body.home .news-item").length) {

                var w = $j('.news-item').width();
                var h = $j('.news-item').height();

                $j(".news-item").each(function(i) {

                    var w = $j('.news-item').outerWidth();
                    w = (w + 7) * i;

                    $j(this).css({
                        'position': 'absolute',
                        'left': w
                    });

                });


                $j(".news-item").hover(function() {
                    $j(this).filter(':not(:animated)').css('z-index', '10').addClass('shadow').animate({
                        width: "174px",
                        height: "188px"
                    },
                    300);
                },
                function() {
                    $j(this).css('z-index', '1').removeClass('shadow').animate({
                        width: w,
                        height: h
                    },
                    100);
                });



            }
        },

        initBannerPopOut: function() {


            if ($j("body.home .pop-out").length) {

                $j('.pop-out').css({
                    "right": "-220px"
                });

                $j(".pop-out").mouseenter(function() {
                    $j(this).css({
                        backgroundPosition: '0 -2px'
                    }).filter(':not(:animated)').animate({
                        right: "0"
                    },
                    "slow");
                });
                $j(".pop-out").mouseleave(function() {
                    $j(this).filter(':not(:animated)').animate({
                        right: "-220"
                    },
                    "slow").css({
                        backgroundPosition: '0 100%'
                    });
                });

            }

        },

        styleSwitcher: function() {

            $j.stylesheetInit();

            $j('.styleswitch').bind(
            'click',
            function(e)
            {
                $j.stylesheetSwitch(this.getAttribute('rel'));
                return false;
            });
        },
	
		emailFriends: function() {
			
			function check_if_field_is_empty(field_id) {

				if( $j(field_id).val() == "" ) {
					$j(field_id).addClass('missing');
					return true;
				} else {
					$j(field_id).removeClass('missing');
					return false;
				}
					
			}
			
			if ($j("div.tell-friends a.send-email").length) {
				 $j('.wrapper').after('<div id="email-overlay-container"></div>');
				  $j('#email-overlay-container').load('/_lib/ajax/tell-a-friend-form.php', function() {
						
						  $j('div.tell-friends a.send-email').click(function() {
						  	var ta_overlay = $j("div#email-overlay-container").overlay({
										mask: { color: '#000', oadSpeed: 200, opacity: 0.5 },					
										closeOnClick: false,
										onClose: function() { 
						
											},
										load: true, // load it immediately after the construction
										oneInstance: true });
							
							ta_overlay.load();
							
						  	$j('div#email-overlay-container button').click(function() {
							
								var form_errors = false;
								
								form_errors = check_if_field_is_empty("div#email-overlay-container input#tellAFriendYourName");
								form_errors = check_if_field_is_empty("div#email-overlay-container input#tellAFriendYourEmail");
								form_errors = check_if_field_is_empty("div#email-overlay-container input#tellAFriendEmail_1");
							 
							 	if( !form_errors ) {
							 				 		
									var yourName = $j("div#email-overlay-container input#tellAFriendYourName").val();
									var yourAddress = $j("div#email-overlay-container input#tellAFriendYourEmail").val();
									var friendsAddress = [];
								
									friendsAddress[0] = $j("div#email-overlay-container input#tellAFriendEmail_1").val(); // Store first email address in the address
									
									if( $j("div#email-overlay-container input#tellAFriendEmail_2").val() ) { // Check if email has been added
										friendsAddress[friendsAddress.length] = $j("div#email-overlay-container input#tellAFriendEmail_2").val();
									}
									
									if( $j("div#email-overlay-container input#tellAFriendEmail_3").val() ) { // Check if email has been added
										friendsAddress[friendsAddress.length] = $j("div#email-overlay-container input#tellAFriendEmail_3").val();
									}
									
									if( $j("div#email-overlay-container input#tellAFriendEmail_4").val() ) { // Check if email has been added
										friendsAddress[friendsAddress.length] = $j("div#email-overlay-container input#tellAFriendEmail_4").val();
									}
								
									if( $j("div#email-overlay-container input#tellAFriendEmail_5").val() ) { // Check if email has been added
										friendsAddress[friendsAddress.length] = $j("div#email-overlay-container input#tellAFriendEmail_5").val();
									}

						 		
							 		$j.getJSON('/_lib/ajax/send-email-to-friend.php', 
										{	
											action: 'send',
											your_name: yourName,
											your_email: yourAddress,
											friends_email: friendsAddress
										}, 
										function(data) { // Get data from JSON file
											
											
											if( data.complete == true ) { // If data complete
												$j('#email-overlay-container form').html("<p>" + data.message + "</p>");
											} else {
												alert(data.message);
											}
																				
									});
						 		
						 	} else {
						 		alert('Please fill in your name, your email address and at least one friends email address.');
						 	}
						 
						 	return false;
						 	
						 });
						  	return false;
						  
						  });
						  
						 
				  });
			}
			
		
		}

        /*

        y: function() {
            if ($j(self.settings.var1.length) {
             }
        }
*/

    };

    $j().ready(function() {

        Global.init();

    });

})(jQuery);

