var machineryRolled = 0;
var technicalRolled = 0;
var newMachWidth = 0;
var newMachBgRight = 0;
var newTechWidth = 0;
var newTechBgRight = 0;

$(document).ready(function() {
	
	$('.meta-nav').parent().hide();
	
	$('#container a[target="_blank"]').addClass("external");
	
	var authorContent = $('#entry-author-info').html();
	$('#entry-author-info').hide();
	
	$('.addthis_toolbox').after('<div id="entry-author-info">'+authorContent+'</div>');
	
	$('.backToTop').click(function(e) {
		e.preventDefault();
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	
	$('a.machClose').click(function(e) {
		e.preventDefault();
		$('.machineryMenu').hide();
		machineryRolled = 0;
	});
	
	/*Tweetable */
	
	jQuery('#tweets').tweetable({username: 'HeadlandNews', limit:3,time: true});
	
	//$('#tweets').append('<a href="http://twitter.com/#!/HeadlandNews" target="_blank" class="followUs">Follow us</a>');
	setTimeout(function(){ $('#tweets').append('<a href="http://twitter.com/#!/HeadlandNews" target="_blank" class="followUs">Follow us</a>') }, 1000);

	/* End Tweetable */
	
	/* Slider */
	
	/*jQuery('#slider').s3Slider({
		timeOut: 5000
	});*/
	
	$('.sliderImage').click(function() {
		imageLink = $(this).children('a').attr('href');
		window.location = imageLink;
	}); 
	
	/* End Slider */
	
	$('#menu-main-menu li span .menuDivider').last().remove();
	$('#menu-main-menu li a span').addClass('other');
	
	var machineryBlock = $('.machineryMenu').html();
	//console.log(machineryBlock);
	var technicalBlock = $('.technicalMenu').html();
	
	var machineryMenu = $('#menu-main-menu li a span').eq(1);
	var technicalMenu = $('#menu-main-menu li a span').eq(2);
	
	$('.machineryMenu').remove();
	$('.technicalMenu').remove();

	machineryMenu.parent().parent().append('<div class="machineryMenu megaMenu"></div>');
	technicalMenu.parent().parent().append('<div class="technicalMenu megaMenu"></div>');
	
	$('.machineryMenu').append(machineryBlock);
	$('.technicalMenu').append(technicalBlock);
	
	machineryMenu.removeClass('other');
	technicalMenu.removeClass('other');
	machineryMenu.append('<div class="menuOpenIcon"></div>');
	technicalMenu.append('<div class="menuOpenIcon"></div>');
	
	$('#menu-main-menu li a span').mouseover(function() {
		if($(this).hasClass('other')) {
			$('.machineryMenu').hide();
			$('.technicalMenu').hide();
			technicalRolled = 0;
			machineryRolled = 0;
		}
	});
	
	$('.menu-header').mouseleave(function() {
		$('.machineryMenu').hide();
		$('.technicalMenu').hide();
		technicalRolled = 0;
		machineryRolled = 0;
	});
	
	technicalMenu.mouseover(function() {
		$(this).css('background-position','138px -40px');
		$(this).parent().addClass('active');
		$('.machineryMenu').hide();
		machineryRolled = 0;
		if(technicalRolled == 1) {
			$('.technicalMenu').show();
			refreshBackgrounds(".technicalMenuInner");
		} else {
			$('.technicalMenu').fadeIn('fast');
			refreshBackgrounds(".technicalMenuInner");
		}
		technicalRolled = 1;
	});
	
	$('.technicalMenu').mouseenter(function() {
		$('.technicalMenu').show();	
		technicalMenu.parent().addClass('active');
		technicalMenu.css('background-position','138px -40px');
	});
	
	$('.technicalMenu').mouseleave(function() {
		technicalMenu.parent().removeClass('active');
		technicalMenu.css('background-position','138px 0px');
	});
	
	technicalMenu.mouseout(function() {
		$(this).css('background-position','138px 0px');
		$(this).parent().removeClass('active');
	});
	
	machineryMenu.mouseover(function() {
		$(this).css('background-position','90px -40px');
		$(this).parent().addClass('active');
		technicalRolled = 0;
		$('.technicalMenu').hide();
		if(machineryRolled == 1) {
			$('.machineryMenu').show();
			refreshBackgrounds(".machineryMenuInner");
		} else {
			$('.machineryMenu').fadeIn('fast');
			refreshBackgrounds(".machineryMenuInner");
		}
		machineryRolled = 1;
	});
	
	$('.machineryMenu').mouseenter(function() {
		$('.machineryMenu').show();	
		machineryMenu.parent().addClass('active');
		machineryMenu.css('background-position','90px -40px');
	});
	
	$('.machineryMenu').mouseleave(function() {
		machineryMenu.parent().removeClass('active');
		machineryMenu.css('background-position','90px 0px');
	});
	
	machineryMenu.mouseout(function() {
		$(this).css('background-position','90px 0px');
		$(this).parent().removeClass('active');
	});
	
	$('.cm_ajax_failed').click(function(e) {
		e.preventDefault();
		$(this).hide();
	});
	$('.cm_ajax_success').click(function(e) {
		e.preventDefault();
		$(this).hide();
	});

});

$(window).load(function() {
    jQuery('#slider').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        //slices:15,
        animSpeed:1500, //Slide transition speed
        pauseTime:7500,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:false, //Next & Prev
        directionNavHide:true, //Only show on hover
        controlNav:false, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:false, //Use left & right arrows
        pauseOnHover:false, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0.8, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, //Triggers after all slides have been shown
        lastSlide: function(){}, //Triggers when last slide is shown
        afterLoad: function(){} //Triggers when slider has loaded
    });
});

function refreshBackgrounds(selector) {
  // Chrome shim to fix http://groups.google.com/a/chromium.org/group/chromium-bugs/browse_thread/thread/1b6a86d6d4cb8b04/739e937fa945a921
  // Remove this once Chrome fixes its bug.
  if (/chrome/.test(navigator.userAgent.toLowerCase())) {
    $(selector).each(function() {
      var $this = $(this);
      if ($this.css("background-image")) {
        var oldBackgroundImage = $this.css("background-image");
        setTimeout(function() {
          $this.css("background-image", oldBackgroundImage);
        }, 1);
      }
    });
  }
}

function machClose() {
	$('.machineryMenu').hide();
	machineryRolled = 0;
}

function techClose() {
	$('.technicalMenu').hide();
	technicalRolled = 0;
}

function makeActive () {
	$('.menuOpenIcon').hide();
	var machWidth = $('#menu-main-menu li a span').eq(1).width();
	var techWidth = $('#menu-main-menu li a span').eq(2).width();
	newMachWidth = machWidth - 15;
	newMachBgRight = newMachWidth - 15;
	newTechWidth = techWidth - 15;
	newTechBgRight = newTechWidth - 15;
	$('#menu-main-menu li a span').eq(1).css({'background-image':'url(http://www.headland.com.au/wp-content/themes/headland/images/menu-closed-icon.png)', 'text-align':'left', 'padding-left':'15px', 'width': newMachWidth+'px', 'background-position':newMachBgRight+'px 0px'});
	$('#menu-main-menu li a span').eq(2).css({'background-image':'url(http://www.headland.com.au/wp-content/themes/headland/images/menu-closed-icon.png)', 'text-align':'left', 'padding-left':'15px', 'width': newTechWidth+'px', 'background-position':newTechBgRight+'px 0px'});
}
