(jQuery)(function($){ 
	var SEL = (function () {	
	
		return {
			init : function (callback) {
				if (callback === undefined) callback = function () {};
				$('html').removeClass('no-js').addClass('has-js');
				log('SEL init')
				
			    // Equal Heights
			    var highest = 0;
			    $('div.equal').each(function() {
			        if ($(this).height() > highest) {
			            highest = $(this).height();
			        }
			    });
			    $('div.equal').height(highest + 15);
			
			    // SlideShow
			    if ($('ul.slideshow').length > 0) {
			        $('ul.slideshow').each(function() {
			            $(this).innerfade({
			                speed: 'slow',
			                timeout: 2000,
			                type: 'sequence',
			                containerheight: 250
			            });
			        });
			    }
				this.HexMap.init()
				this.Flash.init();
				this.Forms.init();
				this.Youtube.init();	
				this.Twitter.init();	
				
				callback();		
			}		
		}		
	})();
	
	
	SEL.HexMap = (function () {
		
		var modal = function (content) {
			$('div#hexmapbox').append('<div id="modal" style="margin-left: -202px;">' +
		        '<a href="#" class="close">close</a>' +
		        '<div class="content">' + 
		            content +
		        '</div>' +
		        '<div class="footer">&#160;</div>' +
		    '</div>');            
		    $('div#modal').css('marginTop',- $('div#modal').height() / 2);
		};
    	
		return {
			init : function (callback) {
				if (callback === undefined) callback = function () {};
				log('HexMap init');
				
				// HexMap				
			    if($('div#hexmapbox').length > 0) {
			        $('div#hexmapbox').append('<a id="hoverbox" href="#">hover</a>')
			        var ext = '.png';
			            if ($.browser.msie && $.browser.version.substr(0,1)<7) { ext = '.gif'; }
			        var map;
			        var $hoverbox = $('a#hoverbox');
			        $hoverbox.hide();
			        var boxX = 0;
			        var boxY = 0;
			        var bgImg = '';
			        $('.modal').hide();
			        $('body').mouseover(function(e) {
			            if ($(e.target).is(':not(map#hexmap area, a#hoverbox)')) {
			             $hoverbox.hide();
			            }
			        }); 
			        $hoverbox.mouseover(function(){
			            $hoverbox.attr('style','left: '+boxX+ 'px; top: ' + boxY + 'px;background-image: url('+ bgImg +');')
			        });
			        $hoverbox.click(function(){    
			            $('div#modal').remove();
			           	modal($($hoverbox.attr('href')).html());
			            return false;
			        })
			        $('div#modal a.close').live('click',function(){                
			            $('div#modal').remove();
			            return false;
			        });
			        $(document).keyup(function(e){
			            if (e.keyCode == 27) {
			               $('div#modal').remove();
			            }
			        });
			
			         $('map#hexmap area').mouseover(function(e){
			             var coords = $(this).attr('rel').split(',');
			             boxX = coords[0];
			             boxY = coords[1];
			             bgImg = pathprefix + 'images/hex-' + $(this).attr('href').replace('#','') + ext;
			             $hoverbox
			                 .show()
			                 .attr('style','left: '+boxX+ 'px; top: ' + boxY+'px;background-image: url('+ bgImg +');')
			                 .attr('href',$(this).attr('href'))
			                 .attr('title',$(this).attr('title'));
			             $hoverbox.show();    
			         }).mouseout(function(){
			             $hoverbox.hide();
			         });
			     }
				
			}
		}
	})();
	
	SEL.Flash = (function () {
				
		return {
			init : function (callback) {
				if (callback === undefined) callback = function () {};
				log('Flash init');
			    // FlashVideo
			    if ($('#video-player').length > 0) {
			        var flashvars = {};
			        flashvars.videoSource = $('.media a.src').attr('rel');
			
			        if (window.location.search.indexOf('play=true') > 0) {
			            flashvars.setAutoPlay = 'true';
			        } else {
			            flashvars.setAutoPlay = 'false';
			        }
			
			        var params = {};
			        params.scale = "noscale";
			        params.quality = "high";
			        params.wmode = "transparent";
			        params.allowfullscreen = "true";
			        params.allowScriptAccess = "sameDomain";
			        params.align = "middle";
			        params.bgcolor = "#000000";
			
			        var attributes = {};
			        attributes.id = "video-player";
			        attributes.name = "video-player";
			
			        swfobject.embedSWF("flash/minimal-white-01.swf", "video-player", "587", "320", "9.0.45", "flash/expressInstall.swf", flashvars, params, attributes);
			    }
			
			    // FlashMap
			    if ($('#flash-map').length > 0) {
			        var flashvars = {};
			        flashvars.BoroughPage = '';
			
			        var params = {};
			        params.scale = "noscale";
			        params.quality = "high";
			        params.wmode = "transparent";
			        params.allowfullscreen = "true";
			        params.allowScriptAccess = "sameDomain";
			        params.align = "middle";
			        params.bgcolor = "#000000";
			
			        var attributes = {};
			        attributes.id = "video-player";
			        attributes.name = "video-player";
			
			        swfobject.embedSWF("flash/sel-map.swf", "flash-map", "582", "466", "9.0.45", "flash/expressInstall.swf", flashvars, params, attributes);
			    }
			    
			    
     			callback();
			}
		}
	})();
     
	SEL.Forms = (function () {
		var inputBackgrounds = function ($t, imgName) {
			if($t.attr('value').length > 0) {
			  	$t.css('background-image','none');      
			} else if(imgName.length === 0) {
			} else {
			  	$t.css('background-image','url('+pathprefix+'images/'+ imgName + ')');
			}
		}	
		return {
			init : function (callback) {
				if (callback === undefined) callback = function () {};
				log('Forms init');
				
				 // Form backgrounds
			    if ($('#search-box').length) {
			        $('#search-box fieldset input').addClass('search').removeClass('textbox');
			        if ($('input.username').length > 0) {
			            inputBackgrounds($('input.username'), 'bgUsername.gif');
			            inputBackgrounds($('input.password'), 'bgPassword.gif');
			        }
			        inputBackgrounds($('input.search'), 'bgSearch.gif');
			    }			
			    $('input.username').blur(function() {
			        $(this).removeClass('focus');
			        inputBackgrounds($(this), 'bgUsername.gif');
			    });
			    $('input.password').blur(function() {
			        $(this).removeClass('focus');
			        inputBackgrounds($(this), 'bgPassword.gif');
			    });
			    $('input.search').blur(function() {
			        $(this).removeClass('focus');
			        inputBackgrounds($(this), 'bgSearch.gif');
			    });
			    /*IE :focus fix*/
			    $('input.username').focus(function() {
			        $(this).addClass('focus');
			    });
			    $('input.password').focus(function() {
			        $(this).addClass('focus');
			    });
			    $('input.search').focus(function() {
			        $(this).addClass('focus');
			    });
			
			    // Flat forms
			    if ($('#add-user').length) {
			        if ($('#add-user div.alert').css('display') == 'none') {
			            $('#add-user div.form.flat input[type="text"]').labelswitch();
			        }
			    }
			    if ($('#search-users').length) {
			        if (!$('#search-users div.alert').length) {
			            $('#search-users div.form.flat input[type="text"]').labelswitch();
			        }
			    }			
			    // Form input return handling
			    $('input.search').keypress(function(e) {
			        if (e.which == 13) {
			            $('input#SearchInput_btnSearch').click();
			            return false;
			        }
			    });
			    $('input.password').keypress(function(e) {
			        if (e.which == 13) {
			            $('input#ucLogIn_btnLogIn').click();
			            return false;
			        }
			    });
			    $('input#txtSearchTerms, select#ddlBorough').keypress(function(e) {
			        if (e.which == 13) {
			            $('input#btnSearchDirectory').click();
			            return false;
			        }
			    });
			
			    // Scrolling 
			    if (window.location.pathname == "/directory.aspx" && window.location.search.length > 0) {
			        $('div#results').scrollTo();
			    }
			    if (window.location.pathname == "/organisation.aspx" && window.location.hash.length > 0) {
			    	var $a = $('a[name=' + window.location.hash.replace('#', '') + ']');
			        $a.parent().addClass('emphasis');
			        $a.scrollTo();
			    }
				callback();
			}
		}
	})();
	
	SEL.Youtube = (function () {	
		return {
			init : function (callback) {
				if (callback === undefined) callback = function () {};
				log('Youtube init');
				if ($.jTube !== undefined) {
					var $youtube = $('div#youtube');
					if ($youtube.exists()) {
						$.jTube({
							user: 'SELcic',
							userType: 'favorites',
							limit: 7,
							page: 1,
							success: function(videos) {
								var html = '';
								for (var i = 0; i < videos.length; i++) {
									html += '<li><a href="' + videos[i].link + '" target="_blank"><img src="' + videos[i].thumbnail + '" width="190" alt="' + videos[i].title + '" title="' + videos[i].title + '"></a><span class="time">' + videos[i].length + '</span></li>';
								}
								$youtube.append('<ul>' + html + '</ul>')
							},
							error: function(error) {
								log(error);
							}
						});
					}
				}
				callback();
			}		
		}
	})();
	
	SEL.Twitter = (function () {	
		return {
			init : function (callback) {
				if (callback === undefined) callback = function () {};
				if (window.twitterlib !== undefined) {
					var $seltweets = $('div#seltweets');
					if ($seltweets.exists()) {
						twitterlib.timeline('SocEntLdn', {limit : 3}, function (tweets, options) {
							if (tweets.length) {
								var html = '';
								for (var i = 0; i < tweets.length; i++) {
									html += twitterlib.render(tweets[i]);
								}
								$seltweets.append('<ul>' + html + '</ul>');
							} else {								
								$seltweets.hide();
							}
						});
					}
					
					var $selsearch = $('div#seltweets-search');
					if ($selsearch.exists()) {
						twitterlib.search ('#Socent', {limit : 3}, function (tweets, options) {
							if (tweets.length) {
								var html = '';
								for (var i = 0; i < tweets.length; i++) {
									html += twitterlib.render(tweets[i]);
								}
								$selsearch.append('<ul>' + html + '</ul>');
							} else {								
								$selsearch.hide();
							}
						});
					}
				}
				callback();
			}		
		}
	})();
	
	// init the app
	SEL.init(function () {
		log('all loaded');
	});
});