function embedFlash1(){
	var flashvars = {};
	flashvars.resourceURL="lib/resources/athome";
	flashvars.section="athome.swf";
	flashvars.id="3";
	flashvars.trackId="allergies/";
	var params = {};
	params.allowFullScreen="false"
	params.allowscriptaccess="always"
	params.quality="high";	
	params.scale =  "noscale";
	params.wmode="transparent";
	params.bgcolor = "#FFFFFF";
	var attributes = {};
	
	$("#allergies-at-home-container").flash({
		swf: '/swf/peds/assembler.swf',
		params: params,
		flashvars: flashvars, 
		width:932,
		height:540,
		hasVersion: 9.0
	});
};

function embedFlash2(){
	var flashvars = {};
	flashvars.resourceURL="lib/resources/help";
	flashvars.section="help.swf";
	flashvars.id="4";
	flashvars.trackId="help/";
	var params = {};
	params.allowFullScreen="false"
	params.allowscriptaccess="always"
	params.quality="high";	
	params.scale =  "noscale";
	params.bgcolor = "#FFFFFF";
	params.wmode="transparent";
	var attributes = {};
	
	$("#more-ways-to-help-container").flash({
		swf: '/swf/peds/assembler.swf',
		params: params,
		flashvars: flashvars, 
		width:932,
		height:540,
		hasVersion: 9.0
	});
};

function embedFlash3(){
	var flashvars = {};
	flashvars.resourceURL="lib/resources/learn";
	flashvars.section = "learn.swf";
	flashvars.id="5";
	flashvars.trackId="learn/";
	var params = {};
	params.allowFullScreen="false"
	params.allowscriptaccess="always"
	params.quality="high";	
	params.scale =  "noscale";
	params.bgcolor = "#FFFFFF";
	params.wmode="transparent";
	var attributes = {};

	$("#fun-ways-to-learn-container").flash({
		swf: '/swf/peds/assembler.swf',
		params: params,
		flashvars: flashvars, 
		width:932,
		height:540,
		hasVersion: 9.0
	});
};

function embedFlash4(){
	var flashvars = {};
	flashvars.resourceURL="lib/resources/profiler";
	flashvars.section = "profiler.swf";
	flashvars.id="6";
	flashvars.trackId="profiler/";
	var params = {};
	params.allowFullScreen="false"
	params.allowscriptaccess="always"
	params.quality="high";	
	params.scale =  "noscale";
	params.bgcolor = "#FFFFFF";
	params.wmode="transparent";
	var attributes = {};

	$("#allergy-profiler-container").flash({
		swf: '/swf/peds/assembler.swf',
		params: params,
		flashvars: flashvars, 
		width:932,
		height:540,
		hasVersion: 9.0
	});
};

$(function() {
	
	try{
		
		$.flashExpressInstaller = '/swf/peds/expressInstall.swf';
		
		if($.hasFlashPlayerVersion(9.0)){
			$("#ContentWrapper").addClass("hasFlashContent");
		}
		
		embedFlash1();
		embedFlash2();
		embedFlash3();
		embedFlash4();
		
		$('FORM#kidsProfilerForm SELECT').styledDropDown();
		
		$("FORM#kidsProfilerForm :input").addClass("pageRequired");
		
		var currentProfilerPage = "#kidsProfilerPartOne";
		
		$.validator.addMethod("pageRequired", function(value, element) {
			var $element = $(element);
			function match(selector) {
				return currentProfilerPage == selector && $(element).parents(selector).length;
			}
			if (match("#kidsProfilerPartOne") || match("#kidsProfilerPartTwo")) {
				return !this.optional(element);
			}
			return "dependency-mismatch";
		}, $.validator.messages.required);
		
		var validator = $("#kidsProfilerForm").validate({
			errorPlacement: function(error, element) {},
			highlight: function(element, errorClass) {
				$(element).parents(".questionContainer").addClass(errorClass);
			},
			unhighlight: function(element, errorClass) {
				$(element).parents(".questionContainer").removeClass(errorClass);
			},
			submitHandler:function(form){
				if(!$(form).hasClass('disabled')){						
					$(form).addClass('disabled');
					$(form).ajaxSubmit({
						dataType:  'json', 
						success:   function (result) {
							$(form).removeClass('disabled');
							$.log(result);
							if(result.result != false){
								var profilerResult = result.result;
								if(profilerResult.recommenedProducts.length>0){
									$("#productContainer1").find(".productTitle").html(profilerResult.recommenedProducts[0].name);
									
									//$("#productContainer1").find(".productTitleline").html(profilerResult.recommenedProducts[0].titleLine);
									$("#productContainer1").find(".productDescription").html(profilerResult.recommenedProducts[0].shortDesc);
									$("#productContainer1").find("A.recoProdUrl").attr("href","./../products/"+profilerResult.recommenedProducts[0].id+".jspa");
									$("#productContainer1").find("A.recoProdsUrl").attr("href","./../products/index.jspa#"+profilerResult.recommenedProducts[0].category);
                                    $("#productContainer1 .productTitleline, #productContainer1 .productDescription").find("SUP.footnote").footnotes(function(fnotes){
                                        var footnotesHtml = ""
                                        for (var i = 0; i < fnotes.length; i++) {
                                            footnotesHtml += "<sup>" + fnotes[i].index + "</sup>" + fnotes[i].title + "<br/>";
                                        }
                                        $("#productContainer1").find(".footNote").html(footnotesHtml);
                                    });
									
									$("#productContainer1").find(".productImageContainer IMG")[0].src = profilerResult.recommenedProducts[0].thumbLargeURL;
									$("#productContainer1").find(".productImageContainer IMG").reflect( {	height :.25, opacity :.25 });
								}
								if(profilerResult.recommenedProducts.length>1){
									$("#productContainer2").find(".productTitle").html(profilerResult.recommenedProducts[1].name);
									
									//$("#productContainer2").find(".productTitleline").html(profilerResult.recommenedProducts[1].titleLine);
									$("#productContainer2").find(".productDescription").html(profilerResult.recommenedProducts[1].shortDesc);
									$("#productContainer2").find("A.recoProdUrl").attr("href","./../products/"+profilerResult.recommenedProducts[1].id+".jspa");
									$("#productContainer2").find("A.recoProdsUrl").attr("href","./../products/index.jspa#"+profilerResult.recommenedProducts[1].category);
                                    $("#productContainer2 SUP.footnote").footnotes(function(fnotes){
                                        var footnotesHtml = ""
                                        for (var i = 0; i < fnotes.length; i++) {
                                            footnotesHtml += "<sup>" + fnotes[i].index + "</sup>" + fnotes[i].title + "<br/>";
                                        }
                                        $("#productContainer2").find(".footNote").html(footnotesHtml);
                                    });
									
									$("#productContainer2").find(".productImageContainer IMG")
										.attr('src',profilerResult.recommenedProducts[1].thumbLargeURL)
										.reflect( {	height :.25, opacity :.25 });

								}
								if(profilerResult.tips.length>0){
									$("#profilerTipContainer1").find(".tipText").html(profilerResult.tips[0].text).truncate(110);
									var selectedIndex = $("#state")[0].selectedIndex;
									$.log(selectedIndex);
									if(selectedIndex>0)
										$("DIV#regionalTipImage").spritify(1,selectedIndex-1);
								}
								if(profilerResult.recommenedProducts.length>1){
									$("#profilerTipContainer2").find(".tipText").html(profilerResult.tips[1].text).truncate(110);
									$("#profilerTipContainer2 IMG").attr('src',profilerResult.tips[1].imageUrl);
								}
								$("#profiler").hide();
								$("#profilerResults").show();
							}
						}
					});
				}
			}
		});
		
		$("#nextButtonProfiler").click(function(){
			if(validator.form()){
				$("#kidsProfilerPartTwo").show();
				currentProfilerPage = "#kidsProfilerPartTwo";
			}
			return false;
		});

		
		// Add to parents of "vertical" images class "vertical" and reflect them afterwards
		/*$("IMG.reflect").each(function(){
			function processImage(){
				$.log("Complete: "+img.src+", analizing...."+this.width+"x"+this.height);
				if(this.height>this.width){
					$(this).parents("div").addClass('vertical');
				}
				$(this)
			}
			var img = this;
			if(img.complete){
				processImage.call(img);
			}else{
				$.log("Waiting for complete: "+img.src);
				$(img).load(processImage);
			}
		});*/	
		
				
		$(".sub-tabs > ul").tabs("div.tabPane",{
			history: false, 
			api:false,
			current:'ui-tabs-selected'
		});
		
		$.tools.addTabEffect("hideShowWithHiddenClass", function(tabIndex) {
			this.getPanes().addClass('hidden').eq(tabIndex).removeClass('hidden');
		});
		
		var tabsApi = $(".tabs").tabs("div.tabPane",{
			history: true,
			api:true,
			current:'ui-tabs-selected',
			effect: 'hideShowWithHiddenClass'
		});
		
		
		
	} catch (err) {
		$.log(err);
	}

});