/*
@pagetop, @Escalator, @OpenWindow, @CloseWin
*/
/*-------------------------------------
	page scroll
-------------------------------------*/
jQuery.easing.quart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
$(document).ready(function(){

	tb_init('a[href$=".jpg"]:not(.thickbox, a[href*="?"]), a[href$=".gif"][href!="?"]:not(.thickbox, a[href*="?"]), a[href$=".png"][href!="?"]:not(.thickbox, a[href*="?"]),a[href$=".JPG"]:not(.thickbox, a[href*="?"]), a[href$=".GIF"][href!="?"]:not(.thickbox, a[href*="?"]), a[href$=".PNG"][href!="?"]:not(.thickbox, a[href*="?"])'); 
	
/*-------------------------------------
	page scroll
-------------------------------------*/
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({ scrollTop: targetOffset }, 1200, 'quart');
				return false;
			}
		}
	});
	
/*-------------------------------------
	tab
-------------------------------------*/
	$(function() {
		$("a.tab").click(function() {
			$(".active").removeClass("active");
			$(this).addClass("active");
			$(".tab-content").hide('slow');
			var contentId = $(this).attr("rel");
			$("#" + contentId).show('slow'); 
		});
	});

/*-------------------------------------
	tooltip
-------------------------------------*/
	$(".tooltip").each(function() {
		var title = $(this).attr("title");
		$(this).mouseover(function() {
			if(title != '') {
				$("body").append('<div class="petips"><p>' + title + '</p></div>');
			}
			$('.petips').css({
							 opacity:"0.9",
							 position:"absolute",
							 display:"none"
							 });
			$(".petips").fadeIn();
			$(this).attr("title", "");
		}).mouseout(function() {
			$(".petips").fadeOut();
			$(".petips").remove();
			$(this).attr("title", title);
		}).mousemove(function(e) {
			$(".petips").css({
							 "top":e.pageY + 10 + "px",
							 "left":e.pageX + 10 + "px"
							 })
			})

	});

	$(".quick-viewer").each(function() {
		var filename = $(this).attr("rel");
		$(this).mouseover(function() {
			if(filename != '') {
				var image = '<img src="/studio/img/' + filename + '" />';
				$("body").append('<p class="box-g">' + image + '</p>');
			}
			$('.box-g').css({
							 position:"absolute",
							 display:"none"
							 });
			$(".box-g").fadeIn();
		}).mouseout(function() {
			$(".box-g").fadeOut();
			$(".box-g").remove();
		}).mousemove(function(e) {
			$(".box-g").css({
							 "top":e.pageY - 150 + "px",
							 "left":e.pageX + 10 + "px"
							 })
			})

	});

/*-------------------------------------
	studio slide
-------------------------------------*/
	if($("#gSearch").size() > 0) {
		$('#gSearch ul').simpleSpy();
	}
	
});

(function ($) {

	$.fn.simpleSpy = function (limit, interval) {
		limit = limit || 2;
		interval = interval || 4000;
    
		return this.each(function () {

			var $list = $(this),
			items = [], // uninitialised
			currentItem = limit,
			total = 0, // initialise later on
			height = $list.find('> li:first').height();
            
			// capture the cache
			$list.find('> li').each(function () {
				items.push('<li>' + $(this).html() + '</li>');
			});
        
			total = items.length;
        
			$list.wrap('<div class="spyWrapper" />').parent().css({ height : height * limit });
        
			$list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();
 
			// 2. effect        
			function spy() {
				// insert a new item with opacity and height of zero
				var $insert = $(items[currentItem]).css({
					height : 0,
					opacity : 0,
					display : 'none'
				}).prependTo($list);
                        
				// fade the LAST item out
				$list.find('> li:last').animate({ opacity : 0}, 1000, function () {
					// increase the height of the NEW first item
					$insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                
					// AND at the same time - decrease the height of the LAST item
					// $(this).animate({ height : 0 }, 1000, function () {
					// finally fade the first item in (and we can remove the last)
					$(this).remove();
					// });
				});
            
				currentItem++;
				if (currentItem >= total) {
					currentItem = 0;
				}
            
				setTimeout(spy, interval)
			}    
			spy();
		});
	}

/*-------------------------------------
	event delay
-------------------------------------*/
	$.fn.eventDelay = function(delay, triger, cancel, callback) {
		return this.each(function() {
			var target = $(this);
			var tid;
			target.bind(triger, function(){
				tid = setTimeout(callback, delay * 1000)
			})
			target.bind(cancel, function(){
				clearTimeout(tid)
			})
		});
	}

})(jQuery);


/*-------------------------------------
	php str_replace
-------------------------------------*/
function str_replace(search, replace, subject, count) {
	var i = 0,
		j = 0,
		temp = '',
		repl = '',
		sl = 0,
		fl = 0,
		f = [].concat(search),
		r = [].concat(replace),
		s = subject,
		ra = r instanceof Array,
		sa = s instanceof Array;
	s = [].concat(s);
	if(count) {
		this.window[count] = 0;
	}

	for(i = 0, sl = s.length; i < sl; i++) {
		if(s[i] === '') {
			continue;
		}
		for(j = 0, fl = f.length; j < fl; j++) {
			temp = s[i] + '';
			repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
			s[i] = (temp).split(f[j]).join(repl);
			if(count && s[i] !== temp) {
				this.window[count] += (temp.length - s[i].length) / f[j].length;
			}
		}
	}
	return sa ? s : s[0];
}
