/**
* B:SSEC Client Side Js (Singleton)
* 
* @date 13/10/2011
* @version 1.0
* @author Angel Kostadinov
* @copyright Grid Design
*/
var BSSEC = (function()
{
	return {
		getDomain: function()
		{
			return 'http://' + document.location.toString().match(/:\/\/(.[^\/]+\/(bssec.co.uk){0,1})/)[1];
		},
		subscribe: function(element, type)
		{
			var input = $(':text[name=subscriber_email]'), email = input.val();
			
			if (Core.validator.email(email))
			{
				$.ajax({
					type: 'POST',
					url: this.getDomain() + '/libraries/services/subscribe.php',
					data: $.param(
					{
						email: email,
						type: type
						
					}),
					success: function(response)
					{
						if (response.success)
						{
							var message = $('<div/>').html(response.message);
							
							input.replaceWith(message);
							
							/* Disable further unsubscribe */
							$('p.unsubscribe-link, li.subscribe-button').remove();
						}
					},
					dataType: 'json'
				});
			}
			else 
			{
				alert('Please enter valid email address!');
			}
			
			return false;
		},
		carousel: function()
		{
			$(".carousel > div").jCarouselLite(
			{
				visible:5,
				vertical: true,
				auto: 3000,
				speed: 1500,
				btnNext: ".next",
				btnPrev: ".prev"
			});
		},
		init: function() /* Initialize BSSEC */
		{
			this.menu()
				.flexslider()
				.accordion()
				.prettyPhoto()
				.modernizr();
		},
		flexslider: function()
		{
			$('.flexslider').flexslider(
			{
				directionNav: true,             //Create navigation for previous/next navigation? (true/false)
				controlNav: false 
			});
			
			return this;
		},
		menu: function()
		{
			var megaHoverOver = function()
			{
				$(this).find(".sub").stop().fadeTo('fast', 1).show();
					
				//Calculate width of all ul's
				(function($) { 
					jQuery.fn.calcSubWidth = function() {
						rowWidth = 0;
						//Calculate row
						$(this).find("ul").each(function() {					
							rowWidth += $(this).width(); 
						});	
					};
				})(jQuery); 
				
				if ( $(this).find(".row").length > 0 ) { //If row exists...
					var biggestRow = 0;	
					//Calculate each row
					$(this).find(".row").each(function() {							   
						$(this).calcSubWidth();
						//Find biggest row
						if(rowWidth > biggestRow) {
							biggestRow = rowWidth;
						}
					});
					//Set width
					$(this).find(".sub").css({'width' :biggestRow});
					$(this).find(".row:last").css({'margin':'0'});
					
				} else { //If row does not exist...
					
					$(this).calcSubWidth();
					//Set Width
					$(this).find(".sub").css({'width' : rowWidth});
					
				}
			}
			
			var megaHoverOut = function()
			{ 
			  $(this).find(".sub").stop().fadeTo('fast', 0, function() 
			  {
				  $(this).hide(); 
			  });
			}
		
		
			var config = 
			{    
				 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
				 interval: 100, // number = milliseconds for onMouseOver polling interval    
				 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
				 timeout: 500, // number = milliseconds delay before onMouseOut    
				 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
			};
		
			$("ul#topnav li .sub").css(
			{
				opacity:0
			});
			
			$("ul#topnav li").hoverIntent(config);
			
			return this;
		},
		accordion: function()
		{
			var navy = $('.navy-box-accordion > div'), active = navy.find('h3.current'), index = active.length ? parseInt(active.attr('data-index')) - 1 : 1;
			
			navy.accordion(
			{
				active: index,
				autoHeight: false,
				event: 'mouseover'
			});
			
			return this;
			
			
			//Set default open/close settings
			$('.acc_container').hide(); //Hide/close all containers
			$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
			
			//On Click
			$('.acc_trigger').hover(function()
			{
				if( $(this).next().is(':hidden')) 
				{ //If immediate next container is closed...
					$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container
					$(this).toggleClass('active').next().slideDown(); //Add .acc_trigger class to clicked trigger and slide down the immediate next container
				}
				return false; //Prevent the browser jump to the link anchor
			});
			
			return this;
		},
		modernizr: function()
		{
			if(!Modernizr.input.placeholder)
			{
				$('[placeholder]').focus(function() {
				  var input = $(this);
				  if (input.val() == input.attr('placeholder')) {
					input.val('');
					input.removeClass('placeholder');
				  }
				}).blur(function() {
				  var input = $(this);
				  if (input.val() == '' || input.val() == input.attr('placeholder')) {
					input.addClass('placeholder');
					input.val(input.attr('placeholder'));
				  }
				}).blur();
				$('[placeholder]').parents('form').submit(function() {
				  $(this).find('[placeholder]').each(function() {
					var input = $(this);
					if (input.val() == input.attr('placeholder')) {
					  input.val('');
					}
				  })
				});
			
			}
		},
		prettyPhoto: function()
		{
			$("a[rel^='prettyPhoto']").prettyPhoto();
			
			return this;
		}
	}
})();

$(function() /* Initialize BSSEC on window load */
{
	BSSEC.init();
})
