var Plugins = (function()
{
	return {
		getDomain: function()
		{
			return 'http://' + document.location.toString().match(/:\/\/(.[^\/]+\/(cygnus-instruments.com){0,1})/)[1];
		},
		bind: function() /* Bind application */
		{
			/* Bind flexslider */
			$('.flexslider').flexslider(
			{
				directionNav: true,
				controlNav: true 
			});
			
			
			/* Bind collapsable data */
			$('.header-section').click(function() 
			{
				$(this).next().slideToggle(600).end().find('h4').toggleClass('expand').toggleClass('colapse')
				
				return false;
				
			}).next().show();
			
			
			this.nav().dataImg().goTop().modernizr();
			
			/* Enable pretty photo */
			$("a[rel^='gallery']").prettyPhoto();
			
			/* Pretty slideshows */
			$("a[rel^='gallery-slideshow']").prettyPhoto(
			{
				slideshow:5000, 
				autoplay_slideshow: true
			});

			/* Bind autocomplete */
			$(':input[name=search]').autocomplete(
			{
				source: this.getDomain() + "/plugins/catalog/autocomplete.php",
				minLength: 2,
				select: function( event, ui ) 
				{
					
				},
				search: function(event, ui)
				{
					
				}
			});
			
			/* Bind FAQs accordion */
			$('.faqs h3').click(function() 
			{
				$(this).next().slideToggle('slow');
				return false;
			}).next().hide();
			
			$('.block-result-description').hover(function()
			{
				$(this).addClass('block-result-description-active');
				
			}, function()
			{
				$(this).removeClass('block-result-description-active');
			});
		},
		toggleAll: function()
		{
			$('.header-section').next().slideToggle(600).end().find('h4').toggleClass('expand').toggleClass('colapse');
		},
		goEnquire: function()
		{
			var form = $('div.product-enquiry-form');
			
			$('body,html').animate(
			{
				scrollTop: form.offset().top -10
			},800);
				
			return this;
		},
		goTop: function()
		{
			var top = $('<div/>', 
			{
				title: "Return to the top of the page"
			}).addClass('go-top').appendTo('body'), visible = false
			
			$(window).scroll(function() 
			{
				if($(this).scrollTop() != 0) 
				{
					if (!visible)
					{
						visible = true;
						
						top.stop(true, true).animate(
						{
							right: -15
						}, 500);
					}
				} 
				else 
				{
					top.stop(true, true).animate(
					{
						right: -100
					}, 500, function()
					{
						visible = false
					})
				}
			});
			
			top.click(function() 
			{
				$('body,html').animate(
					{
						scrollTop:0
					},800);
			});	
	
			return this;
		},
		dataImg: function() /* Custom gallery for Cygnus */
		{	
			var P = this, preview = $('<div/>').addClass('preview').append('<div></div>').appendTo('body');
		
			var over = function(event, element)
			{
				var image = $(this).attr('data-image'), id = $(this).attr('data-article-id');


				preview.find('>div').empty().end().css(
				{
					width:	100,
					height:	100,
					top:  $(this).offset().top,
					left: $(this).offset().left - ($.client.browser == "Chrome" ? -85 : ($.browser.msie && $.browser.version < 8 ? -85 : 0))
				}).show();
				
				Core.preloader.reset().queue(
				[
					{
						source: P.getDomain() + '/explorer/articles/article' + id + '/' + image
					}
				]).preload(function(ui)
				{
					var img = ui.images.shift();
					
					preview.stop(true, true).animate(
					{
						width: img.size.width,
						height: img.size.height
					}, function()
					{
						var i = $(img.image);
					
						i.hide();
						
						preview.find('>div').empty().append(i);
						
						/* Fade image */
						i.fadeIn(300);
					});
					
					
				})
			}
			
			var out = function(event, element)
			{
				preview.hide();
			}
			
			$('[data-image]').hover(over, out);
			
			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('');
						}
					})
				});			
			}
		},
		nav: function()
		{
			var nav = $('ul.nav'), binded = false;
			
			nav.find('>li').bind(
			{
				mouseenter: function()
				{
					var menu = $(this), sub = $(this).find('>div');
					
					sub.stop(true, true).fadeIn(300, function()
					{
						if (!binded)
						{
							var carousel = menu.find('.carousel');
							
							if (carousel.length)
							{
								/* Bind carousel */
								menu.find('.carousel').jCarouselLite(
								{
									visible:1,
									speed: 1000,
									start: 0,
									vertical: true,
									btnGo:
								    [
									    ".carousel-control .1", 
									    ".carousel-control .2",
									    ".carousel-control .3", 
									    ".carousel-control .4",
									    ".carousel-control .5"
								    ],
								    afterEnd: function(a) 
								    {
								    	var index = $(a).index();
								    	
								    	$('.carousel-control > a').eq(index - 1).addClass('active').siblings().removeClass('active');
								    }
								});
								
								binded = true;
							}
						}
					});
					
					menu.siblings().find('>div').stop(true, true).hide();
				},
				mouseleave: function()
				{
					var menu = $(this), sub = $(this).find('>div');
					
					sub.hide();
				}
			}).find('>div> ul').hover(function()
			{
				$(this).toggleClass('normal').toggleClass('white');
				
			}, function()
			{
				$(this).toggleClass('white').toggleClass('normal');
			});
			
			
			
			return this;
		},
		menu: function() /* Deprecated */
		{
			function megaHoverOver()
			{
				$(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});
					
				}
			}
			
			function megaHoverOut()
			{ 
				$(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;
		},
		unique: function(select)
		{
			var select = $(select);
			
			select.parents('li:first').siblings().find('select').val(0);
		}
	}
})()

$(function()
{
	Plugins.bind();
});
