// JavaScript Document

var path = "";

var over_menu = null;

$(document).ready(function()
{
	$(".submenu").parents("li").each(function()
	{
		$(this)
			.mouseover(function()
			{ 
				over_menu = this;
				var submenu = $(this).find(".submenu");
				if ( $(submenu).css("display") == "none" )
				{
					$(submenu).stop().css({ left:0, opacity:0 }).animate({ left:20, opacity:1 });
				}
				
				$(submenu).show();
				$(this).children("div").addClass("select"); 
			})
			.mouseout(function()
			{
				over_menu = null;
				$(this).oneTime(100, null, function()
				{
					if ( over_menu == null || over_menu != this )
					{
						$(this).find(".submenu").hide(); 
						$(this).children("div").removeClass("select");
					}
				});
			});
	});
});

var main = 
{
	data : [
		{ "src":"../images/home/logo.gif", "classes":"logo", "events":"main.showLogo()", "style":"position:absolute; top:218px; left:275px; display:none;" },
		{ "src":"../images/home/1.jpg", "classes":"photo1", "events":"main.showPhoto()", "style":"position:absolute; top:0px; left:70px; display:none;" },
		{ "src":"../images/home/2.jpg", "classes":"photo2", "events":"main.showPhoto()", "style":"position:absolute; top:0px; left:70px; display:none;" }
	],
	count : 0,
	init : function()
	{
		$(".home").animate({ width:"100%", left:"0%" }, 1000, function()
		{
			main.effect();
		});
	},
	container : function() { return $(".home").children(".content_area"); },
	effect : function()
	{
		var ary = main.data[main.count];
		
		if ( main.count == 0 )
		{
			var onComplete = function()
			{
				$(main.container()).html('<img src="'+ary.src+'" style="'+ary.style+'" onload="'+ary.events+'" class="'+ary.classes+'">');
			}
			if ( $(main.container()).children("*").length > 0 )
			{
				$(main.container()).children("*").each(function(){ $(this).animate({ opacity:0 }, 1000, onComplete); });
			} else {
				onComplete();
			}
		} else {
			$(main.container()).append('<img src="'+ary.src+'" style="'+ary.style+'" onload="'+ary.events+'" class="'+ary.classes+'">');
		}
	},
	showLogo : function()
	{
		$(main.container()).children( "." + main.data[main.count].classes )
			.css({ opacity:0 })
			.stop().show()
			.animate({ opacity:1, top:198 }, 1000, function()
			{
				$(this).oneTime(4000, "time", function()
				{
					main.count = ( main.count + 1 < main.data.length ) ? main.count + 1 : 0;
					main.effect();
				});
			});
	},
	showPhoto : function()
	{
		$(main.container()).children( "." + main.data[main.count].classes )
			.css({ opacity:0 })
			.stop().show()
			.animate({ opacity:1 }, 1000, function()
			{
				$(main.container()).children("*").not( "." + main.data[main.count].classes ).each(function(){ $(this).remove(); });
				$(this).oneTime(3000, "time", function()
				{
					main.count = ( main.count + 1 < main.data.length ) ? main.count + 1 : 0;
					main.effect();
				});
			});
	}

}


var bespoke = 
{
	data : new Array(),
	count:0,
	init : function()
	{
		$.ajax({
			url:"../bespoke.xml",
			dataType:"xml",
			success:function(data)
			{
				bespoke.data = new Array();
				$(data).find("data").find("img").each(function()
				{
					bespoke.data.push ( { tn:$(this).attr("tn"), src:$(this).attr("src") } );
				});
				bespoke.setup();
			}
		});
		
		bespoke.scrollbar();
	},
	setup : function()
	{
		$(".photo_container").empty();
		$(".photo_container").append ( '<img src="../'+ bespoke.data[bespoke.count].tn +'" >');
	},
	prev : function()
	{
		bespoke.count = ( bespoke.count - 1 > 0 ) ? bespoke.count - 1 : bespoke.data.length - 1;
		bespoke.setup();
	},
	next : function()
	{
		bespoke.count = ( bespoke.count + 1 < bespoke.data.length ) ? bespoke.count + 1 : 0;
		bespoke.setup();
	},
	scrollbar : function()
	{
		scrollbar.init ( "#info", "#drag", { top:10, bottom:296 } );
	}
}

var scrollbar =
{
	startTop:0,
	startY:0,
	draging:false,
	top:0,
	bottom:100,
	content:null,
	drag:null,
	wheel : function ( e )
	{
        var delta = 0;
        if ( !e ) e = window.event;
        if ( e.wheelDelta ) { delta = e.wheelDelta / 120; } 
		else if ( e.detail ) { delta = -e.detail / 3; }
        
		scrollbar.startTop = parseInt($(scrollbar.drag).css("top"));
		
		scrollbar.move ( delta * 20 * -1 );
	},
	move : function ( delta )
	{
		var Y = scrollbar.startTop + delta;
		
		if ( Y < scrollbar.top ) Y = scrollbar.top;
		if ( Y > scrollbar.bottom ) Y = scrollbar.bottom;

		$(scrollbar.drag).css( { top:Y });
		
		var H = scrollbar.bottom - scrollbar.top;
		var P = ( Y - scrollbar.top ) / H;
		
		var cH = $(scrollbar.content).height() - $(scrollbar.content).parent().height();
		if ( cH > 0 )
		{
			$(scrollbar.content).stop().animate({ top:cH * P * -1 });
		}
	},
	init : function( content, drag, attr )
	{
		scrollbar.content = content;
		scrollbar.drag = drag;
		if ( attr != "undefined" )
		{
			if ( attr.top != "undefined" ) this.top = attr.top;
			if ( attr.bottom != "undefined" ) this.bottom = attr.bottom;
		}
		if ( !isTouch() )
		{
			$(drag)
				.mousedown(function(e)
				{
					scrollbar.startTop = parseInt($(this).css("top"));
					scrollbar.startY = e.clientY;
					scrollbar.draging = true;
					
					$(document)
						.mousemove(function(e)
						{
							if ( scrollbar.draging )
							{
								scrollbar.move ( e.clientY - scrollbar.startY );
							}
							e.preventDefault();
						})
						.mouseup(function(e)
						{
							scrollbar.draging = false;
						});
				})
				
			var c = $(content).get(0);
			if ( c.addEventListener ) 
			{
				c.addEventListener('DOMMouseScroll', function ( e ) { scrollbar.wheel( e ); }, false);
			}
			c.onmousewheel =  function ( e ) { scrollbar.wheel ( e ); };
				
		} else {
			var t = $(drag).get(0);
			t.ontouchstart = function(e)
			{
				var touch = e.touches[0];
				scrollbar.startTop = parseInt($(drag).css("top"));
				scrollbar.startY = touch.pageY;
			}
			t.ontouchmove = function(e)
			{
				e.preventDefault();
				var touch = e.touches[0];
				scrollbar.move ( touch.pageY - scrollbar.startY );
			}
		}
	}
}


var jewels =
{
	perpage : 8,
	init : function( cid, page )
	{
		$.ajax({
			url:"jewels_list.php",
			type:"post",
			data:{ cid:cid, page:page },
			dataType:"xml",
			success:function(xml)
			{
				var total = parseInt($(xml).children("data").attr("total"));
				
				var ary = new Array();
				$(xml).children("data").find("item").each(function()
				{
					ary.push ( { data:"../"+$(this).attr("photo"), original:"../"+$(this).attr("original"), width:parseInt($(this).attr("w")), height:parseInt($(this).attr("h")) });
				});
				rotater.init ( $("#jewels"), ary );
				
				var total_page = Math.ceil ( total / jewels.perpage );
				if ( total_page >  1 )
				{
					if ( page < total_page )
					{
						$(".btn_item_next").show();
					}
					if ( page > 1 ) {
						$(".btn_item_prev").show();
					}						
				}
			}
		});
	}
}

var symbols =
{
	perpage : 8,
	init : function( cid, page )
	{
		$.ajax({
			url:"symbols_list.php",
			type:"post",
			data:{ cid:cid, page:page },
			dataType:"xml",
			success:function(xml)
			{
				var ary = new Array();
				$(xml).children("data").find("item").each(function()
				{
					ary.push ( { data:"../"+$(this).attr("photo"), original:"../"+$(this).attr("original"), width:parseInt($(this).attr("w")), height:parseInt($(this).attr("h")) });
				});
				rotater.init ( $("#symbols"), ary );
			}
		});
	}
}


function isTouch()
{
	if ( jQuery.browser.webkit && navigator.platform.indexOf("iP") > -1 ) return true;
	if ( document.ontouchstart ) return true;
	
	return false;
}

var rotater =
{
	container:null,
	data:[],
	interval:0,
	radius:330,
	speed:0.5,
	min_scale:0.3,
	distance:30,
	updateTime:150,
	countLoaded:0,
	width:200,
	height:200,
	over:null,
	pause:false,
	init : function ( container, data )
	{
		var This = this;
		rotater.container = $(container).get(0);
		rotater.data = data;
		
		for ( var i = 0; i < rotater.data.length; i++ )
		{
			rotater.data[i].theta = i * 360 / rotater.data.length;
			
			var top = ( rotater.height - rotater.data[i].height ) / 2;
						
			var mc = $(rotater.container).append('<div id="mc_'+i+'" original="'+rotater.data[i].original+'" style="position:absolute; top:0px; left:0px;"><img src="'+rotater.data[i].data+'" onload="rotater.loaded(this)" style="padding-top:'+top+'px;"></div>' );
			
			if ( !isTouch() )
			{
				$("#mc_"+i)
					.css({ cursor:"pointer" })
					.mouseover(function()
					{ 
						This.over = this.id.replace("mc_","");
					})
					.mouseout(function()
					{
						This.over = null;
					})
			}
			
			$("#mc_"+i)
				.click(function()
				{
					rotater.pause = true;
					changeLightbox( $(this).attr("original"), "" );
				});
		}
		
		if ( isTouch() )
		{
			var touchX = 0;
			var t = $(rotater.container).get(0);
			t.ontouchstart = function(e)
			{
				var touch = e.touches[0];
				touchX = touch.pageX;
			}
			t.ontouchmove = function(e)
			{
				//e.preventDefault();
				
				var touch = e.touches[0];
				
				rotater.speed = Math.ceil( ( touch.pageX - touchX ) / 75 );
				
				rotater.render();
			}
		} else {
			if ( rotater.data.length > 2 )
			{
				$(rotater.container)
					.mousemove(function(e)
					{
						var x = e.pageX;
						var radius = $(window).width() / 2;
						
						if ( x > radius )
						{
						  var speed = ( x - radius ) / radius * 1;
						} else {
							var speed = ( radius - x ) / radius * 1 * -1;
						}
						rotater.speed = speed;
					});
			}
		}
			
	},
	loaded : function( obj )
	{
		this.countLoaded++;
		

		if ( this.countLoaded >= this.data.length )
		{
			this.render();
			if ( !isTouch() )
			{
				this.interval = setInterval ( this.render, this.updateTime );
			}
		}
	},
	render : function()
	{
		if ( !rotater.pause )
		{
			var ary = new Array();
			for ( var i = 0; i < rotater.data.length; i++ )
			{
				var mc = $(rotater.container).children("#mc_"+i);
				
				if ( rotater.data.length > 2 )
				{
					rotater.data[i].theta = rotater.data[i].theta + rotater.speed;
					
					var sinX = Math.sin ( Math.PI / 180 * rotater.data[i].theta );
					var cosX = Math.cos ( Math.PI / 180 * rotater.data[i].theta );
					
					var scale = ( rotater.min_scale + ( cosX + 1 ) * ( ( 1 - rotater.min_scale ) / 2 ) );
					
					var img = $(mc).children("img");
					var w = rotater.data[i].width;
					var h = rotater.data[i].height;
					
					$(img).css({ width:Math.ceil(w * scale), height:Math.ceil(h * scale) });
					
					$(mc).css({ left:Math.ceil(sinX * rotater.radius) - ( rotater.width / 2 ), top:Math.ceil(cosX * rotater.distance) });
				}
				else if ( rotater.data.length == 2 )
				{
					var left = ( i == 1 ) ? -$(mc).width() - 30 : 30;
					$(mc).css({ left:left });
				} 
				else if ( rotater.data.length == 1 )
				{
					$(mc).css({ left:-$(mc).width() / 2 });
				}

				
				var alpha = 0.2 + ( cosX + 1 ) * 0.8;
				if ( alpha > 1 ) alpha = 1;
	
				if ( rotater.over != null )
				{
					if ( i == rotater.over ) alpha = 1;
					else alpha = 0.2;
				}

				if ( $.browser.msie )
				{
					// IE use Filter
					//$(mc).css("filter", "alpha(opacity="+(alpha * 100)+")");
				} else {
					$(mc).css({ opacity:alpha });
				}
				
				ary.push ( { obj:mc, scale:scale });
			}
			ary.sort(function(a,b){
				return a.scale-b.scale
			});
			for ( var i = 0; i < ary.length; i++ )
			{
				$(ary[i].obj).css({ zIndex:i });
			}
		}

	}
}






var news =
{
	show : function( page )
	{
		if ( page == "undefined" ) page = 1;
		$("#news").show().stop().css({ opacity:0 }).animate({ opacity:1 });
		$("#news_container").html("");
		
		$.ajax({
			url:path + "load_news.php",
			type:"post",
			data:{ page:page, path:path },
			success:function(data)
			{
				$("#news_container").html(data);
			}
		});
	},
	
	hide : function()
	{
		$("#news").hide();
	},
	
	detail : function( id )
	{
		$(".news").find(".bg").hide();
		$("#news_list").hide();
		$("#news_paging").hide();
		$("#news_detail").html("");
		$.ajax({
			url:path + "load_news_detail.php", type:"post", data:{ id:id, path:path },
			success:function(data)
			{
				$("#news_detail").html(data);
				$("#news_detail").show();
			}
		});
	},
	
	closeDetail : function()
	{
		$(".news").find(".bg").show();
		$("#news_detail").hide();
		$("#news_list").show();
		$("#news_paging").show();
		getFlash("flash").show_bg();
	},
	
	initPhoto : function()
	{
		$(".photo_tn").children("li").children("a:first").click();
	}
	
}



var contact =
{
	send : function()
	{
		$(".error_message").hide();
		
		var ary = new Array();
		ary.push ( ( $("#name").val() == "" ) ? false : true );
		ary.push ( ( $("#email").val() == "" ) ? false : true );
		ary.push ( ( $("#phone").val() == "" ) ? false : true );
		ary.push ( ( $("#subject").val() == "" ) ? false : true );
		ary.push ( ( $("#message").val() == "" ) ? false : true );
		
		if ( !contact.valid ( ary ) )
		{
			$(".error_message").show();
		} else {
			$("#form").submit();
		}
	},
	valid : function( ary )
	{
		var b = true;
		for ( var i = 0; i < ary.length; i++ )
		{
			if ( !ary[i] ) 
			{
				b = false;
				break;
			}
		}
		return b;
	}
}



function changeLightbox( href, title )
{
	$(".lightbox").attr({ href:href, title:title });
	if ( href != "" ) $(".lightbox").click();
}

function showImage()
{
	$(this).show().css({ opacity:0 }).animate({ opacity:1 }, 3000);
}
