var Product_Advisor =  Product_Advisor || {};
var Bought_Products = 0;

Product_Advisor.init = function(){
    // enable the popup
	if ( jQuery('#ex2').length ) {
	    jQuery('#ex2').jqm({
	    				ajax:'/external/advisor/',
						trigger: 'p#productadivsor a, #prod2, a.quickJump',
						overlay: 30,
						onLoad : Product_Advisor.goToLastStep,
						onHide : Product_Advisor.hideModal
					});
		jQuery('#ex2').jqmAddClose('a#closejqWindow');
	}

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;

    //go to step1
    jQuery("#goto_step1").live("click",Product_Advisor.goto_step1);
    //go to step2
    jQuery("#goto_step2").live("click",Product_Advisor.goto_step2);
     //go to step3
    jQuery("#goto_step3").live("click",Product_Advisor.goto_step3);

	// handle login to add to wishlist
	jQuery('a#wishlist').live("click",Product_Advisor.loginPlease);

	// handle login to add to wishlist
	jQuery('a.cancel').live("click",Product_Advisor.recommendationPlease);
	// handle login
	jQuery('#login-bt').live("click",Product_Advisor.login);

	// handle product addition to basket content
	//jQuery('.buyproducts a').live("click",Product_Advisor.addToBasket);

    //go back to welcome
    jQuery("#goback_welcome.current, #tryagain").live("click",function(){Product_Advisor.goback_welcome();return false;});;
    jQuery("#goback_step1.current").live("click",Product_Advisor.goback_step1);
    jQuery("#goback_step2.current").live("click",Product_Advisor.goback_step2);

  	// move PA on vertical
		jQuery(window).bind('resize', function() {
		var middleVert = Math.round((jQuery(window).height()-450)/2);

		if ( middleVert < 1 ) {
			jQuery('#ex2').css({top:0/*,position:'absolute'*/});
		} else {
			jQuery('#ex2').css({top:middleVert+'px'/*,position:'fixed'*/});
		}
	});
};
/*
 * When the page load check to see the step where the last time popup was open and show
 * it to the user
 */
Product_Advisor.goToLastStep = function(){
    /*var current_step = jQuery("#current_step").val();
    switch(current_step)
    {
        case '1' : Product_Advisor.landon_step1();  break;
        case '2' : Product_Advisor.landon_step2();  break;
        case '3' : Product_Advisor.landon_step3();  break;
    }*/

  // put the PA on the vertical center
	var middleVert = Math.round((jQuery(window).height()-450)/2);
	jQuery('#ex2').css('top',middleVert+'px');

	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh();

	Product_Advisor.recommendationPlease();
}

Product_Advisor.setCurrentStep = function(step){
    jQuery("#current_step").val(step);
}

Product_Advisor.loginPlease = function(){
	jQuery('#recommendation').fadeOut(200, function(){
		jQuery('#loginform').fadeIn(500);
	});

	return false;
}

Product_Advisor.recommendationPlease = function(){
	jQuery('#loginform').fadeOut(200, function(){
		jQuery('#recommendation').fadeIn(500);
	});

	return false;
}

/*Product_Advisor.addToBasket = function(){
	// get product relation
	var rel = jQuery(this).attr("rel");
	var sortId = rel.match(/\d/g)[0];

	// get product details
	var product_name = jQuery('#p'+sortId).text();
	var price = jQuery(this).siblings('span').html();

	// append to current basket content
	var totalLI = parseInt( jQuery('.basket-wrapper ul li').size() + 1 );
	jQuery('.basket-wrapper ul').append('<li class="product-'+ totalLI +'"><span>'+ price +'</span>' + product_name +'</li>');

	// close jqModal
	//jQuery('#ex2').jqmHide();

	// make basket animation
	jQuery('.basket-wrapper li, .basket-background-top div, .basket-background-bottom a, .basket-background-top span').css({ opacity:0 });
	jQuery('.basket-wrapper').show(250, function(){
		jQuery('.basket-wrapper li, .basket-background-top span, .basket-background-top div, .basket-background-bottom a').animate({opacity:1}, 500);
	});
	jQuery('body, html').animate({'scrollTop': 0}, 'slow');

	var basketTimeout = window.setTimeout( function() {
		jQuery('.basket-wrapper li, .basket-background-top span, .basket-background-top div, .basket-background-bottom a').animate({
			opacity:0
		}, 500, null, function(){
			jQuery('.basket-wrapper').hide(250);
		});
		return false;
	},3000);

	return false;

}*/

Product_Advisor.goto_step1 = function(){
	if(Product_Advisor.isChecked("welcome_form")){
	    jQuery.ajax({
	       type    : jQuery("#welcome_form").attr("method"),
	       url     : jQuery("#welcome_form").attr("action"),
	       data    : jQuery("#welcome_form").serialize(),
	       dataType: "json",
	       success : Product_Advisor.landOnStep1
	     });
	}

	return false;
}

Product_Advisor.landOnStep1 = function(jsonResponse){
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
    jQuery('#intro .inner').fadeOut(100, function() {
		// animate intro
		jQuery('#intro').animate({width:0},600,function(){
			// animate step 1
			jQuery('#s1').animate({width:762},300,function() {
			   jQuery("#ul_problems").empty();
				for(var i=0;i<jsonResponse.arProblems.length;i++){
					var problem = jsonResponse.arProblems[i];
					var li_problem = jQuery('<li><input type="radio" class="radio" name="problem"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					jQuery("#ul_problems").append(li_problem);
				}
//				var link = jQuery('<a href="#" id="goto_step2" class="goto2 floatRight"><img src="images/buttons/goto2.gif" width="81" height="20" alt="Go to step 2" /></a>');
//				jQuery("#ul_problems li:last").append(link);
				// show step 1 content
				jQuery('#s1 .stepcontent').fadeIn();
				jQuery('#s1 .steptrigger').addClass('current');
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.goto_step2 = function(){
	if(Product_Advisor.isChecked("step1_form")){
	    jQuery.ajax({
	       type    : jQuery("#step1_form").attr("method"),
	       url     : jQuery("#step1_form").attr("action"),
	       data    : jQuery("#step1_form").serialize(),
	       dataType: "json",
	       success : Product_Advisor.landOnStep2
	     });
	}

	return false;
}

Product_Advisor.landOnStep2 = function(jsonResponse){
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
    jQuery('#s1 .stepcontent').fadeOut(100, function(){
		// animate step 1
		jQuery('#s1').animate({width:60},600,function(){
			// animate step 1
			jQuery('#s2').animate({width:762},300,function(){
				// show step 2 content
				jQuery("#ul_problems2").empty();
				for(var i=0;i<jsonResponse.arProblems.length;i++){
					var problem = jsonResponse.arProblems[i];
					var li_problem = jQuery('<li><input type="radio" class="radio" name="problem2"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					jQuery("#ul_problems2").append(li_problem);
				}
//				var link = jQuery('<a href="#" id="goto_step3" class="goto3 floatRight"><img src="images/buttons/goto2.gif" width="81" height="20" alt="Go to step 3" /></a>');
//				jQuery("#ul_problems2 li:last").append(link);

				jQuery('#s2 .stepcontent').fadeIn();
				jQuery('#s2 .steptrigger').addClass('current');
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.goto_step3 = function(){
	if(Product_Advisor.isChecked("step2_form")){
	    jQuery.ajax({
	       type    : jQuery("#step2_form").attr("method"),
	       url     : jQuery("#step2_form").attr("action"),
	       data    : jQuery("#step2_form").serialize(),
	       dataType: "json",
	       success : Product_Advisor.landOnStep3
	     });
	}

	return false;
}

Product_Advisor.landOnStep3 = function(jsonResponse){
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
    jQuery('#s2 .stepcontent').fadeOut(100, function(){
		jQuery('#s2').animate({width:60},600,function(){
			jQuery('#s3').animate({width:762},300,function(){
				//console.log(jsonResponse.html);
				jQuery("#prods").html(jsonResponse.html);
				jQuery("#s3 .stepcontent").show();
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.goback_welcome = function(){
      jQuery.ajax({
           type    : "GET",
           url     : '/external/advisor/',
           dataType: "html",
           success : Product_Advisor.landOnWelcome
     });
}

Product_Advisor.landOnWelcome = function(jsonResponse){
	// switch to recommendation screen
	Product_Advisor.recommendationPlease();

    Product_Advisor.setCurrentStep(jsonResponse.current_step);
	jQuery('#s1 .stepcontent').fadeOut(100, function(){
		// animate step 1
		jQuery('#s1').animate({width:60},600,function(){
			// animate step 1
			jQuery('#intro').animate({width:702},300,function(){

				jQuery('#intro .inner').fadeIn();
				jQuery('#s1 .steptrigger').removeClass('current');
				jQuery("input[name='advice'][value='"+jsonResponse.advice+"']").attr("checked",true);

				 jQuery('#s2 .stepcontent').hide();
				 jQuery('#s2 .steptrigger').removeClass('current');
				 jQuery('#s3 .stepcontent').hide();
				 jQuery('#s3 .steptrigger').removeClass('current');
				 jQuery("#s2").width(60);
				 jQuery("#s3").width(60);
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.goback_step1 = function(){
     jQuery.ajax({
       type    : jQuery("#welcome_form").attr("method"),
       url     : jQuery("#welcome_form").attr("action"),
       data    : jQuery("#welcome_form").serialize(),
       dataType: "json",
       success : Product_Advisor.landOnStep1Back
     });
}

Product_Advisor.landOnStep1Back = function(jsonResponse){
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
     jQuery('#s2 .stepcontent').fadeOut(100, function(){
		// animate step 1
		jQuery('#s2').animate({width:60},600,function(){
			// animate step 1
			jQuery('#s1').animate({width:762},300,function() {
				jQuery("#ul_problems").empty();
				for(var i=0;i<jsonResponse.arProblems.length;i++){
					var problem = jsonResponse.arProblems[i];

					if (jsonResponse.allready_selected == problem.problem_id){
						var li_problem = jQuery('<li><input checked="checked" type="radio" class="radio" name="problem"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					 } else {
						var li_problem = jQuery('<li><input type="radio" class="radio" name="problem"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					 }
					jQuery("#ul_problems").append(li_problem);
				}
//				var link = jQuery('<a href="#" id="goto_step2" class="goto2 floatRight"><img src="images/buttons/goto2.gif" width="81" height="20" alt="Go to step 2" /></a>');
//				jQuery("#ul_problems li:last").append(link);
				jQuery('#s1 .stepcontent').fadeIn();
				jQuery('#s2 .steptrigger').removeClass('current');
				jQuery('#s3 .stepcontent').hide();
				jQuery('#s3 .steptrigger').removeClass('current');
				jQuery("#s3").width(60);
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}


Product_Advisor.goback_step2 = function() {
     jQuery.ajax({
       type    : jQuery("#step1_form").attr("method"),
       url     : jQuery("#step1_form").attr("action"),
       data    : jQuery("#step1_form").serialize(),
       dataType: "json",
       success : Product_Advisor.landOnStep2Back
     });
}

Product_Advisor.landOnStep2Back = function(jsonResponse) {
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
     jQuery('#s3 .stepcontent').fadeOut(100, function(){
		// animate step 1
		jQuery('#s3').animate({width:60},600,function(){
			// animate step 1
			jQuery('#s2').animate({width:762},300,function() {
				jQuery("#ul_problems2").empty();
				for(var i=0;i<jsonResponse.arProblems.length;i++) {
					var problem = jsonResponse.arProblems[i];
					if (jsonResponse.allready_selected == problem.problem_id) {
						var li_problem = jQuery('<li><input checked="checked" type="radio" class="radio" name="problem2"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					} else {
						var li_problem = jQuery('<li><input type="radio" class="radio" name="problem2"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					}
					jQuery("#ul_problems2").append(li_problem);
				}
//				var link = jQuery('<a href="#" id="goto_step3" class="goto3 floatRight"><img src="images/buttons/goto2.gif" width="81" height="20" alt="Go to step 3" /></a>');
//				jQuery("#ul_problems2 li:last").append(link);
				jQuery('#s2 .stepcontent').fadeIn();
				jQuery('#s3 .steptrigger').removeClass('current');
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}


Product_Advisor.landon_step1 = function() {
     jQuery.ajax({
       type    : jQuery("#welcome_form").attr("method"),
       url     : jQuery("#welcome_form").attr("action"),
       data    : jQuery("#welcome_form").serialize(),
       dataType: "json",
       success : Product_Advisor.landOnStep1Again
     });
}

Product_Advisor.landOnStep1Again = function(jsonResponse) {
   Product_Advisor.setCurrentStep(jsonResponse.current_step);
    jQuery('#intro .inner').fadeOut(100, function(){
		// animate intro
		jQuery('#intro').animate({width:0},600,function(){
			// animate step 1
			jQuery('#s1').animate({width:762},300,function(){
			   jQuery("#ul_problems").empty();
				for(var i=0;i<jsonResponse.arProblems.length;i++){
					var problem = jsonResponse.arProblems[i];
					if (jsonResponse.allready_selected == problem.problem_id){
						var li_problem = jQuery('<li><input checked="checked" type="radio" class="radio" name="problem"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					 } else {
						var li_problem = jQuery('<li><input type="radio" class="radio" name="problem"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					 }

					jQuery("#ul_problems").append(li_problem);
				}
//				var link = jQuery('<a href="#" id="goto_step2" class="goto2 floatRight"><img src="images/buttons/goto2.gif" width="81" height="20" alt="Go to step 2" /></a>');
//				jQuery("#ul_problems li:last").append(link);
				// show step 1 content
				jQuery('#s1 .stepcontent').fadeIn();
				jQuery('#s1 .steptrigger').addClass('current');
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.landon_step2 = function()
{
     jQuery.ajax({
       type    : jQuery("#step1_form").attr("method"),
       url     : jQuery("#step1_form").attr("action"),
       data    : jQuery("#step1_form").serialize(),
       dataType: "json",
       success : Product_Advisor.landOnStep2Again
     });
}

Product_Advisor.landOnStep2Again = function(jsonResponse)
{
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
    jQuery('#intro .inner').fadeOut(100, function(){
		// animate intro
		jQuery('#intro').animate({width:0},600,function(){
			// animate step 1
			jQuery('#s2').animate({width:762},300,function(){
				jQuery("#ul_problems2").empty();
				for(var i=0;i<jsonResponse.arProblems.length;i++){
					var problem = jsonResponse.arProblems[i];
					if (jsonResponse.allready_selected == problem.problem_id){
						var li_problem = jQuery('<li><input checked="checked" type="radio" class="radio" name="problem2"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					} else {
						var li_problem = jQuery('<li><input type="radio" class="radio" name="problem2"  id="problem_'+problem.problem_id+'" value="'+problem.problem_id+'" /><label for="problem_'+problem.problem_id+'">'+problem.problem_name+'</label></li>"')
					}
					jQuery("#ul_problems2").append(li_problem);
				}
//				var link = jQuery('<a href="#" id="goto_step3" class="goto3 floatRight"><img src="images/buttons/goto2.gif" width="81" height="20" alt="Go to step 3" /></a>');
//				jQuery("#ul_problems2 li:last").append(link);

				jQuery('#s2 .stepcontent').fadeIn();
				jQuery('#s2 .steptrigger').addClass('current');
				jQuery('#s1 .steptrigger').addClass('current');
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.landon_step3   = function(){
     jQuery.ajax({
       type    : jQuery("#step2_form").attr("method"),
       url     : jQuery("#step2_form").attr("action"),
       data    : jQuery("#step2_form").serialize(),
       dataType: "json",
       success : Product_Advisor.landOnStep3Again
     });
}

Product_Advisor.landOnStep3Again = function(jsonResponse){
    Product_Advisor.setCurrentStep(jsonResponse.current_step);
    jQuery('#intro .inner').fadeOut(100, function(){
		// animate intro
		jQuery('#intro').animate({width:0},600,function(){
			jQuery('#s3').animate({width:762},300,function(){

				jQuery("#prods").html(jsonResponse.html);

				jQuery('#s2 .steptrigger').addClass('current');
				jQuery('#s1 .steptrigger').addClass('current');
				jQuery('#s3 .stepcontent').fadeIn();
				jQuery('#s3 .steptrigger').addClass('current');
			});
		});
	});

	// cufon
	Cufon.now();
	Cufon.replace('h2.page-title');
	Cufon.refresh;
}

Product_Advisor.isChecked = function(form){
	var checked = false;
	jQuery("#"+form+" input").each(function(){
		if(jQuery(this)[0].checked){
			checked=true;
		}
	});
	return checked;
}

Product_Advisor.login = function(){
	jQuery("#errorMessage").hide();
	jQuery("#login-bt").hide();

	var whishlistData = {username: jQuery("#username").val(), password: jQuery("#password").val(), products: jQuery("#products_ids").val()};
	jQuery.ajax({
		type    : jQuery("#loginplease").attr("method"),
		url     : "/external/ajax/advisor.dispatcher/act/whishlist/",
		data    : whishlistData,
		dataType: "json",
		success : Product_Advisor.postLogin
     });
	return false;
}

Product_Advisor.postLogin = function(jsonResponse){
	if(jsonResponse.validLogin == 1){
		jQuery("#loginplease").submit();
	}else{
		jQuery("#errorMessage").show();
		jQuery("#login-bt").show();
	}
}

Product_Advisor.buyProduct = function(button){
	jQuery(button).hide().next().show().parents('form').submit();
	Bought_Products++;
}

Product_Advisor.hideModal = function(){
	jQuery('.jqmOverlay').hide();
	jQuery('#ex2').hide();
	//refresh for basket to update
	if(Bought_Products > 0){
		document.location.href = document.location.href;
	}
	return true;
}

jQuery(document).ready(Product_Advisor.init);