// JavaScript Document
$.fn.opacity = function(_value) { 
    return $(this).css('opacity',_value);
};


var focus = 1;


try { document.execCommand('BackgroundImageCache', false, true); } catch (e) {} // [IE6] save flicker

if (typeof GMIX == 'undefined') var GMIX = {};

GMIX.V5 = {

	setup: function() {
		//if($('#navi')){ this.setup_navigation(); }
		
		this.setup_navigation(); 
		$('a').each(function() {

			var id = (/^[^#]+(#.+)$/.exec(this.href) || [])[1];
			if (!id) return;
			$(this).click(function() {
				//alert($(id).parent().parent().position().top);
				var top = (id == '#top') ? 0 : $(id).offset().top - 0;				
				GMIX.V5.scroll_to(top,id);
				return false;
			});
		});	
		if($('#sc_balmain').is('*')) { 		
			this.start_showcase_balmain();
		} 
		
	},
	setup_navigation: function() {
		$(window).scroll(function() {
			resetNavi();
			resetSideNavi();
		});
		$(window).resize(function(){
			resetNavi();
			resetSideNavi();
			placeBlinks();
		});
	},
	scroll_to: function(top,id) {
		//alert(top + "_" +id)
		$('html, body').animate(
			{ 	scrollTop: top },650, 'easeInOutExpo',function(){ document.location.hash = id } 
		);
	},
	scroll_to_id: function(id) {
			var top = (id == '#top') ? 0 : $(id).offset().top - 0;				
			$('html, body').animate(
				{ 	scrollTop: top },650, 'easeInOutExpo' 
			);
			GMIX.V5.flag_states(id);
			
	},
	scroll_to_id_hash: function(id) {
			var top = (id == '#top') ? 0 : $(id).offset().top - 0;				
			$('html, body').animate(
				{ 	scrollTop: top },650, 'easeInOutExpo', function(){ window.location.hash = id } 
			);
	},
	flag_states: function (id){
		
		//alert(id);
		if(id == "#top"){			oneup = "#top"; 		onedown = "#showcase";	}
		if(id == "#showcase"){		oneup = "#top"; 		onedown = "#about";		}
		if(id == "#about"){			oneup = "#showcase"; 	onedown = "#contact";	}
		if(id == "#contact"){		oneup = "#about"; 		onedown = "#footer";	}
		if(id == "#footer"){		oneup = "#contact"; 	onedown = "#top";	}
		
		
	},
	start_showcase_balmain: function(){
		
		var curFocus = 1;
		var timepassed = 0;
		
		$('.focusMap').opacity(0.9);
		$('.thum1').opacity(1.0);
		$('.txtos').css({
			display: 'none'});
		$('.txt1').css({
			display: 'block'});
		
		$('.focusMap').mouseover(function(){
			//alert($(this).attr("ref"));
			if($(this).attr("ref") != curFocus){
				timepassed = 0;
				curFocus = $(this).attr("ref");
				GMIX.V5.swapImage(curFocus);
			}
		});
		
		
		
		setInterval(function() { 
		 	timepassed = timepassed + 1;
			if(timepassed == 7 ){
				timepassed = 0;
				curFocus = parseInt(curFocus) + 1;
				if(curFocus >= 8) curFocus = 1;
				GMIX.V5.swapImage(curFocus);
			}
		}, 1000);
		
	}, 
	swapImage :  function(num){
		
		$('.focusMap').opacity(0.9);
		$('.txtos').css({
			display: 'none'});
		
		var _img = $('.imgos').find('img');
		var _src = $(_img).attr("src");
		var _urlArray = _src.split(".jpg");
		var _customImage = _urlArray[0].substr(0,_urlArray[0].length - 1) + num + ".jpg";
		//alert(_urlArray[0].substr(0,_urlArray[0].length - 1));
		//$(_img).attr("src", _customImage);
		$(_img).hide().bind("load",function(){ $(_img).fadeIn(); });
		$(_img).attr("src", _customImage);
		
		var targetThum = ".thum" + num;
		$(targetThum).opacity(1.0);
		
		var targetTxt = ".txt" + num;
		$(targetTxt).css({
			display: 'block'});
		

	}
	
	
};
var hashs = ["#top", "#showcase", "#about", "#contact"];
var oneup = hashs[0];
var onedown = hashs[1];

function moveUp(){
	GMIX.V5.scroll_to_id_hash(oneup);
}
function moveDown(){
	GMIX.V5.scroll_to_id_hash(onedown);
}
function resetNavi(){
	if ($.browser.msie) {
		$('.navi ul').css({
			position: 'absolute'
		}).stop().animate({
			top: $(window).scrollTop() + 110
		}, 800, "easeInOutExpo");
	}
		
}
function resetSideNavi(){
	
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	$('.arrow_up').css({top:$(window).height() - 150});
	$('.arrow_down').css({top:$(window).height() - 116});
}



$(document).ready(function() {
	GMIX.V5.setup();
	// init rollover
	if($('#slideImg1').is('*')) { 		$("#slideImg1").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg2').is('*')) {		$("#slideImg2").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg3').is('*')) {		$("#slideImg3").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg4').is('*')) {		$("#slideImg4").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg5').is('*')) {		$("#slideImg5").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg6').is('*')) {		$("#slideImg6").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg7').is('*')) {		$("#slideImg7").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg8').is('*')) {		$("#slideImg8").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg9').is('*')) {		$("#slideImg9").cycle({		fx: 'fade',		speed:900,		delay: 600});	}
	if($('#slideImg10').is('*')) {		$("#slideImg10").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg11').is('*')) {		$("#slideImg11").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg12').is('*')) {		$("#slideImg12").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg13').is('*')) {		$("#slideImg13").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	if($('#slideImg14').is('*')) {		$("#slideImg14").cycle({		fx: 'fade',		speed:600,		delay: 600});	}
	
	
	
	
	JsRoller.initialize( 'rollover' );	
	if (navigator.platform.indexOf("android")>=0 || navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod' || navigator.platform == 'Android') {
		$("div.arrow").css({"display":"none"});
	}
	jQuery(window).hashchange(function() {
		GMIX.V5.scroll_to_id(document.location.hash);
    });
	
	if($('.navi').is('*')){ $('.navi').height($(document).height()) ; }
	resetSideNavi();
	resetNavi();
	
	
	
	
	if($('#home').is('*')) { 
	//alert(rptH);	
	
		placeBlinks();
	
		$('.scrollArea').css({"width":910*rptH});
	} 
	
});
function placeBlinks(){
	$('.blinks').each(function(i){
		$(this).css({
			"top":blinkY[i],
			"left":$('.wrapper').width()*0.5 + blinkX[i]
		});
	})
}



function toLeft(){
	focus=focus-1;
	if(focus<1) focus = 1;
	slide9Grid();
}

function toRight(){
	focus=focus+1;
	if(focus>=rptH) focus = rptH;
	slide9Grid();
}
function slide9Grid(){
	
	$('.scrollArea').animate({
		"left":-910 * (focus - 1)
	},800, "easeInOutExpo")
}
function setLoop(obj,v1, v2,v3,v4){
	$(obj).delay(v1).fadeIn(v2).delay(v3).fadeOut(v4, function(){ setLoop(obj, v1, v2, v3, v4); });
}

