//author:Johnny
//Email:imagineerjohn@gmail.com
//Network Marketing Dept.-Overseas Group 
//http://www.byd.com.cn
// Document is already ready!!!
$(document).ready(function() {
  // Main Nav	
  var $main_nav_items = $('#main_nav').children('#main_nav li');
  var $sub_items = $('.main_nav div.sub > ul > li');
  $main_nav_items.bind('mouseenter',
  function() {
    var $this = $(this);
    $this.addClass('selected');
	$this.siblings().children('div').hide().css({"height":"0"});
    $this.children('div').show().animate({"height":"240px"},{
        duration:400,
        easing: 'easeInOutCubic'
      });
  }).bind('mouseleave',
  function() {
    var $this = $(this);
	$this.removeClass('selected')
    $this.children('div').hide().css({"height":"0"});
  });
  
$(".main_nav > li a.browse, .main_nav > li div.navi").css({"opacity":"0"});
$(".main_nav > li div.sub").hover(function(){$(".main_nav > li a.browse, .main_nav > li div.navi").stop().animate({"opacity":"1"})},function(){$(".main_nav > li a.browse, .main_nav > li div.navi").stop().animate({"opacity":"0"})})
$(".main_nav > li a.browse").hover(function(){$(this).click()});
  
$("div.sub-scroll").scrollable({mousewheel:true}).navigator(".sub #navi");
   
$("li.sub-btn").hover(function(){$(this).stop().animate({"background-color":"#000","color":"#E00"})},function(){$(this).stop().animate({"background-color":"#444","color":"#EEE"})})
// li hover
$(".sub-nav li:not(.follow-us)").hover(function() {
  $("span", this).css({
    "border-bottom": "3px solid #F00"
  });
},
function() {
  $("span", this).css({
    'border-bottom': 'none'
  });
});
$(".sub-nav li.follow-us a").hover(function(){$(this).children("img").stop().animate({"top":"-2px"},100)},function(){$(this).children("img").stop().animate({"top":"0"},100)});
// contact us
$(".CU[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  top: '35%'
});
$("a.fleet").click(function(){$(".CU").click()});
// find a dealer
$(".FaD[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  top: '35%'
});

//clear value
var $input = $(".areabox input,.contactbox input")
$input.focus(function() {
  $(this).val("");
});
$(".contactbox .close").click(function(){$("div.error").hide()})

//Display Tooltips on hovering over the input fields if an alt tag is present 
$('.contactbox input,.areabox input').hover(function() {
  var msgTip = $(this).attr('alt');
  if (msgTip.length) {
    $(this).parent().append('<div id="menuTooltip">\
		<p>' + msgTip + '</p>\</div>');
    $("#menuTooltip").fadeIn('slow');
  }
},
function() {
  $('div#menuTooltip').remove();
})

//search
$("input#inputBox").focus(function() {
  $(this).val("");
  $(this).animate({
    backgroundColor: "#393C41"
  },
  200);
})
$("input#inputBox").blur(function() {
  $(this).val("search");
  $(this).css("background-color", "#444D52");
})

$("div.home .scroll-news").scrollable({circular: true, mousewheel: true, vertical: true}).autoscroll({
	interval: 5000		
});

///*** VS ***//
  // choices
  $(".customize input").focus(function() {
    $(this).val("");
    $(this).animate({
      "backgroundColor": "#EEE"
    },
    200);
  })
  // calculator 		  
  $("span#calculate").click(function() {
    var elecPrice = $(".clectric-cost input").val();
    var gasPrice = $(".fuel-cost input").val();
    var dailyMileage = $(".mileage input").val();
    var MPG = $("span.MPG").text();
    var MPKWH = $("span.MPKWH").text();
    var elecCost = (dailyMileage / MPKWH) * elecPrice;
    var gasCost = (dailyMileage / MPG) * gasPrice * 365;
    var annualsave = gasCost - elecCost;
    // check if inputvalue isNaN
    if (isNaN(elecPrice)) {$(".error").show("fade", 500).hide("fade", 500)}
    else {
      if (isNaN(gasPrice)) {$(".error").show("fade", 500).hide("fade", 500)}
      else {
        if (isNaN(dailyMileage)) {$(".error").show("fade", 500).hide("fade", 500)}
        else {
          // show the cost
          $(".calc-results").animate({ "height": "100px"});
          $("span#eleccost").empty().append("$" + Math.round(elecCost));
          $("span#fuecost").empty().append("$" + Math.round(gasCost));
          $("span#annsave").empty().append("$" + Math.round(annualsave));
          $(".elec-cost").delay(500).animate({"opacity": "1" },500);
          $(".fue-cost").animate({"opacity": "1"},500);
          $(".calc-results p").delay(1000).animate({"opacity": "1"}, 500)
        }
      }
    }
  })
  //*** HELP CENTER ***//
// setup ul.tabs to work as tabs for each div directly under div.panes
  $("ul.hc_tabs").tabs("div.hc-panels > div", {
    effect: 'fade',
    fadeOutSpeed: 100
  });

  // right hidden information and form
  $(".switcher").bind("click",
  function() {
    $(this).parent().next(".hidden_info").toggle("blind")
  });

  // apple style overlay
  $(".testDrive img[rel]").overlay({
    effect: 'apple',
    mask: 'rgba(0,0,0,.5)'
  });

  // setup button action. it will fire our overlay 
  $(".videos img[rel]").overlay({
    effect: 'apple',
    mask: 'rgba(0,0,0,.5)',
    top: '10%'
  });

  $("ul.faq-cats").tabs("div.faq-details > div");

  // FAQ expand
  $(".faq div.ask").click(function() {
    $(this).toggleClass("asked")
    $(this).next(".faq div.answer").toggle("slide",300)
  });

  $(".expand-all").click(function() {
    $(this).siblings(".ask").toggleClass("asked")
    $(this).siblings("div.answer").toggle("slide",300);
    $(this).toggleClass("collapse");
  });
$(".videos img").hover(function(){$(this).parents().children("img.play").stop().animate({"top":"25%","left":"35%","width":"70px"},100)},function(){$(this).parents().children("img.play").stop().animate({"top":"40%","left":"40%","width":"30px"},200)})
  ///*** BYDCHARGING STATION ***//
// focus scroll
$(".charging-parts-triggers").scrollable({ circular: true , easing: "easeOutBack"}).navigator(".chargin-navi li");
// navi scroller
$(".chargin-navi").scrollable({circular: true }).navigator()
// navi hover effect
$(".chargin-navi a img").hover(function(){$(this).fadeTo('fast',.8).fadeTo('fast',1)})

  //*** NEWS ***//
  // new list
  
  $("ul.news-list li:even").addClass("ji");
  $("ul.news-list li").hover(function(){$(this).css({"background-image":"url(../img/UI/black_bg_trans0.2.png)"}); $(this).children("p.quote").prepend("<img src='../img/UI/quote.png'>").append("<img src='../img/UI/quote2.png'>");},function(){$(this).css({"background-image":"none"});$(this).children("p.quote").children("img").remove()});
  // expand order posts	
  $("ul.news-list").append("<li class='olderPosts'><h5>Older Posts</h5></li>");
  $("ul.news-list li a[target='_blank']").append("<img style='float:right' src='../img/UI/icon_new_window.png'>");

  $(".olderPosts").click(function() {
    $(this).prev("li.hide").show('fade', 1000)
  }).hover(function() {
    $(this).stop().animate({
      "background-color": "#DDD"
    })
  },
  function() {
    $(this).stop().animate({
      "background-color": "#FFF"
    })
  })
  //*** TECHNOLOGY ****//
$(".other-main .tech-focus .parts-triggers a h3").css({"opacity":"0"});
$(".other-main .tech-focus .parts-triggers a").hover(function(){$(this).children("h3").stop().animate({"top":"-100%","opacity":"1"})},function(){$(this).children("h3").stop().animate({"top":"-50%","opacity":"0"})})
// extend overlay
$(".part img[rel]").overlay({
  effect: 'apple',
  mask: 'rgba(0,0,0,.5)'
});
// go to top
$(".gotoTop").delay(200).show("fade");

$(".gotoTop").hover(function() {
  $("img", this).animate({
    "opacity": "0.8"
  },
  500)
},
function() {
  $("img", this).stop().animate({
    "opacity": "0.3"
  },
  100)
})
// style
$(".other-main .part:even").addClass("part135");
$(".other-main .part:odd").addClass("part246");

$(".techmain .part > img").hover(function(){$(this).stop().animate({"background-color":"#03A5E3"})},function(){$(this).stop().animate({"background-color":"#FFF"})})
  //*** E6 ***//
//***** Vehicles *****//
  // style
  $(".other-main .part:even").addClass("part135");
  $(".other-main .part:odd").addClass("part246");

$("ul.focus-nav").tabs(".e6-flash > div,.k9-flash > div, .f3dm-flash > div,.s6dm-flash > div", {
	effect: 'fade',
	fadeOutSpeed: "slow",
	rotate: true
}).slideshow({autoplay: true});

// cabinet
$(".other-main .part .text img").hover(function(){$(this).stop().animate({"background-color":"#03A5E3"})},function(){$(this).stop().animate({"background-color":"#FFF"})});
$(".other-main #p6 .text img.eTaxi").click(function(){$(this).toggleClass("eTaxi2",400)});
$(".other-main #p5 .text img.co2-emission").click(function(){$(this).toggleClass("co2-emission2",400)});
$(".other-main #p4 .text img.cabinet").click(function(){$(this).toggleClass("cabinet2",400);$(this).next("span").toggle('fade',400)});

// apps
$("table.lit-apps td").hover(function() {
  $(this).stop().animate({
    "background-color": "#333"
  },
  100)
},
function() {
  $(this).stop().animate({
    "background-color": "#FFF"
  },
  100)
})

// e6 app windows
$(".360-window[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  effect: 'apple',
  top: '20%'
});

  $(".360-window").click(function() {
	// run grey360 when documents ready
	$(".360View").spritespin({
        width     : 560,
        height    : 400,
        frames    : 34,
        image     : [
		  "../img/e6_360/023[1].jpg",
		  "../img/e6_360/024[1].jpg",
		  "../img/e6_360/025[1].jpg",
		  "../img/e6_360/026[1].jpg",
		  "../img/e6_360/027[1].jpg",
		  "../img/e6_360/028[1].jpg",
		  "../img/e6_360/029[1].jpg",
		  "../img/e6_360/030[1].jpg",		  
		  "../img/e6_360/031[1].jpg",
		  "../img/e6_360/032[1].jpg",
		  "../img/e6_360/033[1].jpg",
		  "../img/e6_360/034[1].jpg",
          "../img/e6_360/001[1].jpg",
		  "../img/e6_360/002[1].jpg",
		  "../img/e6_360/003[1].jpg",
		  "../img/e6_360/004[1].jpg",
		  "../img/e6_360/005[1].jpg",
		  "../img/e6_360/006[1].jpg",
		  "../img/e6_360/007[1].jpg",
		  "../img/e6_360/008[1].jpg",
		  "../img/e6_360/009[1].jpg",
		  "../img/e6_360/010[1].jpg",
		  "../img/e6_360/011[1].jpg",
		  "../img/e6_360/012[1].jpg",
		  "../img/e6_360/013[1].jpg",
		  "../img/e6_360/014[1].jpg",
		  "../img/e6_360/015[1].jpg",
		  "../img/e6_360/016[1].jpg",
		  "../img/e6_360/017[1].jpg",
		  "../img/e6_360/018[1].jpg",
		  "../img/e6_360/019[1].jpg",
		  "../img/e6_360/020[1].jpg",
		  "../img/e6_360/021[1].jpg",
		  "../img/e6_360/022[1].jpg",
        ],
        frameTime : 60
      });		
  })

$(".spec-window[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  effect: 'apple',
  top: '20%'
});
$(".video-window[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  effect: 'apple',
  top: '10%'
});
$(".photo-window[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  effect: 'apple',
  top: '5%'
});
$(".photo-window").click(function(){$("#e6-photos").hide()})

$(".fad-window[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  top: '35%'
});  
})

//*** DOWNLOAD ***//
  $("ul.dl_tabs").tabs("div.dl_panels > div", {
    effect: 'fade',
    fadeOutSpeed: 100
  })
  // list scrollable
  $(".wallpapers .item-list").scrollable({
    circular: true
  }).navigator();

  $(".item-list .items img").click(function() {
    // see if same thumb is being clicked
    if ($(this).hasClass("active")) {
      return;
    }
    // calclulate large image's URL based on the thumbnail URL (flickr specific)
    var url = $(this).attr("src")
    // get handle to element that wraps the image and make it semi-transparent
    var wrap = $(".enlarge").fadeTo("medium", 0.5);
    // the large image from www.flickr.com
    var img = new Image();
    // call this function after it's loaded
    img.onload = function() {
      // make wrapper fully visible
      wrap.fadeTo("fast", 1);
      // change the image
      wrap.find("img").attr({
        src: url
      });
      // get the link
    };
    $("a.wallpaper-dl").attr("href", url);
    // begin loading the image from www.flickr.com
    img.src = url;
    // activate item
    $(".item-list .items img").removeClass("active");
    $(this).addClass("active");
    // when page loads simulate a "click" on the first image
  });
  $(".Wpclick").click(function(){$(".wallpapers .item-list .items img").filter(":first").click();});
  
  $(".advideo-list .adPlayer").hover(function(){$(this).children("img").attr("src","../img/UI/videoplay2.png")},function(){$(this).children("img").attr("src","../img/UI/videoplay1.png")});  
  
  //ABOUT
$(".about-focus").hover(function(){$(this).css({"-moz-box-shadow":"none","-webkit-box-shadow":"none","box-shadow":"none"})},function(){$(this).css({"-moz-box-shadow":"inset 0 0 50px #888","-webkit-box-shadow":"inset 0 0 50px #888","box-shadow":"inner 0 0 50px #888"})});

$(".about-focus a").hover(function(){$(this).parent().css({"-moz-box-shadow":"inset 0 0 50px #888","-webkit-box-shadow":"inset 0 0 50px #888","box-shadow":"inner 0 0 50px #888"})},function(){$(this).parent().css({"-moz-box-shadow":"none","-webkit-box-shadow":"none","box-shadow":"none"})});

  $(".about-main .section").hover(function(){$(this).children("div.caption").stop().animate({"bottom":"0"},500)},function(){$(this).children("div.caption").stop().animate({"bottom":"-40px"},500)});
  
  $(".about-main .section:not(.section4)").click(function(){$(this).toggleClass("sectionB",500);$(this).children("a").children("div.details").delay(500).toggle("fade",500);$(this).siblings().toggleClass("sectionS",100);$(this).children("div.caption").stop().toggle()});
  
  $(".about-focus[rel]").overlay({
  mask: 'rgba(0,0,0,.5)',
  top: '10%'
  });

$(".contact-us-form").validate();
$(".newsletterform").validate();
