var browser;
var tickerTimer;
var showTimer = false;
jQuery(document).ready(function($){
   	   if( $('#news-ticker ul li').length > 0 ){
		$('#news-ticker ul li:first').fadeIn().addClass('activeTicker');
		goTicker()
	   }
	   //Slow down jquery for chopiness
	   jQuery.fx.interval = 1;
	   $('a.fb-share-chiclet').bind('click', function(){
		clicked = $(this);
		window.open (clicked.attr('href'),"mywindow","status=1, width=650, height=400");
		return false;
	   });
	   //Store 
	   browser = $('body').dataset();
	   
	   if( browser.device == 'ipad' || browser.device == 'iphone' )
	   {
		window.onscroll = function() {
		
			document.getElementById('quote-bar').style.top =
			   (window.pageYOffset + window.innerHeight - 25) + 'px';
		};
	   }

	   initSubMenu();
	   
	   // Show and hide the awesome sub menu
	   $('#nav-primary li').each(function(){
		t = $(this);
		var c = t.children('.drop-down-menu');
		e = c.length;
		if(e){
		   a = '#' + t.attr('id');
		   $(a + ' a.top-level-link').bind('mouseleave, mouseenter',function(){
			c.toggleMenu(600);
		   });	
		}
		

	});
	
	$('.drop-down-menu li').each(function(){
	   var urlLink = $(this).find('.sub-menu-link, .sub-menu-coatings-link').attr('href');   
	   $(this).click(function(){	
		window.location = urlLink;
	   })
	});
	if(!Modernizr.input.placeholder){
		// If the browser doesn't support placeholder then fill the default
		// and 'bind the appropriate event
		$('input[type=text], input[type=password], input[type=email], input[type=number], input[type=search]').each(
		   function(){
			var ele = $(this);
			ele.val(ele.attr('placeholder'));
		}).focus(
		   function() {
			toggleDefaultInputValue(this);
		}).blur(
		   function(){
			toggleDefaultInputValue(this);
		}); 
	   }
	   $('#mobile-nav').bind('change', function(){
		window.location= $(this).val();
	   });
});

function goTicker(){
   tickerContainer = $('#news-ticker-content ul');
   tickerItems = $('#news-ticker ul li');
   tickerTimer = window.setTimeout(function(){ tickerTape();}, 5000);
}

function tickerTape(){
   aT = $('#news-ticker .activeTicker');
   nI = aT.next();
   if(nI.length == 0){
	nI = $('#news-ticker ul li:first');
   }
   aT.fadeOut('slow', function(){
	nI.fadeIn('slow', function(){
	   tickerTimer = window.setTimeout(function(){ tickerTape();}, 5000);
	}).addClass('activeTicker');
   }).removeClass('activeTicker');
}

function initSubMenu(){
   $('a.sub-menu-coatings-link').each(function(){
	var t = $(this);
	t.parents('li').addClass('coating-item');
   });
   $('li#menu-item-123').parent('ul').addClass('coating-list');
   viewAll = '<li class="menu-item menu-item-type-post_type menu-item-object-page coating-item"><img width="80px" src="http://69.247.143.19:8080/nitroplate/wp-content/themes/nitroplate/images/buttons/view_more.png" /><a href="http://98.211.44.186:8080/nitroplate/?page_id=7" class="sub-menu-coatings-link">View All</a></li> ';
   $('.drop-down-menu-content ul li.coating-item:last-child').after(viewAll);
}

$.fn.toggleMenu = function(speed) {
   var c = this;
   if(c.hasClass('animating')){ return false;}
   
   var h = c.hasClass('active');
   var d = $('.drop-down-menu.active:not(.animating)');
   
   if( (browser.name == 'firefox' && browser.version>=4) || (browser.name == 'msie' && browser.version>=7) ){
	if(h){
	   c.slideUp(speed, function(){
		c.removeClass('animating');
	   }).unbind('mouseleave').removeClass('active');
	}else{
	   if(d.length){
		d.slideUp(200).unbind('mouseleave').removeClass('active');
		c.slideDown(speed, function(){
				   c.addClass('active').removeClass('animating').bind('mouseleave', function(){ c.toggleMenu(500); });
		   }).addClass('animating');
	   }else{
		c.slideDown(speed, function(){
			c.addClass('active').removeClass('animating').bind('mouseleave', function(){ c.toggleMenu(500); });
		}).addClass('animating');
	   }
	}
   }else{
	if(h){
	   c.css('display','none').removeClass('active').unbind('mouseleave');
	}else{
	   if(d.length){
		d.css('display','none').unbind('mouseleave').removeClass('active');
		c.css('display', 'block').bind('mouseleave', function(){ c.toggleMenu(500); }).addClass('active');
	   }else{
		c.css('display', 'block').bind('mouseleave', function(){ c.toggleMenu(500); }).addClass('active');
	   }
	}
   }
   return false;
}


function toggleDefaultInputValue(ele)
{
	// Toggle the default value of an input element
	// Used if placeholder isnt supported
	var obj = $(ele);
	defaultVal = obj.attr('placeholder');
	curVal = obj.val();
	if(defaultVal == curVal){obj.val(''); return true;}
	if(curVal == ''){obj.val(defaultVal); return true;}
	return false;
}

function getElementOptions(object)
{
   options = $.parseJSON(object.dataset('opts'));
   return options;
}

