// $Id: pconf_onepage_steps.js,v 1.0 2010/08/10 13:23:12 yakoff Exp $
//
jQuery(document).ready(function() {
	jQuery(function() {
		jQuery("#accordion").accordion();
		jQuery("#accordion").accordion("option", "collapsible", true);
		jQuery("#accordion").accordion("option", "autoHeight", false);
	});     

	$(".save_buttons").each(function() {
		$(this).click(function() {
			var active = $("#accordion").accordion( "option", "active" );
			var slotid = $("#slot_" + active).val();

			var addproductid = $("#products_" + active).val();

			if (addproductid == undefined) {
				addproductid = $('input:radio[name=radio_products_' + active + ']:checked').val();
			}

			if (addproductid == '' || addproductid == undefined) {
				$( "#accordion" ).accordion("option", "active", active + 1);
				return;
			}
	
			var productid = $("#productid").val();

			var data = '';
			data = 'mode=add&slot=' + slotid + '&addproductid=' + addproductid + '&productid=' + productid;

			$.ajax({
				url: 'pconf_one_page.php',
				type: 'POST',
				data: data,
				timeout: 100000,
				success: function(x) {
					$( "#accordion" ).accordion("option", "active", active + 1);
					load_price();
				}
			});			
		})
	})

	$(".delete_buttons").each(function() {
        $(this).click(function() {
			var active = $("#accordion").accordion( "option", "active" );
            var slotid = $("#slot_" + active).val();

			var productid = $("#productid").val();
		
			var data = '';
			data = 'mode=delete&slot=' + slotid + '&productid=' + productid;
	
			$.ajax({
				url: 'pconf_one_page.php',
				type: 'GET',			
				data: data,
				timeout: 100000,
				success: function(x) {
					$('input:radio[name=radio_products_' + active + ']').each(function () {
                        this.checked = false;
                    });
					$("#products_" + active).val("");		
					load_price();
				}
			})
		})
	})

	$(".next_buttons").each(function() {
        $(this).click(function() {
			var active = $( "#accordion" ).accordion( "option", "active" );
			$( "#accordion" ).accordion("option", "active", active + 1);
		})
	})

	$('#fabric_name').keypress(function(eventObject){
		if (eventObject.which === 13)
			return false;
	});

	$(".categories_checkboxes, #search_fabric_id").each(function() {
        $(this).click(function() {
			
			var active = $("#accordion").accordion("option", "active");
            var slotid = $("#slot_" + active).val();
            var productid = $("#productid").val();
   
            var data = '&slot=' + slotid + '&productid=' + productid + '&all_slots=true&ajaxload=true&' + $("#slot_categories_form_id_" + active).serialize();
 
            $.ajax({
                url: 'pconf_one_page.php',
                type: 'GET',
                data: data,
                timeout: 100000,
                beforeSend: function() {
                    $("#number_" + active).html('');
                    var l_img = document.createElement('img');
                    l_img.src = images_dir + '/ajax-loader.gif';
                    $("#number_" + active).append(l_img);
                },
 
                success: function(html) {
                    $("#number_" + active).html(html);
                }
            })
		})
	})	
})

function load_price() {

	var productid = $("#productid").val();

	var data = '&get_price=true&productid='+productid;	

	$.ajax({
		url: 'pconf_one_page.php',
		type: 'GET',
		data: data,
		timeout: 100000,
		dataType: "html",
		success: function(t) {
			$("#online_price").html(t);
		}
	})

	var data = '&get_selected_slots=true&productid='+productid;

	$.ajax({
        url: 'pconf_one_page.php',
        type: 'GET',
        data: data,
        timeout: 100000,
        dataType: "html",
        success: function(t) {
            $("#check_slots_script_data").html(t);
			if (selected_slots != undefined) {
				check_selected_slots(selected_slots);
			}
			
			if (is_all_selected == true) {
				$("#all_slot_selected_image").show();
			} else {
				$("#all_slot_selected_image").hide();
			}
        }
    })
}	

function check_selected_slots(selected_slots) {

	$(".checked_images").hide();	
		
	for (var x in selected_slots) {

		$('#checked_image_' + selected_slots[x]).show();

	}

}	

function send_request(x) {
	var page = x.getAttribute('name');
	var active = $("#accordion").accordion("option", "active");
	var slotid = $("#slot_" + active).val();
	var productid = $("#productid").val();


	var cur_page = $(".NavigationCellSel").attr('index');

	if (page == 'next_page') {
		page = (cur_page - 0) + 1;
	}

	if (page == 'previous_page') {
        page = cur_page - 1;
    }

	var data = '';
	data = '&slot=' + slotid + '&productid=' + productid + '&page=' + page + '&all_slots=true&ajaxload=true&' + $("#slot_categories_form_id_" + active).serialize();

	$.ajax({
		url: 'pconf_one_page.php',
		type: 'GET',
		data: data,
		timeout: 100000,
		dataType: 'html',

		beforeSend: function(){
			$("#number_" + active).html('');
			var l_img = document.createElement('img');
			l_img.src = images_dir + '/ajax-loader.gif';
			$("#number_" + active).append(l_img);
		},

		success: function(html) {
			$("#number_" + active).html(html);
		},

		error:function (xhr, ajaxOptions, thrownError){
			alert(xhr.status);
			alert(thrownError);
		}    
	})

	return false;   
}

function func_show_all_fabrics() {

	$("#fabric_name").val('');
	$("#search_fabric_id").trigger('click');	

} 

function performCloseActions() {
	
	load_price();

	var active = $("#accordion").accordion("option", "active");

	$("#main_" + active + " .next_buttons").trigger('click');

	tb_remove();

}


