/*

minus99.com

*/

var canPress = true, onFocus = 0, sh = $(window).height(), info = $('.info'), spots = [], itemHeight = 500, list = $("#list"), isExternal = false, current = 0, int;

// init
$(document).ready(function(){
	
	align();
	$('ul#list li.spot').each(function(){
	    spots.push({ypos:$(this).position().top, isSlide:$(this).hasClass("isSlide") ? true : false});
	});
	
	/// jumper
	var loc = window.location.toString(),
	index = loc.indexOf("#");
	if( index > 0)
	{
		var l = parseInt(loc.substring(index+1, loc.length));
		if(!isNaN(l))
			goto(l, 1);
	}
	// end of jumper
});

function align()
{
	var first  = list.children("li.spot, li.hiddenSpot"),
	m = (sh-500)/2+"px";
	first.css({	"padding-top": m, "height" : sh*3+"px" });
	$('.wallpaper ul li').css("height", sh+"px");
}

// up and down slider
function slideDown(){
	setTimeout(function(){ canPress = true; }, 1000);
	if(onFocus < $("ul#list").children("li.spot").length-1)
		onFocus++;
	goto(onFocus, 1);
}

function slideUp(){
	setTimeout(function(){ canPress = true; }, 1000);
      if(onFocus > 0)
      	onFocus--;
	goto(onFocus, 1);
}

/* goto function */
function goto(n, power){
	
	onFocus = n;
	
	var loc = window.location.toString(),
	l = loc.substring(0, loc.indexOf("#"));	
	window.location.replace(l+"#"+onFocus);
		
	$('html, body').animate({scrollTop:$("#spot"+onFocus).offset().top}, 1000*power, traceSpot());
	$('.wallpaper').animate({top:"-"+($(".wallpaper ul li:eq("+onFocus+")").position().top)+"px"}, 1000*power);
}

/*
$(window).jkey('down', function(){
    if(canPress)
    {
        canPress = false;
        slideDown();
    }
}).jkey('up', function(){
    if(canPress)
    {
        canPress = false;
        slideUp();
    }
}).jkey('right', function()
{
 	if(onFocus == 1)
		$('.about').nextb();
	else if(onFocus == 4)
		$('.brands').nextc();
}).jkey('left', function()
{
	if(onFocus == 1)
		$('.about').prevb();
	else if(onFocus == 4)
		$('.brands').prevc();
});
*/

/*
function initKeys()
{
	jQuery(document).bind('keydown', 'down',function (evt){ 
		if(canPress){
			canPress = false;
			slideDown();
		} 
		return false; 
	});
	
	jQuery(document).bind('keydown', 'up',function (evt){ 
		if(canPress){
			canPress = false;
			slideUp();
		} 
		return false; 
	});
	
	jQuery(document).bind('keydown', 'right',function (evt){ 
		if(onFocus == 1)
			$('.about').nextb();
		else if(onFocus == 4)
			$('.brands').nextc();
		
		return false; 
	});
	
	jQuery(document).bind('keydown', 'left',function (evt){ 
		if(onFocus == 1)
			$('.about').prevb();
		else if(onFocus == 4)
			$('.brands').prevc();
		
		return false; 
	});
}
*/

/*
$(document).ready(function() {
	$(document).shortkeys({
		'up':		function () { if(canPress){ canPress = false; slideDUp(); }  },
		'down':	function () { if(canPress){ canPress = false; slideDown(); } }
	});
});
*/

$(document).keydown(function(e){
	if(e.which == 37) // left arrow
	{
		if(onFocus == 1)
			$('.about').prevb();
		else if(onFocus == 4)
			$('.brands').prevc();
	}
	else if(e.which == 39) // right arrow
	{
		if(onFocus == 1)
			$('.about').nextb();
		else if(onFocus == 4)
			$('.brands').nextc();
	}
	else if(e.which == 38) // up arrow
	{
		if(canPress){
			canPress = false;
			slideUp();
		}
	}
	else if(e.which == 40)
	{
		if(canPress){
			canPress = false;
			slideDown();
		}
	}
	return false;
});

$(function(){
	
	$(window).scroll(function(){ $('.wallpaper').css({"top" : (-$(window).scrollTop()*('.wallpaper').length*sh/$(document).height())+"px"}); });
	
	jQuery(window).bind('scrollstart', function(){
		clearTimeout(int);
      });
	
	jQuery(window).bind('scrollstop', function(e){
		
		var winOffset = $(window).scrollTop();
		
		if(winOffset > sh){
    			$('.mainmenu, #upArrow').fadeIn("fast");
			
			if(winOffset+(sh*4) < $(document).height())
				$('#downArrow').fadeIn("fast");
			else
				$('#downArrow').fadeOut("fast");
		}else
			$('.mainmenu, #upArrow, #downArrow').fadeOut("fast");
		
		if(Math.round($(window).scrollTop()/$(document).height()*10) <= $('li.spot').length)
			onFocus = Math.round($(window).scrollTop()/$(document).height()*10);
		else
			onFocus = $('li.spot').length-1;
		
		int = setTimeout(function(){ goto(onFocus, .4) }, 500);
		
       });
});

function traceSpot()
{
	// do nothing
}

$(window).resize(function(){
    sh = $(window).height();
    align();
    clearTimeout(int);
    int = setTimeout(function(){ goto(onFocus, .2) }, 500);
});


// main menu
var menuisopen = false;
$('.l-menu').click(function(){
	if(!menuisopen)
	{
		menuisopen = true;
		$('.menuLine').animate({width: "140px"}, 200);
		$('.menuIcon').animate({width: "0px"}, 200, function(){
			$('.submenu').show().css('opacity', 0).animate({opacity:1, left:"0px"}, 200);
		});
	}
	else
	{
		menuisopen = false;
        $('.submenu').animate({opacity:0, left:"-10px"}, 200, function(){
			$('.menuLine').animate({width: "23px"}, 200);
			$('.menuIcon').animate({width: "33px"}, 200);
		}).hide();
	}
});

/* other  */
$("ul.sliderContent").each(function(){
	var totalwidth = 0;
	$("li.canvas", this).each(function(){
		totalwidth += $(this).outerWidth();
    	});
    	$(this).css('width', totalwidth+'px');	
	$(this).parent("div.sliderHolder").parent(".content").append('<div class="nav"><a class="next" href="javascript:void(0)">next</a><a class="prev" href="javascript:void(0)">prev</a></div><div class="whereami"><ul></ul></div>');
});

//
$('.about').minusSlider();
$('.brands').brandsMinusSlider();
$(".news .galleryHolder").minusNews();


/* video page */
$("ul.gridContent li").mouseover(function(){ $('.shade', this).show() }).mouseout(function(){ $('.shade', this).hide(); });

/* sport page */
var spd = 200;

$(".sport ul.sportGate li").each(function(){ if($("img", this).length>0){ $(this).append('<div rel="'+$(this).attr("rel")+'" class="clicker"></div>'); } });
$(".sport ul.sportGate li div.clicker").each(function(){
	$(this).mouseover(function(){
		$(this).animate({height:"60px"}, spd);
		$(this).parent('li').animate({height:"60px"}, spd);
		$(this).siblings('span.cameraIcon').hide().css("left", "10px").show();
		$(this).siblings('img.thumb, small', this).fadeIn(spd);
	}).mouseout(function(){
		$(this).animate({height:"20px"}, spd);
		$(this).parent('li').animate({height:"20px"}, spd);
		$(this).siblings('span.cameraIcon').hide().css("left", "260px").show();
		$(this).siblings('img.thumb, small', this).fadeOut(spd);
	}).click(function(){
		$(this).animate({height:"20px"}, spd);
		$(this).parent('li').animate({height:"20px"}, spd);
		$(this).siblings('span.cameraIcon').hide().css("left", "260px").show();
		$(this).siblings('img.thumb, small', this).fadeOut(spd);
		
		var externalUrl = "http://www.cemhakko.com/?cat="+$(this).attr("rel");
		$(".sport ul.sportGate, .sport .title").fadeOut(500, function(){
			$(".sport .externalContent").load(externalUrl, function(){
					$(this).fadeIn(1000);
					$('.galleryHolder', this).minusGallery();
					$('.backtosportgate a', this).click(function(){
						$(".sport .externalContent").fadeOut(500, function(){
							$(".sport .externalContent").html("");
							$(".sport ul.sportGate, .sport .title").fadeIn(500);
						});
					});
			});
		});
	})
});



/* social page */
$(".social ul.socialGate li").each(function(){ if($("img", this).length>0){ $(this).append('<div rel="'+$(this).attr("rel")+'" class="clicker"></div>'); } });
$(".social ul.socialGate li div.clicker").each(function(){
	$(this).mouseover(function(){
		$(this).animate({height:"60px"}, spd);
		$(this).parent('li').animate({height:"60px"}, spd);
		$(this).siblings('span.cameraIcon').hide().css("left", "10px").show();
		$(this).siblings('img.thumb, small', this).fadeIn(spd);
	}).mouseout(function(){
		$(this).animate({height:"20px"}, spd);
		$(this).parent('li').animate({height:"20px"}, spd);
		$(this).siblings('span.cameraIcon').hide().css("left", "260px").show();
		$(this).siblings('img.thumb, small', this).fadeOut(spd);
	}).click(function(){
		$(this).animate({height:"20px"}, spd);
		$(this).parent('li').animate({height:"20px"}, spd);
		$(this).siblings('span.cameraIcon').hide().css("left", "260px").show();
		$(this).siblings('img.thumb, small', this).fadeOut(spd);
		
		var externalUrl = "http://www.cemhakko.com/?page_id="+$(this).attr("rel");
		$(".social ul.socialGate, .social .title").fadeOut(500, function(){
			$(".social .externalContent").load(externalUrl, function(){
					$(this).fadeIn(1000);
					$('.textScrollerHolder #mcs_gallery', this).mCustomScrollbar("vertical",300,"easeOutCirc",1.05,"auto","yes","yes",15);
					$('.backtosocialgate a', this).click(function(){
						$(".social .externalContent").fadeOut(500, function(){
							$(".social .externalContent").html("");
							$(".social ul.socialGate, .social .title").fadeIn(500);
						});
					});
			});
		});
	})
});

// video 
$("#mcs_container").mCustomScrollbar("vertical",300,"easeOutCirc",1.05,"auto","yes","yes",15);
$('#mcs_container ul.gridContent li a').click(function(){
	$(this).animate({opacity: "0.5"}, 200);
	var vid = $(this).attr("rel"), img = $(this).attr("name");
	
	$('#mcs_container').animate({top:"-500px"}, 300);
	$('#tv_container')
	.html('<div class="video"><div id="thevideo"></div><span>'+$(this).attr("title")+'</span></div>')
	.animate({top:0}, 300, function(){
		
		var flashvars = { theVideo: vid, preview: img};
		var params = { wmode: "transparent" }
		swfobject.embedSWF("http://www.cemhakko.com/wp-content/flash/flasherPlayer.swf", "thevideo", "600", "350", "9.0.0", "http://www.cemhakko.com/wp-content/themes/twentyten/js/swfobject/expressInstall.swf", flashvars, params);
		
		$(".video .backtothumbs").fadeIn("fast")
		.click(function(){
			$(this).fadeOut("fast", function(){
				$('#tv_container').animate({top:"500px"}, 300).html("");
		    		$('#mcs_container').animate({top:0}, 300);
			});
		});
	});
});
