(function($) {
        $.fn.fadeTransition = function(options) {
            // var options = $.extend({ pauseTime: null, transitionTime: null }, options);
            var transitionObject;

            Trans = function(obj) {
                var timer = null;
                var current = 0;
                var els = $("> *", obj).css("display", "none").css("left", "0").css("top", "0").css("position", "absolute");
                $(obj).css("position", "relative");
                $(els[current]).css("display", "block");

                function transition(next) {
                    $(els[current]).fadeOut();
                    $(els[next]).fadeIn();
                    current = next;
                    cue();
                };

                function cue() {
                    if ($("> *", obj).length < 2) return false;
                    //if (timer) clearTimeout(timer);
                    //timer = setTimeout(function() { transition((current + 1) % els.length | 0) }, options.pauseTime);
                };

                this.showItem = function(item) {
                    if (timer) clearTimeout(timer);
                    transition(item);
                };

                cue();
            }

            this.showItem = function(item) {
                transitionObject.showItem(item);
            };

            return this.each(function() {
                transitionObject = new Trans(this);
            });
        }

    })(jQuery);

    var page = {
        tr: null,
        init: function() {
            page.tr = $(".area").fadeTransition({ pauseTime: 7000, transitionTime: 2000 });
            $("div.navigation").each(function() {
                $(this).children().each(function(idx) {
                    if ($(this).is("a")){
												$(this).click(function() { page.tr.showItem(idx);return false; })
										}
                });
            });
        },

        show: function(idx) {
            if (page.tr.timer) clearTimeout(page.tr.timer);
            page.tr.showItem(idx);
        }
    };

// startup 
$(document).ready(function () {

	// Print buttons 

	//remove the last class from the last <li> in the 
	//top menu and add the print this page link
	//$('.graybar ul:first li:last').removeClass('last');
	//$('.graybar ul:first').append('<li class="last"><a href="javascript:;" title="Print This Page" class="print-this">Print This Page</a></li>');

	//add a print this page link to the footer
	if($('#footer').length >= 0){
		$('#footer ul.home').prepend('<li><a href="javascript:;" title="打印此页面" id="print" class="print-this">打印此页面</a></li>');
	}

	//add the print only logo
	if ($('.main').size() != 0) {
		//if main exists then put it there 
		$('.main').prepend('<img src="/images/print-logo.jpg" id="print-logo" width="144" height="32" style="display: none;" />');
	}else{
		//otherwise put it at the top of content
		$('#feature').prepend('<img src="/images/print-logo.jpg" id="print-logo" width="144" height="32" style="display: none;" />');
	}

	//add event listeners and bind function
	if($('.print-this').length >= 0){
		$('.print-this').click(function(){window.print();});
	}


	// language dropdown
	if($('#language').length >= 0){
		$('#language').superfish({
			delay		: 800,
			animation	: {opacity: 'show', height: 'show'},
			speed		: 300
		});
	}

	// search -- click to clear text
	if($('#search').length >= 0){
		$("#search input[name='q']").click(function () {
			if ($(this).val() == '搜索')
				$(this).val('');
		});
	}

	// home page filter
	if($('#filer').length >= 0){
		$('#filer > ul li a').attr('href', 'javascript:;');
		$('#filter > ul li a').click(function () {
			$('#filter > ul li a').attr('class', '');
			if ($(this).attr('cat').toLowerCase() == 'all') {
				$('#swap ul').show('fast');
			} else {
				$('#swap ul').hide('fast');
				$("#swap ul[class='" + $(this).attr('cat').toLowerCase() + "']").show('fast');
			}
			$(this).attr('class', 'selected');
		});
	}

	/*add class to last li on global nav*/
	$('#global-nav > li:last').addClass('nav-last');

	/* global nav dropdown */
	if($('#global-nav').length >= 0){
		$('#global-nav').superfish({
			delay		: 800,
			animation	: {opacity: 'show', height: 'show'},
			speed		: 300
		});
	}

	/* flash banner */
	$('#banner').flash({
    src: 'flash/banner_engine.swf',
    width: 960,
    height: 283,
		wmode: 'transparent'
		//flashvars: { foo: 'bar', baz: 'zoo' }
	});
	
	if($('#logos').length >= 0){
		$('#logos').innerfade({
					speed: 'slow',
					timeout: 3000,
					type: 'sequence'
					//containerheight: '220px'
		});
	}
	/*$('#banners').innerfade({
				speed: 'slow',
				timeout: 4000,
				type: 'sequence',
				containerheight: '281px'
	});*/
	if($(".area").length >= 0){
		page.init();
	}

	function centerObj(obj){
		var mtop = parseInt(($(window).height()-obj.height())/2);
		if( mtop < 0 ){
			mtop = 0;
		}
		var mleft = parseInt(($(window).width()-obj.width())/2);
		if( mleft < 0 ){
			mleft = 0;
		}
		obj.css('top',mtop);
		obj.css('left',mleft);
	}

	$(window).bind("resize", function(){
		centerObj($('#OnetoOneModal'));
	});
	centerObj($('#OnetoOneModal'));

	
});

