$(document).ready(function(){
	initSlide();
	initSlideinSlide();
});
$(window).load(function(){
	imgResize();
})
/*****/

function imgResize(){
	var _imgHold = $('.bg-body');
	var _ul = _imgHold.find('ul');
	var _li = _imgHold.find('li');
	var _imgs = _imgHold.find('img');
	if(_imgs.length < 2){
		if(!($.browser.msie && $.browser.version < 7)){
			_imgHold.css({
				position:'fixed'
			});
		}
		else{
			$(window).scroll(function(){
				_imgs.eq(0).css({marginLeft:$(window).scrollLeft()})
			});
		}
		_imgHold.css({overflow:'hidden'});
		if(_imgHold.length) recalc();
		if(_imgHold.length) $(window).resize(recalc);
		if(_imgs.length) imageStretcher.stretch(_imgs[0]);
	}
	else{
		initRes();
		$(window).resize(function(){
			initRes();
		})
	}
	function recalc(){
		var winW = $(window).width();
		var winH = $(window).height();
		_imgHold.css({
			height:winH
		});
		$('body').css({
			height:winH,
			position:'relative',
			overflowY:'hidden'
		});
	}
	function initRes(){
		var _width = 0;
		wh = $(window).height();
		_li.each(function(){
			var _this = $(this);
			initHeightResize(_this);
			_width += parseInt(_this.outerWidth(true));
			_ul.css({
				width: _width
			});
		});
		_ul.css({
			height: $(window).height()
		});
		$('body').css({
			overflowY:'hidden',
			height: $(window).height()
		})
	}
	var wh = 0;;

	function initHeightResize(obj){
		var _obj = obj;
		var _img = _obj.find('img');
		var _ratio = _img.attr('width')/_img.attr('height');
		//var _winH = $(window).height();
		_img.css({
			height:wh,
			width: Math.round(wh*_ratio)
		});
		_obj.css({
			height:wh,
			width: Math.round(wh*_ratio)
		});
	}
}

// image stretcher module
var imageStretcher = (function(){
	var windowWidth, windowHeight, images = [];
	function addImage(img) {
		recalcSize();
		images.push(img);
		img.onload = function(){
			getRatio(img, resizeImage);
			img.onload = null;
		}
		img.src = img.src; // IE Fix
	}
	function removeImage(img) {
		for(var i=0; i<images.length; i++) {
			if(images[i] === img) {
				images.splice(i,1);
				return;
			}
		}
	}
	function getRatio(img, cb) {
		img.style['msInterpolationMode'] = 'bicubic'; // IE7 fix
		var newImg = new Image();
		newImg.onload = function() {
			img.ratio = newImg.width / newImg.height;
			if(typeof cb === 'function') cb.apply(img)
			newImg.onload = null;
		}
		newImg.src = img.src;
	}
	function resizeImage(img) {
		var _ratio = this.ratio;
		var _slideWidth = windowWidth;
		var _slideHeight = _slideWidth/_ratio;
		if(_slideHeight < windowHeight) {
			_slideHeight = windowHeight;
			_slideWidth = _slideHeight * _ratio;
		}
		this.style.width = _slideWidth+'px';
		this.style.height = _slideHeight+'px';
		this.style.top = (windowHeight-_slideHeight)/2+'px';
		this.style.left = (windowWidth-_slideWidth)/2+'px';
	}
	function recalcSize() {
		windowWidth = getClientWidth();
		windowHeight = getClientHeight();
	}
	function handleResize() {
		recalcSize();
		for(var i=0; i<images.length; i++) resizeImage.apply(images[i]);
	}
	addHandler(window, 'resize', handleResize);

	// util functions
	function addHandler(object, event, handler) {
		if (typeof object.addEventListener != 'undefined') object.addEventListener(event, handler, false);
		else if (typeof object.attachEvent != 'undefined') object.attachEvent('on' + event, handler);
	}
	function getClientWidth(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientWidth'] || window.document.body['clientWidth']}
	function getClientHeight(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientHeight'] || window.document.body['clientHeight']}

	// methods
	return {
		stretch: addImage,
		unstretch: removeImage
	}
})()
/****/
/* Redirect function */
/*function initRedirect(){
	var time_before_redirect = 5500;
	var _cookieName = 'redirect';
	if(getCookie(_cookieName)) time_before_redirect=0;
	if($('#placeholder-load').length){
		_time = setTimeout(function(){
			window.location=$('#location-indicator').attr("href");
		}, time_before_redirect);
	}
	setCookie(_cookieName, 0, 60, '/');
}*/

/***/
/* Coockies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function initSlide(){
	var wrappers = $('.slide-block');
	var speed = 400;
	var open_class = 'open';
	wrappers.each(function(){
		var wrapper =$(this);
		var opener = wrapper.find('.title>.open-close');
		var slide_block = wrapper.find('.block');
		if (!wrapper.hasClass(open_class)) {
			slide_block.css({
				display: 'none'
			});
		}
		opener.click(function(){
			if(wrapper.find('.block').length){
				if (!wrapper.hasClass(open_class)) {
					slide_block.slideDown(speed , function(){
						wrapper.addClass(open_class);
					});
				}
				else {
					slide_block.slideUp(speed , function(){
						wrapper.removeClass(open_class);
					});
				}
				return false;
			}
		});
	});
}

function initSlideinSlide(){
	var wrappers = $('.slide-block .block>ul>li:has("a.open-close")');
	var speed = 400;
	var open_class = 'open';
	wrappers.each(function(){
		var wrapper = $(this);
		var opener = wrapper.find('>.open-close');
		var slide_block = wrapper.find('>ul');
		var slide_H = slide_block.outerHeight(true);
		if (!wrapper.hasClass(open_class)) {
			slide_block.css({
				display: 'none'
			});
		}
		opener.click(function(){
			if (!wrapper.hasClass(open_class)) {
				slide_block.slideDown(speed , function(){
					wrapper.addClass(open_class);
				});
			}
			else {
				slide_block.slideUp(speed , function(){
					wrapper.removeClass(open_class);
				});
			}
			return false;
		});
	});
}

