$(function(){
	isIE6 = (typeof document.body.style.maxHeight === "undefined") ? true : false;
	if (isIE6) ie6Modify();
	printy();
	modal();
	//scrollable();
	//fonty();
	flashy();
	carousel();
	formalize();
	scrollable();
	scrollableBig();
	more();
});
$(window).load(function(){
	sideMenu();
});
function scrollable() {
	
	if ($('#scrollable').length>0) {
		$('#scrollable').jScrollPane({scrollbarWidth:'8'});		
	}
}
function scrollableBig() {
	
	if ($('#big-scroll-content').length>0) {
		$('#big-scroll-content').jScrollPane({scrollbarWidth:'15'});		
	}
}
function printy() {
	$('a[href="#print-recipe"]').live('click',function(){
		window.print();
		return false;
	});
}
function sideMenu() {
	//alert($('#side-menu>ul>li>a.selected+ul').length)
	$('#side-menu>ul>li>a.selected').find('+ul').slideToggle();
	$('#side-menu>ul>li>a.selected').parent().find('>ul>li ul').toggle();
}
function formalize() {
	if($('#sign-up-form').length>0) {
		$('form#sign-up-form div#submit-btn button').hover(function(){
			$(this).toggleClass('hover');
		},function(){
			$(this).toggleClass('hover');
		});
		$("input[name='birthday']").mask("99/99/9999");
		$('#sign-up-form').submit(function(){
			$.post($(this).attr('action'),$(this).serialize(),function(data){
				if (data.complete==false) {
					$('#msg').removeClass('complete').html(data.msg).css('display','none').fadeIn('slow');
				} else {
					$('#msg').addClass('complete').html(data.msg).css('display','none').fadeIn('slow');
					$('input[type="text"]').val('');
					$('input[type="checkbox"]').each(function(){
						this.checked = 0;
					});		
				}
			},'json');
			return false;
		});
	}
}
function flashy() {
	if ($('#flash').length>0) {
		$('#flash').flash({
		    src: 'public/img/home-temp.swf',
		    width: 980,
		    height: 514,
			wmode: 'transparent'
		});
	}
}
function modal() {
	if (isIE6) {
		var top_img = new Image();
		var bottom_img = new Image();
		var print_btn = new Image();
		var x_btn = new Image();
		print_btn.src="/public/img/modal-print-btn.gif";
		x_btn.src="/public/img/modal-x-btn.gif";
		top_img.src="/public/img/modal-curve-top.png";
		bottom_img.src="/public/img/modal-curve-bottom.png";
	}
	$('.get-more-info a').click(function(){
		window.scroll(0,0);
		var newhtml = $(this).parent().next().html();
		$('<div id="modal-bg" />').appendTo('body');
		$('<div id="modal-content" />').appendTo('body');
		$('#modal-content').html('<div id="modal-content-container"><div id="mbox-top"></div><div id="mbox"><div class="close-btn-bg"><a href="#close" class="close-btn">Close</a></div><div class="print-btn-bg"><a href="#print" class="print-btn">Print</a></div><div id="modal-interior-content"><div class="pop-up-content"></div></div></div><div id="mbox-bottom"></div></div>');
		$('#modal-interior-content .pop-up-content').html(newhtml);
		$('#modal-interior-content .pop-up-content').css('display','block');
		$('#modal-bg').css('opacity','0.5');
		$('#modal-bg').height($('#all-contain').outerHeight({margin:true}));
		$('#modal-bg').toggle();
		$('#modal-content').toggle();
		$(window).resize(function(){
			$('#modal-bg').height($('#all-contain').outerHeight({margin:true}));
		});
		return false;
	});
	$('a.close-btn').live('click',function(){
		closeModal();
		return false;
	});
	$('a.print-btn').live('click',function(){
		$('#all-contain').addClass('no-print');
		window.print();
		return false;
	});
}
function closeModal() {
	$('#modal-bg').remove();
	$('#modal-content').remove();
}

function ie6Modify() {
	$('#credit-menu li:eq(0)').addClass('first-child');
	$('div#side-menu ul>li:first-child').css('background','transparent');
}
function flashy() {
	if ($('#flash').length>0) {
		$.fn.flash.update = function(htmlOptions) {
			if (isIE6) {
				var flash_bg = new Image();
				flash_bg.src="/public/img/home-noflash.png";
			}
			$('#flash').html('<div id="no-flash">No Flash</div>');
		};
		
		$('#flash').flash({
		    src: 'public/swf/sweet_goods_loader.swf',
		    width: 624,
		    height: 325,
			wmode: 'transparent'
		});
	}
}

function carousel() {
	if ($('.infiniteCarousel').length<1) return;
	
	$('.infiniteCarousel').infiniteCarousel();
	$('a.back', this).click(function () {
	    return gotoPage(currentPage - 1);                
	});
	$('a.forward', this).click(function () {
	    return gotoPage(currentPage + 1);
	});
	if(isIE6 || $('body').hasClass('ie7')) {
		$('.back').hover(function(){
			$(this).toggleClass('hover-back');
		},
		function(){
			$(this).toggleClass('hover-back');
		});
		$('.forward').hover(function(){
			$(this).toggleClass('hover-fwd');
		},
		function(){
			$(this).toggleClass('hover-fwd');
		});
	}
	$(this).bind('goto', function (event, page) {
	    gotoPage(page);
	});
	$('#carousel').css('visibility','visible');
	//
	
	$('div.left a').hover(
	function(){
		var new_href = $(this).attr('href');	
		//Set content of roll area
		var content = $(this).parent().find('div').html();
		$('#pos_left').html(content);
		//	
		$('#pos_left').css('visibility','visible');
		$('#pos_left_btn').css('visibility','visible');
		$('#pos_left_btn').css('cursor','pointer');
		//
		$('#pos_left_btn').click(function(){
			window.location=new_href;
		});
		//
		$('#pos_right_btn').css('visibility','hidden');
		$('#pos_right').css('visibility','hidden');
		$('#pos_mid_btn').css('visibility','hidden');
		$('#pos_mid').css('visibility','hidden');
		//
		$('#pos_left_btn').hover(null,function(){
			$('#pos_left_btn').css('visibility','hidden');
			$('#pos_left').css('visibility','hidden');
		});
	});
	
	////
	$('div.mid a').hover(
	function(){
		var new_href = $(this).attr('href');
		var content = $(this).parent().find('div').html();
		$('#pos_mid').html(content);
		$('#pos_mid').css('visibility','visible');
		$('#pos_mid_btn').css('visibility','visible');
		$('#pos_mid_btn').css('cursor','pointer');
		
		$('#pos_mid_btn').click(function(){
			window.location=new_href;
		});
		//
		$('#pos_left_btn').css('visibility','hidden');
		$('#pos_left').css('visibility','hidden');
		$('#pos_right_btn').css('visibility','hidden');
		$('#pos_right').css('visibility','hidden');
		//
		$('#pos_mid_btn').hover(null,function(){
			$('#pos_mid_btn').css('visibility','hidden');
			$('#pos_mid').css('visibility','hidden');
		});
	});

	
	//
	$('div.right a').hover(
	function(){
		var new_href = $(this).attr('href');
		var content = $(this).parent().find('div').html();
		$('#pos_right').html(content);
		$('#pos_right').css('visibility','visible');
		$('#pos_right_btn').css('visibility','visible');
		$('#pos_right_btn').css('cursor','pointer');
		$('#pos_right_btn').click(function(){
			window.location=new_href;
		});
		//
		$('#pos_left_btn').css('visibility','hidden');
		$('#pos_left').css('visibility','hidden');
		$('#pos_mid_btn').css('visibility','hidden');
		$('#pos_mid').css('visibility','hidden');
		//
		$('#pos_right_btn').hover(null,function(){
			$('#pos_right_btn').css('visibility','hidden');
			$('#pos_right').css('visibility','hidden');
		});
	});
	
	$('a.back', this).hover(function(){
		$('#pos_left_btn').css('visibility','hidden');
		$('#pos_left').css('visibility','hidden');
	});
}

function more(){
	$("a.more_link").live("click",function(){
	var targ = $(this).attr('href');
	var more_link = $(this).parent();
	$.post(targ,null,function(data){
		more_link.remove();
		$("ul#moments").append(data);
	},"html");
	return false;
	});
	
	
}
