var accHeight = 0;
var exploding_info_top = 300;
var imageSetNumber = 1;
var imageDelay = false;
var featuredCount = 1;
var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
if (badBrowser) {
  exploding_info_top = 500;
}
$(document).ready(initHome);
$(document).ready( function() { 
  if (badBrowser) {$('#exploding').pngFix();}
});

function initHome() {
  $('#exploding .info').css({top:exploding_info_top, opacity:0, display:'block'});
  $('#exploding .frame > div').mouseenter(explode);
  $('#exploding .nav > div').click(imageSetClick);
  $('#accordion .article > p').mouseover(openAccordion);
  $('#accordion .content').each(initAccordion);
  setInterval ( "timerImageSet()", 7000);
  setInterval ( "timerFeatured()", 3000);
}

function timerFeatured() {
  featuredCount++;
  if (featuredCount == 19) featuredCount = 1;
  $('#featured').attr({'src':'http://www.impactmenusystems.com/images/uploads/featured/product'+featuredCount+'.jpg'});
}

function explode() {
  var time = 320;
  $('#exploding .info').animate({top:exploding_info_top, opacity:0}, {duration:time, queue:false});
  $(this).find('.info').animate({top:268, opacity:1}, {duration:time, queue:false});
  switch( $(this).attr('class') ){
    case 'one':
      $('#exploding .two').animate({left:455}, {duration:time, queue:false});
      $('#exploding .three').animate({left:682}, {duration:time, queue:false});
      break;
    case 'two':
      $('#exploding .two').animate({left:228}, {duration:time, queue:false});
      $('#exploding .three').animate({left:682}, {duration:time, queue:false});
      break;
    case 'three':
      $('#exploding .two').animate({left:228}, {duration:time, queue:false});
      $('#exploding .three').animate({left:455}, {duration:time, queue:false});
      break;
  }
}

function imageSetClick() {
  imageDelay = true;
  index = $(this).attr('class');
  rotateImageSet(index);
}

function timerImageSet() {
  if (imageDelay) { imageDelay = false; }
  else {
    if (imageSetNumber == 1) rotateImageSet('btn2');
    else if (imageSetNumber == 2) rotateImageSet('btn3');
    else if (imageSetNumber == 3) rotateImageSet('btn1');
  }
}

function rotateImageSet(setIndex) {
  if( setIndex == 'btn1'){
    imageSetNumber = 1;	  
    $('#exploding .btn1 img').attr('src','http://www.impactmenusystems.com/images/uploads/template/one_on.jpg');
    $('#exploding .one img').attr('src','http://www.impactmenusystems.com/images/uploads/template/bar_before.jpg');
    $('#exploding .two img').attr('src','http://www.impactmenusystems.com/images/uploads/template/bar_mid.jpg');
    $('#exploding .three img').attr('src','http://www.impactmenusystems.com/images/uploads/template/bar_after.jpg');
  } else
    $('#exploding .btn1 img').attr('src','http://www.impactmenusystems.com/images/uploads/template/one.jpg');
	
  if( setIndex == 'btn2'){
    imageSetNumber = 2;
    $('#exploding .btn2 img').attr('src','http://www.impactmenusystems.com/images/uploads/template/two_on.jpg');
    $('#exploding .one img').attr('src','http://www.impactmenusystems.com/images/uploads/template/outside_before.jpg');
    $('#exploding .two img').attr('src','http://www.impactmenusystems.com/images/uploads/template/outside_mid.jpg');
    $('#exploding .three img').attr('src','http://www.impactmenusystems.com/images/uploads/template/outside_after.jpg');
  } else
    $('#exploding .btn2 img').attr('src','http://www.impactmenusystems.com/images/uploads/template/two.jpg');
	
  if( setIndex == 'btn3'){
    imageSetNumber = 3;
    $('#exploding .btn3 img').attr('src','http://www.impactmenusystems.com/images/uploads/template/three_on.jpg');
    $('#exploding .one img').attr('src','http://www.impactmenusystems.com/images/uploads/template/theater_before.jpg');
    $('#exploding .two img').attr('src','http://www.impactmenusystems.com/images/uploads/template/theater_mid.jpg');
    $('#exploding .three img').attr('src','http://www.impactmenusystems.com/images/uploads/template/theater_after.jpg');
  } else
    $('#exploding .btn3 img').attr('src','http://www.impactmenusystems.com/images/uploads/template/three.jpg');
}

function initAccordion(index, element){
  if( $(this).height() > accHeight ){
    accHeight = $(this).height();
  }
  if(index != 0){
    $(this).height(0);
    $(this).prev('p').css({paddingTop:20, paddingBottom:20});
  }
}

function openAccordion() {
  if($(this).css('paddingTop') == '20px'){
    var articles = $('#accordion .article > p');
    for (var i=0; i<articles.length; i++){
      if (articles[i] != this) {$(articles[i]).next('.content').animate({height:0},{duration:500, queue:false});
        $(articles[i]).animate({paddingTop:20, paddingBottom:20},{duration:500, queue:false});
      } else {$(this).next('.content').animate({height:accHeight},{duration:500, queue:false});
        $(this).animate({paddingTop:0, paddingBottom:0},{duration:500, queue:false});
      }
    }
  }
}