/*!
 * cayenne.co.jp common.js
 * /themes/cayenne/common/scripts/jQuery.js
 *
 * Copyright 2010, Yuya Amano 
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Date: 2010/07/15
 */

(function($){

	$.fn.setInitAnimation = function(){
		var caFooterObj = $('#footerCayenne');
		//window jQuery object
		var windowObj = $(window);
		//documet jQuery object
		var winDocumentObj = $(document);
		//body jQuery object
		var winBodyObj = $('body');
		//get window height
		var windowHeight = windowObj.height() + windowObj.scrollTop();
				
		//set footer image OUTSIDE the window
		if(isIphone()){
			return;
		}
		
		caFooterObj.hide();
		caFooterObj.css({
			top : windowHeight,
			opacity : 0
		});		
		//hide scroll bar while the animation
		winBodyObj.css('overflow','hidden');
		//let the image to apper with fadein animation
		caFooterObj.show();
		caFooterObj.animate(
			{
				top : windowHeight - caFooterObj.height() + 'px',
				opacity : 1
			},
			1000,
			'',
			function(){
			
				//show the scroll bar
				winBodyObj.css('overflow','auto');
				
				if(!isPositionFixAvailable()){
					//if user scroll the window, stick the image bottom of the window
					windowObj.scroll(function(){
						eventScrollWindow();
					});
					windowObj.resize(function(){
						eventResizeWindow();
					});
				}else{
					caFooterObj.css({
						'position' : 'fixed',
						'top' : windowObj.height() - caFooterObj.height() + 'px'
					});
				}
			}
		);
		
		function eventResizeWindow()
		{
			windowHeight = windowObj.height();
			caFooterObj.css('top', windowHeight - 195 + 'px');
		}
		function eventScrollWindow()
		{
			windowHeight = windowObj.height() + windowObj.scrollTop();
			caFooterObj.css('top', windowHeight - 195 + 'px');			
		}
		function isPositionFixAvailable(){
			if($.browser.msie){
				if($.browser.version > 6.0){
					return false;
				}
			}
			return true;
		}
		function isIphone(){
			osType = (/(win|mac|linux|sunos|solaris|iphone|ipod|ipad)/.exec(navigator.platform.toLowerCase()) || [u])[0].replace('sunos', 'solaris');
			iphone = false;
			switch(osType){
				case 'iphone':
				case 'ipod':
				case 'ipad':
					return true;
					break;
			}
			return false;
		}
	};

})(jQuery);

//can not get scrollTop value right after 'ready' or 'onload' event. wait for 100msec.
$(window).load(function(){
	setTimeout(
		function(){
			$(document).setInitAnimation();
		}
	,100)
});




