$(document).ready(function() {

if ( $.browser.msie && $.browser.version == 7 ) {
		$('.navigation ul').append('<li class="helper"/>');
		$('.navigation a.active').append('<ins/>');
	}
	
	$('.wrap_car ul a').click(function() {
var rel = $(this).attr('rel');
$('.sl_wrap img[rel='+rel+']').parent().addClass('active').delay(100).fadeIn(100).siblings('li').removeClass('active').fadeOut(100);
var ac = $('.sl_wrap').find('.slider li.active').index();
$('.intro_wrap > div').hide().eq(ac).show();
return false;
});
	
	//$('.shine a').blink();

	if ( $.browser.msie && (jQuery.browser.version == 7) ) {
		$('.phone').append('<ins style="visibility:hidden;display:inline-block;">1111111</ins>');
	}
	
	/*if ( $('body div').hasClass('sl_wrap') ) {
		$('.sl_wrap sub').animate({
				opacity: 1
			}, 500, function(){
				$('.sl_wrap sub').click();
		});
	}*/
	
	$('.intro_wrap div:first').show();
/*
	function totalwidth() {
           var itemwidth = $('.wrapper1').width();
           $('.slide').width(itemwidth);
           var itemnum = $('.slide').length;
           $('.slides').width(itemwidth * itemnum);

       }
      totalwidth();

       function correct() {
           var reLiTmp = $('.sc .active').attr('id');
           var rerLi = reLiTmp.replace('p-', '');
           var reLiWidth = $('.slides .slide.active').width();
           var wSlide = rerLi * reLiWidth;

           $('.slides').css({'left':-wSlide});
       }


       $('.sc li').click(function() {
          var that = $(this);
          var moveToTemp = that.attr('id');
          var moveTo = moveToTemp.replace('p', 's');
          var moveTo2 = moveToTemp.replace('p-', '');
          var move = $('.slide').width() * moveTo2;
          that.addClass('active').siblings().removeClass('active');
          $('.slide#' + moveTo + '').addClass('active').siblings().removeClass('active');
          $('.slides').animate({
                   left:-move
               }, 0, 'easeInOutExpo');

       });





    $(window).resize(function() {
        totalwidth();
        correct();
    });
*/

		/* slider */
	$('.slider li').hide();
	$('.slider li:first-child').show();
	$('.slider li:first-child').addClass('active');

	$('.sl_wrap sup').click(function(){
		var open = $(this).parents('.sl_wrap').find('.slider li.active').next();
		if (!$(open).length) {
			open = $(this).parents('.sl_wrap').find('.slider li:first');
		}
		$(open).addClass('active').delay(100).fadeIn(100).siblings('li').removeClass('active').fadeOut(100);

		var ab = $(this).parents('.sl_wrap').find('.slider li.active').index();
		//$(this).parents('.content').find('.jcarousel-skin-tango').hide().eq(ab).show();
		
		$('.intro_wrap > div').hide().eq(ab).show();

                	/* slider but show/hide
	if ( $('.slider li:first').hasClass('active') ) {
		$('.sl_wrap sub').hide();
	} else {
		$('.sl_wrap sub').show();
	}
	
	if ( $('.slider li:last').hasClass('active') ) {
		$('.sl_wrap sup').hide();
	} else {
		$('.sl_wrap sup').show();
	}*/
	}); 

	$('.sl_wrap sub').click(function(){
		var open = $(this).parents('.sl_wrap').find('.slider li.active').prev();
		if (!$(open).length) {
			open = $(this).parents('.sl_wrap').find('.slider li:last');
		}
		$(open).addClass('active').delay(100).fadeIn(100).siblings('li').removeClass('active').fadeOut(100);

		var ac = $(this).parents('.sl_wrap').find('.slider li.active').index();
		//$(this).parents('.content').find('.jcarousel-skin-tango').hide().eq(ac).show();
		
		$('.intro_wrap > div').hide().eq(ac).show();

               	/* slider but show/hide
	if ( $('.slider li:first').hasClass('active') ) {
		$('.sl_wrap sub').hide();
	} else {
		$('.sl_wrap sub').show();
	}
	
	if ( $('.slider li:last').hasClass('active') ) {
		$('.sl_wrap sup').hide();
	} else {
		$('.sl_wrap sup').show();
	} */
	});
	
	$('.sl_wrap_inner sup').click(function(){
		var open = $(this).parents('.sl_wrap_inner').find('.slider li.active').next();
		if (!$(open).length) {
			open = $(this).parents('.sl_wrap_inner').find('.slider li:first');
		}
		$(open).addClass('active').delay(100).fadeIn(100).siblings('li').removeClass('active').fadeOut(100);
	});

	$('.sl_wrap_inner sub').click(function(){
		var open = $(this).parents('.sl_wrap_inner').find('.slider li.active').prev();
		if (!$(open).length) {
			open = $(this).parents('.sl_wrap_inner').find('.slider li:last');
		}
		$(open).addClass('active').delay(100).fadeIn(100).siblings('li').removeClass('active').fadeOut(100);
	});

        	/* slider but show/hide 
	if ( $('.slider li:first').hasClass('active') ) {
		$('.sl_wrap sub').hide();
	} else {
		$('.sl_wrap sub').show();
	}
	
	if ( $('.slider li:last').hasClass('active') ) {
		$('.sl_wrap sup').hide();
	} else {
		$('.sl_wrap sup').show();
	}*/

		/* jCarousel */
	$('.jcarousel-skin-tango li a').addClass('fancy');

	if ( $('.jcarousel-skin-tango').length ) {
		$('.jcarousel-skin-tango').jcarousel({
		wrap: 'circular'
		});
	}

//	$('jcarousel-skin-tango').hide();

//	$('.sl_wrap sub').animate({
//		opacity: 1
//    }, 50, function(){
//        $('.sl_wrap sub').click();
//		$('.wrapper').hide();
//		$('.wrapper:first').show();
//    });

		/* fancybox */
	if ( $('.fancy').length ) {
		$('a.fancy').fancybox();
	}

	$('#footer li').addClass(function(){
		return 'f-' + $(this).index();
	});



	$('.foot_inner li a').click(function(){

		$(this).addClass('active').parents('li').siblings().find('a').removeClass('active');

		var url = $(this).attr('href').split('#');

//		if ( $('body div.slide').hasClass(url[1]) ) {
//
//			$('.sl_wrap sub').animate({
//				opacity: 1
//			}, 51, function(){
//				$('.'+url[1]).addClass('activ');
//				var dom = $('.slide.activ').index();
//				$("#footer li.f-"+dom).find('a').addClass('active').click();
//
//			});
//
//		} else {
//
//			$('.sl_wrap sub').animate({
//				opacity: 1
//			}, 50, function(){
//				$('.sl_wrap sub').click();
//				$('.slides div:first').show();
//			});
//		}

//		$('.wrapper').hide().eq($(this).parents('li').index()).show();

		$('.sl_wrap sub').animate({
				opacity: 1
			}, 50, function(){
				$('.sl_wrap sub').click();
				$('.slides div:first').show();
			});

	});


	var url = location.hash.replace('#','');



	if ( url.length != 0 ) {

		if ( $('.slides div.slide').hasClass(url) ) {

			$('.sl_wrap sub').animate({
				opacity: 1
			}, 51, function(){
				$('.'+url).addClass('activ');
				var dom = $('.slide.activ').index();
				$("#footer li.f-"+dom).find('a').addClass('active').click();

			});

		} else {

			$('.sl_wrap sub').animate({
				opacity: 1
			}, 50, function(){
				$('.sl_wrap sub').click();
				$('.slides div:first').show();
			});
		}
		
	}

//	$('.logo').click(function(){
//		$('.wrapper').hide();
//		$('.wrapper:first').show();
//		$('#footer a').removeClass('active');
//	});

		 /* ajax */
	 $('.country').change(function() {
	  var sel = $('.country option:selected').val();
          var link = window.location;

	  $.ajax({
	   type: "POST",
	   url: "/ajax/",
	   data: "country=" + sel + "&link="+link+"",
	   dataType: 'json',
	   success: function(msg) {
		$('select.region').html(msg.region);
		$('select.city').html(msg.city);
	   }
	  });
	 });

	 $('.region').change(function() {
	  var sel = $('.country option:selected').val();
	  var reg = $('.region option:selected').val();
          var link = window.location;

	  $.ajax({
	   type: "POST",
	   url: "/ajax/",
	   data: "country=" + sel + "&region=" + reg + "&link="+link+"",
	   success: function(msg) {
		$('select.city').html(msg);
	   }
	  });
	 });

	/* 
	$('.main_view li:first').addClass('active');
	$('.main_view li').attr('rel',function(){
		return $(this).index()+1;
	});

	var mvwidth = $('.main_view').width();
	var imgsum = $('.main_view ul img').size();
	var imgreelwidth = mvwidth * imgsum;

	$('.main_view ul').css({'width' : imgreelwidth});


	rotate = function(){
		var triggerID = $active.attr('rel') - 1;
		var imgreelposition = triggerID * mvwidth;

		$('.main_view li').removeClass('active');
		$active.addClass('active');

		$('.main_view ul').fadeOut(200).animate({
			left: -imgreelposition
		}, 0 ).fadeIn(600);
	};

	rotateSwitch = function(){
		play = setInterval(function(){
			$active = $('.main_view li.active').next();
			if ( $active.length === 0) {
				$active = $('.main_view li:first');
			}
			rotate();
		}, 7000);
	};

	rotateSwitch();
	
	*/
	if( $('.main_view').length > 0 ) {
		$('.main_view').crossSlide(
        { fade: 1 },
        $.map($(".main_view img"), function(img) {
            return {
                src: img.src,
	            from: '100% 100% 1x',
	            to: '100% 100% 1x',
                time: 7
            }
        })
    );
}


	/* manager */
	$('.manager a').click(function(){
		$('.overlay').show();
		$('.popup').show();
		return false;
	});
	$('.close').click(function(){
		$('.popup').hide();
		$('.overlay').hide();
	});
	$('.overlay').click(function(){
		$(this).hide();
		$('.popup').hide();
	});

	if ( $('form.popup *').hasClass('invalid') ) {
		$('.popup').show();
		$('.overlay').show();
	} else if ( $('form.popup *').hasClass('required') ) {
		$('.popup').show();
		$('.overlay').show();
	} else {
		$('.popup').hide();
		$('.overlay').hide();
	}

	$('.invalid').parent().siblings().css('color','#f00');
	$('.required').parent().siblings().css('color','#f00');

	if ( $('.success form').length == 0 ) {
		$('.sex_message').addClass('yeah');
		$('<div class="close" />').prependTo('.sex_message');
		$('<div class="overlay" />').appendTo('.success').show();
		$('.close').click(function(){
			$('.sex_message').hide();
			$('.overlay').hide();
		});
		$('.overlay').click(function(){
			$(this).hide();
			$('.sex_message').hide();
		});
	}
	
	$('.jcarousel-skin-tango ul:hidden').parent().siblings('div').hide();
	
	rotslider();
	
	function rotslider() {
		tol = setInterval(function(){
			$('.sl_wrap_inner sup').click();
		}, 7000);
	}

	$('.sl_wrap_inner sup').hover(function(){
		clearInterval(tol);
	}, function(){
		rotslider();
	});

	if ( $('.country option:selected').length) {
                var curlink = window.location;
		var sel = $('.country option:selected').val();
	    $.ajax({
		   type: "POST",
		   url: "/ajax/",
		   data: "country=" + sel + "&link="+curlink,
		   dataType: 'json',
		   success: function(msg) {
			$('select.region').html(msg.region);
			$('select.city').html(msg.city);
		   }
		});
	}


	autoRotate();

});

function autoRotate() {
	setInterval(function(){
		$('.sl_wrap sup').click();
	}, 20000);
}



