//JQUERY COOKIE
jQuery.jcookie=function(d,e,b){if(arguments.length>1&&(e===null||typeof e!=="object")){b=jQuery.extend({},b);if(e===null){b.expires=-1}if(typeof b.expires==="number"){var g=b.expires,c=b.expires=new Date();c.setDate(c.getDate()+g)}return(document.cookie=[encodeURIComponent(d),"=",b.raw?String(e):encodeURIComponent(String(e)),b.expires?"; expires="+b.expires.toUTCString():"",b.path?"; path="+b.path:"",b.domain?"; domain="+b.domain:"",b.secure?"; secure":""].join(""))}b=e||{};var a,f=b.raw?function(h){return h}:decodeURIComponent;return(a=new RegExp("(?:^|; )"+encodeURIComponent(d)+"=([^;]*)").exec(document.cookie))?f(a[1]):null};

if(typeof(dressColour) == "undefined"){
	var dressColour = "";
}

if(typeof(ellaWaving) == "undefined"){
	var ellaWaving 	="";
	
}

function replacePNG(){
	$('img').each(function(){
		pngSRC = $(this).attr('src').split('.png');
		pngHeight = $(this).height();
		pngWidth = $(this).width();
		if(pngSRC.length > 1){
			$(this).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr('src') + "', sizingMethod='crop')")
			$(this).attr('src', '/static/images/blank.gif').attr('width', pngWidth).attr('height', pngHeight);
		}
	});
}

function faqInit(){
	$('.faqType li div').hide();
    $('.faqType li').each(function(e){
        $(this).hover(
            function(){
                $(this).css('background','#F2F2F2');
            },
            function(){
                if($(this).attr('class') != 'faqOn'){
                    $(this).css('background','none');
                }
            }
        );
        $(this).find('span:first').click(function(){
            var theFAG = $('.faqOn div:visible'); 
            $('.faqOn div:first').hide(400);
            $('.faqOn').css('background','none');
            $('.faqOn').removeClass('faqOn');
            $(this).parent().css('background','#F2F2F2');
            //$(this).css('background','url(images/arrows_small.png) 0 -79px no-repeat');
            $(this).parent().addClass('faqOn');
            $(this).next('div:first').not($(theFAG)).show(400);
        })
    });
}

function createScroll(){
	if($("#mainContents").hasClass(".giving")){
		$("#mainContents").removeClass("scroll1 scroll2").css("height", "470px");
		$('.scroll-pane').jScrollPane({dragMaxHeight:26, scrollbarWidth:43, scrollbarMargin:20, reinitialiseOnImageLoad:true});
		$(".jScrollPaneContainer").css("height", "490px");
		return false;
	}
	if($('#topBlock').size() <= 0){
		if($('#mainContents').height() >= 540){
			$('#mainContents').removeClass('scroll1').addClass('scroll2');
			//isSafari3 = false;
			//if(window.devicePixelRatio){
				//isSafari3 = true;
			//}
			//if(isSafari3 == false){
				$('.scroll-pane').jScrollPane({dragMaxHeight:30, scrollbarWidth:24, scrollbarMargin:10, reinitialiseOnImageLoad:false});
			//}
		}
		if($('.awards #mainContents').height() >= 350){
			$('#mainContents').removeClass('scroll1').addClass('scroll2');
			$('.scroll-pane').jScrollPane({dragMaxHeight:55, scrollbarWidth:52, scrollbarMargin:0, reinitialiseOnImageLoad:false});
		}
		
		if($('#stretchBox #mainContents').height() >= 390){
			$('#mainContents').removeClass('scroll1').addClass('scroll2');
			$('.scroll-pane').jScrollPane({dragMaxHeight:30, scrollbarWidth:24, scrollbarMargin:10, reinitialiseOnImageLoad:false});
		}
		
		
		
		if($('#story #mainContents').height() >= 290){
			$('#mainContents').removeClass('scroll1').addClass('scroll2');
			$('.scroll-pane').jScrollPane({dragMaxHeight:30, scrollbarWidth:24, scrollbarMargin:10, reinitialiseOnImageLoad:false});
		}
		
		
		
		if($('#formInner').height() >= 440){
			$('#formInner').removeClass('scroll1').addClass('scroll2');
			$('.scroll-pane').jScrollPane({dragMaxHeight:46, scrollbarWidth:43, scrollbarMargin:20, reinitialiseOnImageLoad:true});
		}
	}
	else{
		//var contentSize = $('#topBlock').outerHeight() + $('#midBlock').outerHeight() + $('#btmBlock').outerHeight();
		//if(contentSize >= 520){
		if($('#mainContents').height() >= 540){
			$('#topBlock').prependTo('#mainContentWrap');
			$('#btmBlock').appendTo('#mainContentWrap');
			$('#mainContents').removeClass('scroll1').addClass('scroll2');
			var newHeight = 489 - $('#topBlock').outerHeight() - $('#btmBlock').outerHeight();
			$('#mainContents').css('height', newHeight + 'px');
			if($('.recipeUploadForm').size() > 0){
				$('.scroll-pane').jScrollPane({dragMaxHeight:80, scrollbarWidth:53, scrollbarMargin:10, reinitialiseOnImageLoad:false});
			}
			else if($('.becomeAFriendForm').size() > 0){
				$('.scroll-pane').jScrollPane({dragMaxHeight:46, scrollbarWidth:43, scrollbarMargin:10, reinitialiseOnImageLoad:false});
			}
			else{
				$('.scroll-pane').jScrollPane({dragMaxHeight:30, scrollbarWidth:24, scrollbarMargin:10, reinitialiseOnImageLoad:false});
			}
		}
	}
}

function trackProduct(page){
	pageTracker._trackPageview('/RightNavigationBarClick');
	document.location.href = 'http://www.ellaskitchen.co.uk' + page;
}

function wwifr(){
	replaceText('h1', 'ellas', '/static/flash/', '', '', '', '', '', '-3', '1.4');
	replaceText('h2', 'ellas', '/static/flash/', '', '#FFFFFF', '#c60001', '', '', '-3', '1.4');
	replaceText('h3', 'ellas', '/static/flash/', '', '', '', 'underline', '', '-3', '1.4');
	replaceText('.rightbox', 'ellas', '/static/flash/', 'bold', '', '', '', '', '', '', '', '-3', '1.4');
}

function dropDowns(){
	$('.mySelect').hide().each(function(e){
		$('<div class="selectMenu" style="z-index:' + (100 - e) + ';"><span>' + this.options[ this.selectedIndex ].text + '</span></div>').insertBefore($(this));		
		$(this).parent().find('.selectMenu').append('<div class="selectMenuList" style="z-index:' + (100 - e) + ';"><div><ul></ul></div></div>');
		$(this).find('option').not(':first').each(function(){
			$(this).parent().parent().find('ul').append('<li>' + $(this).text() + '</li>');
		});
	});
	$('.selectMenu span').click(function(){
		$('.selectMenuList').hide();
		$(this).parent().find('.selectMenuList').show();
	})
	$('.selectMenuList li').not('.selectMenuList li:contains("_")').click(function(){
		$(this).parent().parent().parent().parent().find('span').text($(this).text());
		$(this).parent().parent().parent().hide();
		
		var index	= $( this ).prevAll().length;
		var $select = $(this).parent().parent().parent().parent().parent().find('select')[0];
		
		$select.selectedIndex = index + 1;
		$( $select ).change();

	});
	$('.selectMenu').mouseleave(function(){
		$(this).find('.selectMenuList').hide();
	});
}

var sounds = $.jcookie('sounds');
function initSounds(){
	if((sounds == undefined || sounds == null || sounds == "true" || sounds == true) && $('#flashGame').size() == 0){
		sounds = "true";
		$('#flags tr:first').prepend('<td width="85"><img class="sounds on" src="/static/images/sound.gif" /></td>');
		$('#flags tr:last').prepend('<td>sound on</td>');
	}
	else if($('#flashGame').size() == 0){
		sounds = "false";
		$('#flags tr:first').prepend('<td width="85"><img class="sounds off" src="/static/images/sound.gif" /></td>');
		$('#flags tr:last').prepend('<td>sound off</td>');
	}
	else{
		sounds = "false";
	}
	$('.sounds').click(function(){
		if(sounds == "true"){
			sounds = "false";
			$('#flags .sounds').removeClass('on').addClass('off');
			$('#flags tr:last td:first').text('sound off');
		}
		else if(sounds == "false"){
			sounds = "true";
			$('#flags .sounds').removeClass('off').addClass('on');
			$('#flags tr:last td:first').text('sound on');
		}
		$.jcookie('sounds', sounds, {path: '/'});
		$('#background').find('object').flash(function(){
			this.SetVariable('soundon', sounds);
		});
	});
}

function tracking(){
	var loc = window.location.search;
	if(loc=="?bn=true"){
		pageTracker._trackEvent("Buy Now", "Carousel");
	}
}

$(window).load(function(){
	tracking();
});


$(document).ready(function(){
	bubbleHeight = $('#bubble div').height() + 42 + 'px';
	$('#bubble img').height(bubbleHeight);
	$('#bubble').height(bubbleHeight);
	$("input[type=file]").filestyle({ 
		image: "/static/images/browse.gif",
		imageheight : 31,
		imagewidth : 65,
		width : 150
 	});
	$('.file').wrap('<div class="textfield5"></div>');
	dropDowns();
	createScroll();
	initSounds();
	if(typeof(animations) == 'undefined'){
		animations = 'on';
	}
	$('#background').flash({swf:'/static/flash/clouds.swf', width:'100%', height:'768', wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{sun:sun, animations:animations, soundon:sounds}});
	$('#planeDiv').flash({swf:'/static/flash/planebanner.swf', width:'100%', height:'100', wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#girl').flash({swf:'/static/flash/ellas_body.swf', width:134, height:208, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{colour:dressColour, waving:ellaWaving}});
	$('#girl2').flash({swf:'/static/flash/ellas_body.swf', width:134, height:208, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{colour:dressColour, waving:ellaWaving}});
	$('#girl3').flash({swf:'/static/flash/ellas_body.swf', width:134, height:208, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{colour:dressColour, waving:ellaWaving}});
	$('#girl4').flash({swf:'/static/flash/ellas_body.swf', width:134, height:208, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{colour:dressColour, waving:ellaWaving}});
	$('#girl5').flash({swf:'/static/flash/ellas_body.swf', width:134, height:208, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{colour:dressColour, waving:ellaWaving}});
	$('#girlnpd').flash({swf:'/static/flash/ellas_body.swf', width:134, height:208, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{colour:dressColour, waving:ellaWaving}});
	$('#funStuff').flash({swf:'/static/flash/fun_stuff.swf', width:1090, height:500, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#danmark').flash({swf:'/static/flash/danmark.swf', width:35, height:30, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#norge').flash({swf:'/static/flash/norge.swf', width:35, height:30, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#sverige').flash({swf:'/static/flash/sverige.swf', width:35, height:30, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#uae').flash({swf:'/static/flash/uae.swf', width:35, height:30, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#uk').flash({swf:'/static/flash/uk.swf', width:35, height:30, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	$('#usa').flash({swf:'/static/flash/us.swf', width:35, height:30, wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{}});
	faqInit();
	if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        var ieversion=new Number(RegExp.$1)
        if(ieversion < 7){
            replacePNG();
        }
    }
});

window.onload=function(){
	wwifr();
}

function dev(){
	if(document.location.hash == "#dev"){
		$("img[src='/media/96/2396-fourthumb.png']").attr("src", "/static/images/npd/organic1-from4months-small.png");
		$("img[src='/static/images/npd/stageonefour.gif']").attr("src", "/static/images/npd/organic1-from4months.png");
		$("img[src='/media/95/2395-sixthumb.png']").attr("src", "/static/images/npd/organic1-from6months-small.png");
		$("img[src='/static/images/npd/stageonesix.gif']").attr("src", "/static/images/npd/organic1-from6months.png");
	}
}

$(document).ready(function() {
   dev();
	//Select all anchor tag with rel set to tooltip
	$('a[rel=tooltip]').mouseenter(function(e) {
		
		
		
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');	
		
		if(tip != '' || tip != null){
			//Remove the title attribute's to avoid the native tooltip from the browser
			$(this).attr('title','');
		
			//Append the tooltip template and its value
			$(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');		
		}
		
			//Show the tooltip with faceIn effect
		$('#tooltip').fadeIn('500');
		$('#tooltip').fadeTo('10',0.9);
		
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', '50px' );
		$('#tooltip').css('left', '-30px' );
		$(this).parent().css('z-index', '999999' );		
		
	}).mouseleave(function() {
		
		if($('.tipBody').size() > 0){
			//Put back the title attribute's value
			$(this).attr('title',$('.tipBody').html());
		}
	
		//Remove the appended tooltip template
		$(this).children('div#tooltip').remove();
		
		$(this).parent().css('z-index', '1' );	
		
	});

});


