var website_id = 6;
var sub_category_id = 5;
var remote_server_location = 'https://secure.fumzup.com.au/extranet/fumzup/api/';
//website_id = 10;
//sub_category_id = 22;
//remote_server_location = 'http://publiceye.david.dev/extranet/fumzup/api/';

function load_table(o_input){
	$('#dialog').jqmShow();

	$.getJSON(remote_server_location + "ajax_search.php?sub_category_id=" + sub_category_id + "&d=" + o_input.d + "&o=" + o_input.o + "&p=" + o_input.p + "&r=" + o_input.r + "&si=" + o_input.si + "&sf=" + o_input.si + "&jsoncallback=?",$("#fumzup_search").serialize(),
		function(data){
			if (data.status==0){
				$('#search_id').val(data.search_id);
				$("#step3-content").ajax_datatable({
					show_pagination:true,
					show_pagination_goto:true,
					show_search:false,
					show_records_per_page:true,
					lock_order_by:false,
					div_container:'step3-content',
					parent_function_name:'load_table',
					headings:data['headings'],
					no_of_rows:data['no_of_rows'],
					data:data['data'],
					row_data:data['row_data'],
					payload:o_input,
					rpp_dropdown:[5,10,15,20,25],
					row_clicked: function(o_row_data){
						var url = o_row_data.row_data;
						/*if(window.location.toString().match(/excite/)){
							var s = '<script type="text/javascript">';
							s+='syndConversionType = "301";';
							s+='syndAccountID = "959258";';
							s+='syndDomain = "http://r.excite.looksmart.com";';
							s+='syndFunnelValue = "100";</script>';
							s+='<script src="http://r.excite.looksmart.com/scripts/convTracking.js" type="text/javascript"></script>';
							$(document).ready(function(){$('body :last').after(s)});
						}*/
						window.open(url,'_blank');
					}
				});
				var contents = $('#step3-content .dt_h_cell_1 a').html();
				$('#step3-content .dt_h_cell_1 a').remove();
				$('#step3-content .dt_h_cell_1').html(contents);
				if ($('#pos_1_dd').html() != null){
					contents = $('#pos_1_dd').clone();
					$('#pos_1_dd').remove();
					contents.prependTo('#step3-content .dt_h_cell_4');
					$('#pos_1_dd').change(
						function(event){
							$('#pos_1').val($('#pos_1_dd').val());
							load_table({
								d:$('#dt_d_step3-content').val(),
								o:$('#dt_o_step3-content').val(),
								p:1,
								r:$('#dt_r_step3-content').val(),
								si:$('#dt_si_step3-content').val(),
								sf:$('#dt_sf_step3-content').val()
							});
						});
					contents = $('#pos_2_dd').clone();
					$('#pos_2_dd').remove();
					contents.prependTo('#step3-content .dt_h_cell_5');
					$('#pos_2_dd').change(
						function(event){
							$('#pos_2').val($('#pos_2_dd').val());
							load_table({
								d:$('#dt_d_step3-content').val(),
								o:$('#dt_o_step3-content').val(),
								p:1,
								r:$('#dt_r_step3-content').val(),
								si:$('#dt_si_step3-content').val(),
								sf:$('#dt_sf_step3-content').val()
							});
						});
					contents = $('#pos_3_dd').clone();
					$('#pos_3_dd').remove();
					contents.prependTo('#step3-content .dt_h_cell_6');
					$('#pos_3_dd').change(
						function(event){
							$('#pos_3').val($('#pos_3_dd').val());
							load_table({
								d:$('#dt_d_step3-content').val(),
								o:$('#dt_o_step3-content').val(),
								p:1,
								r:$('#dt_r_step3-content').val(),
								si:$('#dt_si_step3-content').val(),
								sf:$('#dt_sf_step3-content').val()
							});
						});
					contents = $('#step3-content .dt_h_cell_7 a').html();
					$('#step3-content .dt_h_cell_7 a').remove();
					$('#step3-content .dt_h_cell_7').html(contents);
				}else{
					if ($('#step3-content .dt_h_cell_6 a').html() == null){
						contents = $('#step3-content .dt_h_cell_5 a').html();
						$('#step3-content .dt_h_cell_5 a').remove();
						$('#step3-content .dt_h_cell_5').html(contents);
					}else if ($('#step3-content .dt_h_cell_7 a').html() == null){
						contents = $('#step3-content .dt_h_cell_6 a').html();
						$('#step3-content .dt_h_cell_6 a').remove();
						$('#step3-content .dt_h_cell_6').html(contents);
					}else{
						contents = $('#step3-content .dt_h_cell_7 a').html();
						$('#step3-content .dt_h_cell_7 a').remove();
						$('#step3-content .dt_h_cell_7').html(contents);
					}
				}
			}else{
				alert('A error has occured');
			}
			$('#create_search_record').val("false");
			$('#dialog').jqmHide();
		}
	);

}

function sort_features(a, b){
	return (b[1] - a[1]);
}

$(document).ready(function(){
	$.jqm.params.modal = true;
	$('#dialog').jqm();
	
	$("#no_feature_dialog").dialog({
		bgiframe: true,
		resizable: false,
		height: 140,
		modal: true,
		autoOpen: false
	});
	
	$("#no_slider_select_dialog").dialog({
		bgiframe: true,
		resizable: false,
		height:240,
		width:300,
		modal: true,
		autoOpen: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Yes': function() {
				$(this).dialog('close');
				show_results();
			},
			'No': function() {
				$(this).dialog('close');
			}
		}
	});
		
	$('#accordion').accordion({
		autoHeight: false,
		icons: {
			header: 'ui-icon-plusthick',
			headerSelected: 'ui-icon-minusthick'
		}
	});
	
	$('#step2_heading span').bind('click',function(event){
		return false;
	});
	$('#step2_heading a').bind('click',function(event){
		return false;
	});
	$('#step3_heading span').bind('click',function(event){
		return false;
	});
	$('#step3_heading a').bind('click',function(event){
		return false;
	});
	
	$('#advanced_search a').click(function(event){
		$('#advanced_search_outer').slideToggle("slow");
	});
	
	$('#advanced_search_outer a').click(function(event){
		$('#create_search_record').val("true");
		$('#advanced_search_outer').slideToggle("slow");
		load_table({
			d:$('#dt_d_step3-content').val(),
			o:$('#dt_o_step3-content').val(),
			p:1,
			r:$('#dt_r_step3-content').val(),
			si:$('#dt_si_step3-content').val(),
			sf:$('#dt_sf_step3-content').val()
		});
	});

	$('#step1_heading span').bind('click',function(event){
		$('#step2_heading span').bind('click',function(event){
			return false;
		});
		$('#step2_heading a').bind('click',function(event){
			return false;
		});
	});
	$('#step1_heading a').bind('click',function(event){
		$('#drag_me').remove();
		$('#step1_modify').remove();
		$('#step2_modify').remove();
		$('#step2_heading span').bind('click',function(event){
			return false;
		});
		$('#step2_heading a').bind('click',function(event){
			return false;
		});
	});

	$('#step_footer1 a').click(function(event){
		if ($(".feature_values").length==0){
			$("#no_feature_dialog").dialog('open');
		}else{
			$('#accordion').accordion('activate', 1 );
			$('#step1_heading a').append('<span id="step1_modify">&nbsp;&nbsp;&nbsp;[modify]</span>');
			setTimeout("$('#drag_me').fadeOut(500)",1500);
		}
	});

	$('#step_footer2 a').click(function(event){
		var slider_moved=false;
		$(".feature_values").each(function (index, domElement) {
			if (domElement.value!=1){
				slider_moved=true;
			}
		});
		if (slider_moved==false){
			$("#no_slider_select_dialog").dialog('open');
		}else{
			show_results();
		}
	});

	function show_results(){
		$('#step2_heading a').append('<span id="step2_modify">&nbsp;&nbsp;&nbsp;[modify]</span>');
		$('#accordion').accordion( 'activate' , 2 );
		$('#step2_heading span').unbind('click');
		$('#step2_heading a').unbind('click');
		$('#step2_heading a').bind('click',function(event){
			$('#step2_modify').remove();
		});
		var feature;
		var features = [];
		var x=0;
		$(".feature_values").each(function (index, domElement) {
			feature = [domElement.id,domElement.value];
			features[x] = feature;
			x++;
		});
		features.sort(function(a,b){return b[1] - a[1]});
		$('#pos_1').val("");
		$('#pos_2').val("");
		$('#pos_3').val("");
		var feature_id;
		for (x=0;x<features.length && x<3;x++){
			feature_id = features[x][0].substring(8,features[x][0].indexOf('_',8));
			$('#pos_' + (x+1)).val(feature_id);
		}
		
		$('#create_search_record').val("true");
		
		load_table({
			d:"down",
			o:2,
			p:1,
			r:10,
			si:'',
			sf:''
		});
	}
	
	$.getJSON(remote_server_location + "ajax_start.php?sub_category_id=" + sub_category_id + "&website_id=" + website_id + "&jsoncallback=?",
		function(data){
			load_features(data);
		}
	);

	function load_features(o_data){
		var s_feature;

		$('#session_id').val(o_data.session_id);
		$('#token').val(o_data.token);

		if (o_data.status==0){

			//###########################
			//
			//    create feature items
			//
			//###########################
			for (var x=0; x<o_data.features.length; x++){
				s_feature = '<div class="feature"><input id="feature_' + o_data.features[x].feature_id + '" type="checkbox" value="' + o_data.features[x].slider_type + '" class="feature_checkbox" /><label id="feature_' + o_data.features[x].feature_id + '_label" for="feature_' + o_data.features[x].feature_id + '">' + o_data.features[x].name + '</label>';
				if (o_data.features[x].slider_type == 'Both'){
					s_feature += '<div class="sub" id="feature_' + o_data.features[x].feature_id + '_sub">';
					s_feature += '<input id="feature_' + o_data.features[x].feature_id + '_sub_pos" name="feature_' + o_data.features[x].feature_id + '_sub" type="radio" value="pos" checked="checked" />';
					s_feature += '<label id="feature_' + o_data.features[x].feature_id + '_sub_pos_label" for="feature_' + o_data.features[x].feature_id + '_sub_pos">' + o_data.features[x].positive_label + '</label>';
					s_feature += '<br />';
					s_feature += '<input id="feature_' + o_data.features[x].feature_id + '_sub_neg" name="feature_' + o_data.features[x].feature_id + '_sub" type="radio" value="neg" />';
					s_feature += '<label id="feature_' + o_data.features[x].feature_id + '_sub_neg_label" for="feature_' + o_data.features[x].feature_id + '_neg">' + o_data.features[x].negative_label + '</label>';
					s_feature += '</div>';
				}
				s_feature += '<input type="hidden" name="feature_' + o_data.features[x].feature_id + '_name" value="' + o_data.features[x].name + '" /></div>';
				$('#step1-content').append(s_feature);
				
				//###########################
				//
				//    Radio button click events
				//
				//###########################
				
				$('#' + 'feature_' + o_data.features[x].feature_id + '_sub_pos').click(function(event){
					var s_id = $(this).attr('id').substring(0,$(this).attr('id').indexOf('_sub_pos'));
					$('#' + s_id + '_slider_label').html('<label id="' + s_id + '_slider_rating_label" for="' + s_id + '_slider_rating">' + $('#' + s_id + '_label').text() + ':</label><br /><span class="slider_label_sub"> - ' + $('#' + s_id + '_sub_pos_label').text() + '</span>');
					$('#' + s_id + '_slider_rating').attr('name',s_id + '_slider_rating_pos');
				});
				$('#' + 'feature_' + o_data.features[x].feature_id + '_sub_neg').click(function(event){
					var s_id = $(this).attr('id').substring(0,$(this).attr('id').indexOf('_sub_neg'));
					$('#' + s_id + '_slider_label').html('<label id="' + s_id + '_slider_rating_label" for="' + s_id + '_slider_rating">' + $('#' + s_id + '_label').text() + ':</label><br /><span class="slider_label_sub"> - ' + $('#' + s_id + '_sub_neg_label').text() + '</span>');
					$('#' + s_id + '_slider_rating').attr('name',s_id + '_slider_rating_neg');
				});
			}
			//###########################
			//
			//    add filler divs
			//
			//###########################
				
			var extras = 4 - (x % 4);
			for (x=0; x<extras && extras!=4; x++){
				s_feature = '<div class="feature"></div>';
				$('#step1-content').append(s_feature);
			}

			//###########################
			//
			//    create filter items
			//
			//###########################
			var s_filter;
			var s_filter_id;
			for (var x=0; x<o_data.filters.length; x++){
				s_filter_id = 'filter_' + o_data.filters[x].id + '_' + o_data.filters[x].filter_type;
				s_filter  = '<div id="' + s_filter_id + '_outer" class="filter_outer">';
				s_filter += '<div>';
				s_filter += '<input id="' + s_filter_id + '" name="' + s_filter_id + '" type="checkbox" value="true" class="filter_check"/>';
				s_filter += '<label for="' + s_filter_id + '">' + o_data.filters[x].name + '</label>';
				s_filter += '</div>';
				s_filter += '<div class="filter_values" id="' + s_filter_id + '_values">';
				if (o_data.filters[x].answer_type == 'Option'){
					s_filter += '<input id="' + s_filter_id + '_1" name="' + s_filter_id + '_rb" type="radio" value="Yes" checked="checked" /><label for="' + s_filter_id + '_1">Yes</label><br />';
					s_filter += '<input id="' + s_filter_id + '_2" name="' + s_filter_id + '_rb" type="radio" value="No" /><label for="' + s_filter_id + '_2">No</label>';
				}else{
					for (var y=0; y<o_data.filters[x].answers.length; y++){
						s_filter += '<input id="' + s_filter_id + '_cb_' + (y+1) + '" name="' + s_filter_id + '_cb_' + (y+1) + '" type="checkbox" value="' + o_data.filters[x].answers[y].value + '" />';
						s_filter += '<label for="' + s_filter_id + '_cb_' + (y+1) + '">' + o_data.filters[x].answers[y].label + '</label><br />';
					}
				}
				s_filter += '</div>';
				s_filter += '</div>';
				$('#filters').append(s_filter);
			}
			
			$('.filter_check').click(function(event){
				if ($(this).attr('checked')==true){
					$('#' + $(this).attr('id') + '_values').show();
				}else{
					$('#' + $(this).attr('id') + '_values').hide();
				}
			});
			
			//###########################
			//
			//    feature checbox click events
			//
			//###########################
				
			$('.feature_checkbox').click(function(event){
				var s_id = $(this).attr('id');
				if ($(this).attr('checked')==true){
					
					$('#' + s_id + '_sub').show();
					
					s_slider  = '<div id="' + s_id + '_slider_outer" class="slider_outer">';
					s_slider += '<div id="' + s_id + '_slider_rating_score" class="slider_value_label">(1/6) Not so important</div>';
					s_slider += '<div id="' + s_id + '_slider_label" class="slider_label">';
					s_slider += '<label for="' + s_id + '_slider_rating">';
					if ($('#' + s_id + '_sub_pos').attr('checked')==true){
						s_slider += $('#' + s_id + '_label').text() + ':</label><br /><span class="slider_label_sub"> - ' + $('#' + s_id + '_sub_pos_label').text() + '</span>';
						direction = 'pos';
					}else if ($('#' + s_id + '_sub_neg').attr('checked')==true){
						s_slider += $('#' + s_id + '_label').text() + ':</label><br /><span class="slider_label_sub"> - ' + $('#' + s_id + '_sub_neg_label').text() + '</span>';
						direction = 'neg';
					}else{
						s_slider += $('#' + s_id + '_label').text() + ':</label>';
						if ($(this).attr('value')=='Positive'){
							direction = 'pos';
						}else{
							direction = 'neg';
						}
					}
					s_slider += '</div>';
					s_slider += '<input type="hidden" id="' + s_id + '_slider_rating" name="' + s_id + '_slider_rating_' + direction + '" value="1" class="feature_values" />';
					s_slider += '<div id="' + s_id + '_slider" class="slider"></div>';
					s_slider += '</div>';
					$('#step2-content').append(s_slider);

					//###########################
					//
					//    init the slider events
					//
					//###########################
				
					$('#' + s_id + '_slider').slider({
						value:1,
						min: 1,
						max: 6,
						step: 1,
						slide: function(event, ui) {
							$('#' + $(this).attr('id') + '_rating').val(ui.value);
							var description;
							if (ui.value==1){
								description = 'Not so important';
							}else if (ui.value==2){
								description = 'Quite important';
							}else if (ui.value==3){
								description = 'Important';
							}else if (ui.value==4){
								description = 'Very important';
							}else if (ui.value==5){
								description = 'Extremely important';
							}else if (ui.value==6){
								description = 'Absolutely essential';
							}else{
								description = 'N/A';
							}
							$('#' + $(this).attr('id') + '_rating_score').html('(' + ui.value+'/6) ' + description);
						}
					});
					
				}else{
					$('#' + $(this).attr('id') + '_sub').hide();
					$('#' + s_id + '_slider_outer').remove();
				}
			});
		}else{
			alert(o_data.message);
		}
	}
	$('.slider').slider({
		value:0,
		min: 0,
		max: 6,
		step: 1,
		slide: function(event, ui) {
			$('#' + $(this).attr('id') + '_rating').val(ui.value);
			$('#' + $(this).attr('id') + '_rating_score').html(ui.value+'/6 Importance');
		}
	});
	$('#search_container').show();
});