﻿var previous = "images/mayagame_32.jpg";
var previousOn = "images/mayagame2_32.jpg";
var next = "images/mayagame_34.jpg";
var nextOn = "images/mayagame2_34.jpg";
var photos = ["images/focus1.jpg", "images/focus2.jpg", "images/focus3.jpg"];
var texts = ["《山寨外传》是一款独特的武侠社交游戏，在这里你将拥有属于自己的山寨，体验占山为王的感觉！你可以招募喽啰，训练他们练就一身武艺，下山劫富济贫，行走江湖！","", ""];
var hrefs = ["http://apps.renren.com/happyhill", "http://www.kaixin001.com/!app_heights/", "jobs.htm"];
var index = 0;
var switchObj = null;
$(document).ready(function () {
	$("#PreviousPhoto").mouseover(function () {
		changePhoto(this, previousOn);
	});
	$("#PreviousPhoto").mouseout(function () {
		changePhoto(this, previous);
	});
	$("#NextPhoto").mouseover(function () {
		changePhoto(this, nextOn);
	});
	$("#NextPhoto").mouseout(function () {
		changePhoto(this, next);
	});
	$("#PreviousPhoto").click(function () {
		playPhotos("desc");
	});
	$("#NextPhoto").click(function () {
		playPhotos("asc");
	});
	initFocusIndex();

	$("#Menu>a>img").mouseover(function () {
		changeImg(this, "mayagame", "mayagame2");
	});

	$("#Menu>a>img").mouseout(function () {
		changeImg(this, "mayagame2", "mayagame");
	});
	
	$("#Banner").hover(switchStop,switchStart);
	switchStart();
});

var switchStart = function(){
	switchObj = setInterval(function(){playPhotos("asc");},5000);
}
var switchStop = function(){
	if(switchObj != null){
		window.clearInterval(switchObj);
	}
}

var changeImg = function (img, name1, name2) {
	var src = $(img).attr("src");
	src = src.replace(name1, name2);
	$(img).attr("src", src);
}

var initFocusIndex = function () {
	for (var i = 0; i < photos.length; i++) {
		$("#FocusIndex").append("<a href=\"javascript:void(0);\" onclick=\"playPhotos(" + i + ");\">" + (i + 1) + "</a>");
	}
	$("#FocusIndex>a:eq(" + index + ")").addClass("FocusOn");
}

var changePhoto = function (imgcontainer, src) {
	$(imgcontainer).children("img:first").attr("src", src);
}

var playPhotos = function (order) {
	if (order == "asc") {
		asc();
	}
	else if (order == "desc") {
		desc();
	}
	else {
		index = order;
	}
	$("#FocusIndex>a").removeClass("FocusOn");
	$("#FocusIndex>a:eq(" + index + ")").addClass("FocusOn");
	$("#FocusPhoto>a>img").fadeOut("slow", function () {
		$("#FocusPhoto>a>img").attr("src", photos[index]);
		$("#FocusPhoto>a").attr("href", hrefs[index]);
		$("#FocusPhoto>a>img").fadeIn("slow");
		if($.trim(texts[index]) == ""){
			$("#FocusText").css("visibility","hidden");
		}
		else{
			$("#FocusText").css("visibility","display");
			$("#FocusText").text(texts[index]);
		}
	});
}
var asc = function () {
	index++;
	if (index >= photos.length) {
		index = 0;
	}
}
var desc = function () {
	index--;
	if (index < 0) {
		index = photos.length - 1;
	}
}

