(function ($) {

var self = $.cune = {
	speed: 1000,
	
	init: function () {
		jQuery.easing.def = 'easeInOutCubic';
		
		self.head();
		self.footer();
		self.products();
		self.dealers();
		self.profile();
		
		if (/Mobile.*Safari/.test(navigator.userAgent))
			self.iosFix();
	},
	
	head: function () {
		$('ul.head li.label a').click(function () {
			var a        = $(this);
			var contents = a.parent().parent().parent().find('div.content');
			
			if (a.hasClass('a_cl')) {
				a.removeClass('a_cl');
				contents.slideUp(self.speed);
				return false;
			}
			else if  (a.hasClass('a_op')) {
				a.removeClass('a_op');
				contents.slideDown(self.speed);
				return false;
			}
			
			if (a.hasClass('fo')) {
				a.removeClass('fo');
				if (a.parent().parent().parent().find('div.content:visible').length)
					return false;
				else {
					$('#rap').scrollTo(a, self.speed, {onAfter: function () { contents.slideToggle(self.speed); }});
					return false;
				}
			}
			
			a[a.hasClass('op') ? 'removeClass' : 'addClass']('op');
			
			$('#rap').scrollTo(a, self.speed, {onAfter: function () { contents.slideToggle(self.speed); }});
			
			return false;
		});
	},
	
	footer: function () {
		$('#footer a').click(function () {
			var a = $(this);
			if (a.parent().hasClass('open')) {
				$('ul.head li.label a').addClass('a_op').addClass('op');
				$('#rap').scrollTo($('#top'), self.speed, {onAfter: function () {
					$('ul.head li.label a').each(function () { $(this).click(); });
					$('#products div.handling div.sub_contents').slideDown(self.speed, function () { $(this).parent().find('div.sub_label a').addClass('op'); });
					$('#dealers div.handling div.sub_contents').slideDown(self.speed, function () { $(this).parent().find('div.sub_label a').addClass('op'); });
				}});
			}
			else if (a.parent().hasClass('close')) {
				$('ul.head li.label a').addClass('a_cl').removeClass('op');
				$('#rap').scrollTo($('#top'), self.speed, {onAfter: function () {
					$('ul.head li.label a').each(function () { $(this).click(); });
					$('#products div.handling div.sub_contents').slideUp(self.speed, function () { $(this).parent().find('div.sub_label a').removeClass('op'); });
					$('#dealers div.handling div.sub_contents').slideUp(self.speed, function () { $(this).parent().find('div.sub_label a').removeClass('op'); });
				}});
			}
			else {
				$(a.attr('href')).find('li.label a').addClass('op');
				$('#rap').scrollTo($(a.attr('href')), self.speed, {onAfter: function () {
					$(a.attr('href')).find('li.label a').addClass('fo').click();
				}});
			}
			return false;
		});
	},
	
	products: function () {
		$('#products div.handling div.sub_label a').click(function () {
			var a = $(this);
			if (a.hasClass('op')) a.removeClass('op'), $('#products div.handling div.sub_contents').slideUp(self.speed);
			else                  a.addClass('op'),    $('#products div.handling div.sub_contents').slideDown(self.speed);
			
			return false;
		});
	},
	
	dealers: function () {
		$('#dealers div.handling div.sub_label a').click(function () {
			var a = $(this);
			if (a.hasClass('op')) a.removeClass('op'), $('#dealers div.handling div.sub_contents').slideUp(self.speed);
			else                  a.addClass('op'),    $('#dealers div.handling div.sub_contents').slideDown(self.speed);
			
			return false;
		});
	},
	
	profile: function () {
		$('div.lang a').click(function () {
			$('#lang').attr('src', $('#lang').attr('src').replace(/profile_text_\w{2}.gif/, 'profile_text_' + $(this).attr('href').replace(/#/, '') + '.gif'));
			return false;
		});
	},
	
	iosFix: function () {
		$('#footer').css({
			width:  '102%',
			right:  '-5px',
			bottom: '-10px'
		});
	}
	
}

$(function () { self.init(); });

})(jQuery);
