// JavaScript Document

$(document).ready(function() {
	var sText = '';
	$('.searchHolder .submit').hide();
	$('.searchHolder .text').wrap('<span></span>').val(sText).click(function() {
		if($(this).val() == sText) {
			$(this).val('');
		}
		$('.searchHolder span.extra').animate({width:'0px'}, 'swing', function() {
			$(this).prev().children().removeClass('sampleText');
			$(this).next().show();
		});
	});
	$('.searchHolder span').after('<span class=extra></span>');
	$('.sideMenu, .sideModuleHolder').each(function() {
	//	$(this).html('<div class=lmiddle>'+$(this).html()+'</div>').prepend('<div class=ltop style=cursor:move title="click and drag to discover the background"></div>').append('<div class=lbottom></div>');
	});
	if($('.layoutContent').length == 2) {
		$('.layoutContent:first').find('.lbottom').addClass('lbottom_splash');
	}
	$('.layoutLeft .sideMenu, .layoutLeft .sideModuleHolder, .layoutLeft .sideHistoryCookbook').draggable({
		handle: '.ltop',
		revert: true,
		start: function() {
			$(this).css({zIndex: '1'}).siblings().css({zIndex: '0'});
		}
	});
	$('.mainMenu ul li a').each(function() {
		var width = $(this).width();
		if(width%2) {
			$(this).css('width', $(this).width()+1)
		}
		$(this).prepend('<div class=nw></div><div class=ne></div>');
	}).click(function() {
		$(this).addClass('selected').parent('li').siblings().children('a').removeClass('selected');
	});
	$("a[rel^='lightbox']").prettyPhoto({
		allowresize: false
	});
	$(".msg_row:odd td").css("backgroundColor","#ffffff");
	$(".msg_head").each(function(){
		$(this).parent('td').click(function(){
			//$(this).parents("tr").siblings().children("td").children(".msg_body").slideUp();
			$("#"+$(this).children('div:first').attr('rel')).slideToggle();
		});
	});
	// if($("#recipesList").length > 0) {
	// loadRecipes();
	// }
	$('.cookingBox').each(function() {
		var width = $(this).width();
		if(width%2) {
			$(this).css('width', $(this).width()+1)
		}
		$(this).children('h3:first').prepend('<span class=nw></span><span class=ne></span>');
		$(this).append('<span class=sw></span><span class=se></span>');
	});
	$('.imageHolder').each(function() {
		var width = $(this).find('img').width();
		$(this).width(width);
	})
	$('.imgOver').each(function() {
		var img_src = $(this).children('img').attr('src');
		var img_height = $(this).children('img').height();
		$(this).prepend('<div style=display:none;position:absolute;top:50%;left:50%;cursor:pointer; class=playBtn></div>');
		var width = $(this).children('.playBtn').width()/2;
		var height = $(this).children('.playBtn').height()/2;
		$(this).children('.playBtn').css({marginLeft: '-'+width+'px', marginTop: '-'+height+'px'})
		$(this).mouseover(function() {
			$(this).children('.playBtn').show()
		}).mouseout(function() {
			$(this).children('.playBtn').hide()
		});
	});
	$('.listOver').each(function() {
		var img_src = $(this).children('img').attr('src');
		var img_height = $(this).children('img').height();
		$(this).css({float: 'none', marginRight: '0px', backgroundImage: 'url('+img_src+')', backgroundRepeat: 'no-repeat', backgroundPosition: '50%',  height: img_height}).children('img').hide();
		$(this).prepend('<div style=display:none;position:absolute;top:50%;left:50%;cursor:pointer; class=playBtn></div>');
		var width = $(this).children('.playBtn').width()/2;
		var height = $(this).children('.playBtn').height()/2;
		$(this).children('.playBtn').css({marginLeft: '-'+width+'px', marginTop: '-'+height+'px'})
		$(this).mouseover(function() {
			$(this).children('.playBtn').show();
		}).mouseout(function() {
			$(this).children('.playBtn').hide();
		});
	});
	$('.yellowBoxList li').each(function() {
		$(this).not('.selected').children('.image').hide();
		$(this).children('a:first').click(function() {
			$(this).addClass('likeHeader').parents('li').siblings().find('.btn').removeClass('likeHeader');
			$(this).parents('li').siblings().find('.captionOverText').hide();
			$(this).next().slideDown('fast', function() {
				$(this).find('.playBtn').css({top: '40%'});
				$(this).find('.captionOverText').slideDown();
			});
			$(this).parents('li').siblings().children('.image').hide('fast');
		})
	});
	// $('.hoverInfo').prepend('<div class=hoverInfoHolder style=height:'+$('.hoverInfo').find('img').height()+'px><div class=hoverInfoText style=display:none><div>'+$('.hoverInfo').attr('title')+'</div></div><div class=hoverInfoArea></div></div>');
	$('.captionHover, .captionStatic').each(function() {
		$(this).prepend('<div class=captionOverHolder style=height:'+$(this).find('img').attr('height')+'px><div class=captionOverText style=display:none><div>'+$(this).attr('title')+'</div></div><div class=captionOverArea></div></div>');
	});
	$('.captionHover .captionOverArea').mouseover(function() {
		$(this).prev('.captionOverText').slideDown('fast');
	}).mouseout(function() {
		$(this).prev('.captionOverText').slideUp('fast');
	});
	// this function will collapse first li
	$('.yellowBoxList li:first').each(function() {
		$(this).children('a:first').addClass('likeHeader');
		$(this).children('a:last').show().find('.captionOverText').show();
	})
	$('.internal_tabs_btns table tr td a').click(function() {
		if($('#recipesList .pleaseWait').length == 0) {
			var cat;
			var string = splitHash();
			if(string[1] > 0) cat = string[1];
			var filter = $(this).attr('rel');
			$(this).attr('href','#/'+filter);
			if(cat) {
				$(this).attr('href','#/'+filter+'/'+cat);
				filter = filter+'&cat='+cat;
			}
			loadRecipes('filter='+filter);
			$(this).removeClass('tabs_link').addClass('tabs_nolink').parents('td').addClass('active').siblings().removeClass('active').find('a').removeClass('tabs_nolink').addClass('tabs_link');
		}
	});
	var t;
	$("#cookingSlider li").show();
	$("#cookingSlider").prepend('<div class=title>&nbsp;<span style=display:none></span>&nbsp;</div>').find('li a').mouseover(function() {
		clearTimeout(t);
		$(this).parents('ul').prev().prev().find('span').html($(this).attr('title')).fadeIn();
		$(this).css({opacity: '1'});
		$(this).parents('li').siblings().children('a').css({opacity: '0.2'});
	}).mouseout(function(a) {
		t = setTimeout("$('#cookingSlider li').children('a').animate({opacity: '1'}, 'fast');$('#cookingSlider .title span').fadeOut();", 200);
	});
	$('.fruitHolder').prepend('<div class=nw></div><div class=ne></div><div class=sw></div><div class=se></div>');
});

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function splitHash() {
	var string = location.hash.slice(2).split("/");
	return string;
}

function loadRecipes(filter) {
	if($('#recipesList .pleaseWait').length == 0) {
		var filter;
		var cat;
		if(filter) {
			filter = '&'+filter;
		} else {
			var string = splitHash();
			if(string[1] > 0) cat = '&cat='+string[1];
			else cat = '';
			if(string[0] || string[1]) {
				filter = '&filter='+string[0]+cat;
				$('.internal_tabs_btns table tr td a').each(function() {
					if($(this).attr('rel') == string[0]) {
						$(this).parents('td').addClass('active').siblings().removeClass('active').find('a').removeClass('tabs_nolink').addClass('tabs_link');
					}
				});
			} else
				filter = '';
		};
		$('#recipesList').wrapInner('<div class=loadingOpacity></div>');
		$('.loadingOpacity').css('opacity', '0.2');
		$('#recipesList').prepend('<strong class=pleaseWait>loading...</strong>');
		$.ajax({
			beforeSend: function() {
				t = setTimeout("$('.pleaseWait').append(' <strong class=pleaseWait>still loading, please be patient... or <a href=>reload</a> the page</strong>')", 5000)
			},
			type: "POST",
			url: "index.php",
			data: "load=list"+filter,
			success: function(html) {
				clearTimeout(t);
				$("#recipesList").html('').html(html);
			}
		});
	}
}