/**
 * @version  1.00
 * @updated  2010/12/20
 */



(function ($) {
	var common = window.common = {
		init: function() {
			common.load();
			common.scroll();
			common.hover();
			common.stripe_table();
			common.height();
			common.windowopen();
			common.timer();
			common.control_next();
			common.control_back();
		},


		load: function() {
			$("body").fadeIn(750);
		},
		
		
		
		scroll: function() {
			var evnt = 0;
			
			jQuery.fn.extend({
				scrollTo : function(speed) {
					/* hashの取得が出来なければ、処理を中断 */
					if(!$(this)[0].hash || $(this)[0].hash == "#") {
						return false;
					}
					return this.each(function() {
						var targetOffset = $($(this)[0].hash).offset().top - 20;
						$('html,body').animate(
							{scrollTop: targetOffset},
							speed,
							'swing',
							function () {
								evnt = 1;
							}
						);
					});
				}
			});
			
			$('a[href*=#page-top],a.pn').bind('click', function() {
				$(this).scrollTo('normal');
				return false;
			});
			
			var d = document;			
			var clear = function () {
				$('html,body').stop();
				evnt = 0;
			}
			
			if (/Firefox/.test(navigator.userAgent)) {
				d.body.addEventListener('DOMMouseScroll', function () {
					if (evnt == 1) clear();
				}, false);
			}
		},
		
		
		
		hover: function() {
			var preLoad = new Object();
			$('img.hover,input.hover').not("[src*='_hover.']").each(function(){
				var imgSrc = this.src;
				var fType = imgSrc.substring(imgSrc.lastIndexOf('.'));
				var imgName = imgSrc.substr(0, imgSrc.lastIndexOf('.'));
				var imgOver = imgName + '_hover' + fType;
				preLoad[this.src] = new Image();
				preLoad[this.src].src = imgOver;
				$(this).hover(
					function (){
						this.src = imgOver;
					},
					function (){
						this.src = imgSrc;
					}
				);
			});
		},
		
		
		
		stripe_table: function() {
			if ($('table.stripe')) {
				$('table.stripe').each(function(){
					$(this).children('tbody').each(function(){
						$(this).children('tr:even').not('tr.tbl-h2,tr.tbl-h3').each(function(){
							this.className = 'even';
						});
					});
				});
			}
		},
		
		
		
		height: function() {
			if ($('ul.horizon')){
				$('ul.horizon').each(function(){
					var highest = 0;
					
					$(this).children().each(function(){
						var height = $(this).height();
						
						if(height > highest) {
							highest = height;
						}
						
						$(this).children().height(highest);
					});
				});
			}
		},
		
		
		windowopen: function () {
			$("a#coupon").click(function(){
				window.open("/coupon/", "", "width=300,height=510,status=no,resizable=no,scrollbars=no");
				return false;
			})

		},
		
		
		
		control_next: function () {
			$('ul#sl-btn li#sl-next a').click(function () {
				if ($(this).attr('class') != 'off') {
					common.slide_next();
					$('ul#sl-bnrs').stopTime();
					common.timer();
					return false;
				} else {
					return false;
				}
			});
		},
	
		
		
		timer: function () {	
	
			$('ul#sl-bnrs').everyTime(5000, function() {
				common.slide_next();	
			});

		},
		
		
				
		slide_next: function (next_key,current_key) {
			$('ul#sl-btn li a').each(function () {
				$(this).attr('class','off');
			});
			
			var key = parseFloat($('ul#sl-bnrs li.current').attr('id').replace('sl-',''));

			if (key != 2) {
				$('ul#sl-bnrs li#sl-' + (key + 1)).attr('class','next-current');
			} else {
				$('ul#sl-bnrs li#sl-0').attr('class','next-current');
			}
			
			$('ul#sl-bnrs li.next-current').css('left','180px');
			$('ul#sl-bnrs li.current').css('left','0px');
			
			$('ul#sl-bnrs li.next-current,ul#sl-bnrs li.current').animate({
				"left": "-=180px"
			}, 500, function() {
				$('ul#sl-bnrs li#sl-' + key).attr('class','');
				$('ul#sl-bnrs li.next-current').attr('class','current');
				
				$('ul#sl-btn li a').each(function () {
					$(this).attr('class','');
				});
			});

		},
		
		
		
		control_back: function (next_key,current_key,op) {
		
			$('ul#sl-btn li#sl-back a').click(function () {
			
				if ($(this).attr('class') != 'off') {
				
					$('ul#sl-btn li a').each(function () {
						$(this).attr('class','off');
					});
					
					var key = parseFloat($('ul#sl-bnrs li.current').attr('id').replace('sl-',''));
		
					if (key != 0) {
						$('ul#sl-bnrs li#sl-' + (key - 1)).attr('class','next-current');
					} else {
						$('ul#sl-bnrs li#sl-2').attr('class','next-current');
					}
					
					$('ul#sl-bnrs li.next-current').css('left','-180px');
					$('ul#sl-bnrs li.current').css('left','0px');
					
					$('ul#sl-bnrs li.next-current,ul#sl-bnrs li.current').animate({
						"left": "+=180px"
					}, 500, function() {
						$('ul#sl-bnrs li#sl-' + key).attr('class','');
						$('ul#sl-bnrs li.next-current').attr('class','current');
						
						$('ul#sl-btn li a').each(function () {
							$(this).attr('class','');
						});
					});


					$('ul#sl-bnrs').stopTime();
					common.timer();
					return false;
					
				} else {
					return false;
				}
			});
		}


	}



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