var timeout    = 25;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
   jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('div').css('visibility', 'visible');
}

function jsddm_close()
{  
   if(ddmenuitem) {
	   ddmenuitem.css('visibility', 'hidden');
   }
}

function jsddm_timer()
{  
  closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}
}

function swapImage()
{
     var originalimgSrc = $(this).attr('src');	 
	 var hoverimgSrc = originalimgSrc.replace('_o','_h');
	 $(this).attr('src', hoverimgSrc);
}
function swapImageRestore()
{
     var originalimgSrc = $(this).attr('src');	 
	 var hoverimgSrc = originalimgSrc.replace('_h','_o');
	 $(this).attr('src', hoverimgSrc);
}
function swapTabRestore()
{
     var originalimgSrc = $(this).attr('src');	 
	 var hoverimgSrc = originalimgSrc.replace('_h','_o');
	 if(!$(this).parent().parent().hasClass('active')) {
		  $(this).attr('src', hoverimgSrc);
	 }
}

$(document).ready(function() {  

   $('.hoverItem').each(function(){
 	 var img = new Image();
	 img.src = $(this).attr('src').replace('_o','_h');	 
	 $(this).bind('mouseover', swapImage);
	 $(this).bind('mouseout', swapImageRestore);
   });
   $('.hoverTab').each(function(idx){
 	 var img = new Image();
	 img.src = $(this).attr('src').replace('_o','_h');	 
	 $(this).bind('mouseover', swapImage);
	 $(this).bind('mouseout', swapTabRestore);
   }); 
   
   $('.tabView > .tab').each(function(){
     $(this).find('.hoverTab:eq(0)').mouseover();
   });






   $('#brandSubNav li h4 + div').each(function(){
     var originalSrc = $(this).prev().children('a').find('img').attr('src'); 
     $(this).hover(function(){
	   var hoverSrc = originalSrc.replace('_o','_h');
	   if($(this).prev().find('img').hasClass('hoverItem')) $(this).prev().children('a').find('img').attr('src', hoverSrc)
	 }, 
	 function(){
	   var hoverSrc = originalSrc.replace('_h','_o');
	   if($(this).prev().find('img').hasClass('hoverItem')) $(this).prev().children('a').find('img').attr('src', hoverSrc)
	 })
   });
   
   
   var tabLoc = $('#tabSummary').attr('class');   
   if(tabLoc=='active') {
	  $('#tabNav ul').css("background-position", "left -80px");
   } else {     
     $('#tabSummary').hover(function(){
       $('#tabNav > ul').css('background-position', 'left -80px');
     }, function(){
       $('#tabNav > ul').css('background-position', 'left top');
     });
   }
   
   var tabHeight = $('#tabNav ul').height();
   if(tabHeight >=34) {
	  $('#tabNav ul').css('background-image', 'url(img/bg_tab_CSR_long.gif)');
   }
   
   $('ul.subItems > li').addClass('liSubItems');
   

   $('#mainContent .summary, .eventImg').children(':last-child').filter('p').addClass('last');
   $('#mainContent .appendix').parent().addClass('withAppendix');
   
   $('.subTitle + .newsTitle').addClass('nextTonewsTitle');
   $('.itemTitle + .date').addClass('nextToitemTitle');
   
   if($('#block-addon .middle').height()<100) { $('#block-addon .middle').addClass('short'); }
   
 	$('#numberPromotion').jcarousel({ scroll:1 });	
	$('#phonePromotion').jcarousel({ scroll:1 });		
    //$('#numberPromotion ul.jcarousel-list.jcarousel-list-horizontal').css('width', '1000px');
				
   $('#brandSubNav > ul > li').bind('mouseover', jsddm_open);
   $('#brandSubNav > ul > li').bind('mouseout',  jsddm_timer);
   
    $('#keyvisual').agileTab({
      trigger: 'hover',
      neighborItem: true,  
      animation: true,
	  animationInteval: 5000,
	  hoverToStop: true,
	  animationFade: true,	  	  
      initialItem: 1,
	  circulation: true
	});
    $('#serviceCombo').agileTab({
      trigger: 'click',
      neighborItem: true,  
      animation: true,  
	  animationInteval: 3000,
	  hoverToStop: true,		  
	  animationFade: true,	  
      initialItem: 1,
	  circulation: true,
	  carousel: false
	});
    $('#mobilePackage').agileTab({
      trigger: 'click',
      neighborItem: false,
      animation: false,  
	  animationInteval: 500,
	  hoverToStop: false,
	  animationFade: false,	 
      initialItem: 1,
	  circulation: true,
	  carousel: true
	});
    $('#customerBonus').agileTab({
      trigger: 'click',
      neighborItem: false,
      animation: false,  
	  animationInteval: 500,
	  hoverToStop: false,
	  animationFade: false,	 
      initialItem: 1,
	  circulation: true,
	  carousel: true
	});	
	
    $('#productCombo').agileTab({
      trigger: 'click'
	});	
    

	
	$('#mobilePackage .tabContent li:eq(0)').clone().appendTo('#mobilePackage .tabContent');
	$('#promoCustomer .tabContent li:eq(0)').clone().appendTo('#promoCustomer .tabContent');
   

    $('.aPop a.more').hover(function(){
			$(this).next().show();
		},
		function(){
			$(this).next().hide();			
		}
	);	

    $('#mobilePackage, #customerBonus').find(".tab span").hover(function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");		
		}
	);

    $(".aShowlayer").click(function(){ $(this).next().show(); });
    $("#PAdeclaration").click(function(){ $(this).hide(); });
	var localLi = $("#localNav ul.navMenu li").size();
	if(localLi>0 && localLi<=2) {$("#localNav ul.navMenu").addClass('mini'); }
	if(localLi>2 && localLi<=6) {$("#localNav ul.navMenu").addClass('short'); }	
	
	
});

