﻿var interval = 4000;
var cid = 0;
var timer = 0;
var imgw = 841;
var cPos = 0;
var moving = false;
var scrollt = 0;
var phace = 0;
$(document).ready(function(){
	
	$('.navitem a').click(function(){
		$.easing.def = 'easeOutQuad';
		var loc = $(this).attr('href');
		var p = $(loc).offset().top;
		moving = true;
		$('html,body').animate({scrollTop: $(loc).offset().top},'slow', function(){
//
			var wt = $(window).scrollTop();
			var nv = (wt-cPos)+5;
			if (wt>=cPos){
				nv = (wt-cPos)+5;
				$('.nav').animate({top: cPos+nv}, 500);
			} else {
				$('.nav').animate({top: cPos}, 300);
			}
			moving = false;
//
		});
		return false;
	});


// load images
	var img1 = new Image();
	var img2 = new Image();
	var img3 = new Image();
	var imgholder = $('.img_items');
	$(img1).load(function(){
		var toAdd1 = $('<div class="img_item"></div>');
		$(toAdd1).append(img1);
		$(imgholder).append(toAdd1);
		$(img2).load(function(){
			var toAdd2 = $('<div class="img_item"></div>');
			$(toAdd2).append(img2);
			$(imgholder).append(toAdd2);
			$(img3).load(function(){
				var toAdd3 = $('<div class="img_item"></div>');
				$(toAdd3).append(img3);
				$(imgholder).append(toAdd3);
				$('.img_main').slideToggle(300, function(){
					timer = setInterval(function(){
						slideImg();
					}, interval);					
				});
				var nv = 240+$('.nav').offset().top+15;
				$('.nav').animate({'top':nv}, 300)
				cPos = nv;
			}).attr('src', imgx[0]);
		}).attr('src', 'images/wel.jpg');
	}).attr('src', imgx[imgx.length-1]);
// / load image


	$('.menuItem').click(function(){
		var href = $(this).children('a').attr('href');
		document.location = href;
	});
});

function scrollNav(){
	if (moving){
		phace = 0;
		clearInterval(scrollt);
		moving = false;
		scrollt = setInterval(function(){scrollNav();}, 500);
	}
	phace++;
	if (phace >= 3){
		doNavScroll();
	}
}

function doNavScroll(){
	var wt = $(this).scrollTop();
	var nv = (wt-cPos);
	if (wt >= cPos){
		var nv = (wt-cPos)+5;
		$('.nav').animate({top: cPos+nv}, 300);
	} else {
		$('.nav').animate({top: cPos}, 300);
	}	
	clearInterval(scrollt);
}

function slideImg(){
	var img = new Image();
	cid++;
	if (cid >= imgx.length) cid = 0;
	var ciid = cid + 1;
	$(img).load(function(){
		var imgholder = $('.img_items');
		var toAdd = $('<div class="img_item"></div>');
		$(toAdd).append(img);
		$(imgholder).append(toAdd);
		var l = imgw * 2;
		$.easing.def = 'easeInOutBack';
		var igx = new Image();
			$(imgholder).animate({'left':-l}, 2000, function(){
				var pre = $(imgholder).children().first();
				$(pre).remove();
				$(imgholder).css({'left':-imgw});
			});
	}).attr('src', imgx[cid]);
}

function _rs() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

