/* Author: Nick Cousins */

/* Global Variables */
var current_slide = 0;   /* Initialise current slide pointer */


/*  On Page Ready */
$(function(){

    /* Homepage Hero Image Preloader */
    
    var imageList = new Array();
    imageList.push('cement-industry.jpg');
    imageList.push('heap-leaching.jpg');
    imageList.push('mining.jpg');
    imageList.push('ports-inland-terminals.jpg');
    imageList.push('power-stations.jpg');
    imageList.push('quarrying-aggregates.jpg');
    imageList.push('rail-car-loading-unloading.jpg');
    imageList.push('steel-manufacturers.jpg');
    imageList.push('stockyard-management.jpg'); 
    imageList.push('new-products.jpg'); 
    
    $.each(imageList,function(index,value){
       $('body').append('<img style="display:none" src="'+template_directory+'/images/home/ajax-area/'+value+'" alt=""/>'); 
    });
    
    /* End Homepage Hero Image Preloader */
    
    /* Homepage Hero Nav Hover */    
    $('#product_industries li').mouseover(function()
    {
        doHoverOnProductIndustry($(this)); 
        clearInterval(slideshowinterval);
    });
    /* End Homepage Hero Nav Hover */
    
    /* Searchbox Label */
    $('.searchbox').val('Search...'); 
    $('.searchbox').focus(function(){
        $(this).val('');
    });
    $('.searchbox').blur(function(){
    if ($(this).val=='') $(this).val('Search...');
    });
    /* End Searchbox Label */
    
    /* Homepage Hero Nav Animator */
	var slideshowinterval = setInterval('skip_slideshow()',5000);
    /* End Homepage Hero Nav Animator */
   
    /* Make entire List Item clickable where it contains a link */
    $('li').click(function(){
		var lnk = $('a:first', this);
		
        if (lnk.length > 0 && lnk.attr('href') != '')
			window.location.href=$(this).find('a').attr('href');
    });
    $('li span').click(function(){
        if ($(this).find('a').length > 0)
			window.location.href=$(this).find('a').attr('href');
    });
    /* End List Item Clicky Thingy */
    
    /* Search */
    $('#s').keydown(function(e){
        if (e.which == 13)
            window.location.href='/?s='+$(this).val();
    })
    /* End Search */
    
    /* Fix WordPress' "clever" classes that break the carousel */
    $('article img').removeClass('alignnone');
    
    /* Carousel */
    $('#product_gallery').iziziCarousel(
    {
        images:$('article img'),
        circular:false,
        autoRotate:true,
        width:'630',
        height:'293'
    });   
    
    $('.iziziCarousel_next').click(function(){
       $('#product_gallery').iziziCarousel('next'); 
    });
    
    $('.iziziCarousel_prev').click(function(){
       $('#product_gallery').iziziCarousel('prev'); 
    });
    var cvidpage = 1;
    $('.video_next').click(function(){
        if (cvidpage < vidpagecount)
        {
            $('.vidpageslide').animate({left:'-=270'}); 
            cvidpage++;
        }
    });
    $('.video_prev').click(function(){
       if (cvidpage > 1)
        {
            $('.vidpageslide').animate({left:'+=270'}); 
            cvidpage--;
        }
    });
    /* End Carousel */
  
    /* Accordion Nav Animation */
    $('.accordion-nav span').click(function(e){
        e.stopPropagation();

        var tAcNode = jQuery(this).parent().children('ul');

        if (tAcNode.css('display') != 'block') var openn = true; else var openn = false;
        $('.accordion-nav ul').not($(this).parents()).each(function(){
            $(this).slideUp();
        });

        if (openn) {
            tAcNode.slideDown({duration:400,easing:'swing'
            });
        }
    });
    /* End Accordion Nav Animation */
    
    /* Open Accordion Nav at Current Point */
    /*$('.accordion-nav .current').parents().each(function(){
            $(this).show();
    });
    $('.accordion-nav .current').children('ul').show();*/
    /* End Open Accordion Nav at Current Point */
        
    /* Markets Map Hover */
    $('.markets-hover a').mouseout(function(){
        $('.markets-hover').css('background-position','0 0');
    });
    
    $('.markets-hover a').mouseover(function(){
        if ($(this).attr('id')=='mkt-north-america')
            {
                $('.markets-hover').css('background-position','0 -385px');
            }
        else
        if ($(this).attr('id')=='mkt-latin-america')
            {
                $('.markets-hover').css('background-position','0 -770px');
            }
        else
        if ($(this).attr('id')=='mkt-africa')
            {
                $('.markets-hover').css('background-position','0 -1155px');
            }
        else
        if ($(this).attr('id')=='mkt-europe')
            {
                $('.markets-hover').css('background-position','0 -1538px');
            }
        else
        if ($(this).attr('id')=='mkt-oceania')
            {
                $('.markets-hover').css('background-position','0 -2307px');
            }
        else
        if ($(this).attr('id')=='mkt-asia')
            {
                $('.markets-hover').css('background-position','0 -1923px');
            }
    });

    /* End Markets Map Hover */
    
});
/* End DOM Ready */
   
    
/* Hover Function for homepage hero nav */
function doHoverOnProductIndustry(obj){
   $('#hero img').attr('src',template_directory+'/images/home/ajax-area/'+obj.attr('data-ref')+'.jpg');
   
   if (obj.attr('data-ref').indexOf('products') != -1)
     $('#hero_link').attr('href','/products/'+obj.attr('data-ref'));
   else
     $('#hero_link').attr('href', obj.attr('data-ref'));

   obj.parent().children().removeClass('current');
   obj.addClass('current');
   obj.parent().children('li').each(function(i,v){
	if (v==obj)
		{
			current_slide=i;
		}
   });
}

/* Automatic slide advance for homepage hero nav */
function skip_slideshow()
{
	var slidescount = $('#product_industries li').length;
	if (current_slide == (slidescount-1))
		{
			doHoverOnProductIndustry($('#product_industries li:first-child'));
			current_slide=0;
		}
		else
	$('#product_industries li').each(function(i,v){
		if (i - current_slide == 1)
		{
			doHoverOnProductIndustry($(this));
			current_slide=i;
			return false;
		}
	});
}
