var lmi = {
	init: function() {

	    $("#stb").keyup(function () {
	        $("#search_results").load("get_results_2.php", {'search_terms': $("#stb").val()}, function () {
	            $("#search_results").fadeIn(1500);
	        });
	    });
	    
	    $("#astb").keyup(function () {
	        if ($("#b_search_results").is(":hidden")) {
                $("#b_search_results").fadeIn(500);
            }
	        $("#the_results").load("get_results.php", {'search_terms': $("#astb").val()}, function () {
                $("#the_results").fadeIn(1000);
                $("body").append('<div class="lb_overlay"></div>');
	            
                window.remove_search_overlay();
	        });
	    });
	    
	    $("#astb").focus(function () {
	        if ($("#b_search_results").is(":hidden")) {
                $("#b_search_results").fadeIn(500);
            }
	        $("#the_results").load("get_results.php", {'search_terms': $("#astb").val()}, function () {
                $("#the_results").fadeIn(1000);
                $("body").append('<div class="lb_overlay"></div>');
	            
                window.remove_search_overlay();
	        });
	    });
	
        var product_popup = "";

        var speeds          = new Array(3000, 2000, 3000, 2000, 3000, 2000, 3000, 3000, 2000, 3000, 2000);
	    var timeouts        = new Array(3500, 3000, 2500, 3500, 3000, 3500, 3000, 2500, 3500, 2500, 3500);

        // 9 mini slideshows
        for (var i = 1; i < 10; i++) {
            // start slideshow
            $('#slideshow_'+i).cycle({
                sync: 1,
                fx: 'fade',
                speed: speeds[i],
                timeout: timeouts[i]
            });
        }
    }
}

jQuery(document).ready(function() {
	lmi.init();
});

var current_photo_index = 1;
var hgphotos = [];
var current_product_id = null;

function view_search_results() {
    window.location = "search.php?action=search&search_terms="+$("#astb").val();
}

function submit_product_form() {
    alert("Request submitted.  Thank You.");
    hide_form();
}

function hide_for_product_form() {
    $("#product_contact_submission").fadeOut(200);
    $("#product_description").fadeOut(200);
    $("#supplier_thumb").fadeOut(200);
    $("#technical_data_box").fadeOut(200);
    $("#order_box").fadeOut(200);
    $("#technical_data").fadeOut(200);
    $("#order_data").fadeOut(200);
    $("#description_toggle").fadeIn(200);
    $("#order_form").fadeIn(200);
}

function hide_form() {
    $("#order_form").fadeOut(200);
    $("#description_toggle").fadeOut(200);
    $("#video_area").fadeOut(200);
    $("#product_description").fadeIn(200);
    $("#supplier_thumb").fadeIn(200);
    $("#technical_data_box").fadeIn(200);
    $("#order_box").fadeIn(200);
    $("#technical_data").fadeIn(200);
    $("#order_data").fadeIn(200);
}

function show_video(document_id) {
    $("#product_description").fadeOut(200);
    $("#technical_data_box").fadeOut(200);
    $("#order_box").fadeOut(200);
    $("#technical_data").fadeOut(200);
    $("#order_data").fadeOut(200);
    $("#supplier_thumb").fadeOut(200);
    
    $("#description_toggle").fadeIn(200);
    $("#video_area").fadeIn(200);
    $("#flv_movie_"+document_id).fadeIn();
    
}

function show_product_image(document_id) {
    $("#product_image_"+document_id).fadeIn(200);
}

function swap_main_product_photo(photo_index, hourglass_status) {
    if (!hourglass_status) {
        $("#hourglass").fadeOut(100);
    } else {
        $("#hourglass").fadeIn(100);
    }
    for (var i = 1; i < 11; i++) {
        $("#main_product_photo_"+i).hide();
    }
    current_photo_index = photo_index;
    $("#main_product_photo_"+photo_index).fadeIn(600);
}

function from_hourglass() {
    if (hgphotos[current_photo_index - 1] != "EMPTY") {
        show_full_image(hgphotos[current_photo_index - 1]);
    }
}

function show_full_image(image_url) {
    //add the elements to the dom
    $("body").append('<div class="lb_overlay"></div>');
    $("#product_photos_out").prepend('<div id="lb_container"></div>');
    
    $("#lb_container").html('<img src="'+image_url+'" border="0" alt="" />');
    $("#lb_container").fadeIn(600);
    window.remove_overlay();
}

function remove_overlay() {
// allow users to be able to close the lightbox
	$("#lb_container, .lb_overlay").click(function() {
        $("#lb_container, .lb_overlay").fadeOut(600);
        $("#lb_container, .lb_overlay").remove();
	});
}

function remove_search_overlay() {
    // allow users to be able to close the lightbox
    $(".lb_overlay").click(function() {
        $("#b_search_results, .lb_overlay").fadeOut(600);
        $(".lb_overlay").remove();
    });
}

function open_document_family(document_id) {
    $('#doc_family_'+document_id).toggle();
    if ($("#doci_"+document_id).attr("src") == "images/plus.gif") {
        $("#doci_"+document_id).attr({src:'images/neg.gif'});
    } else {
        $("#doci_"+document_id).attr({src:'images/plus.gif'});
    }
}

function load_prev_product(product_id, category_id) {
    empty_magnigy_photos();
    $("#product_content_area").fadeOut(900);
    if (current_product_id > 0) {
        product_id = current_product_id;
    }
    $.get("../ajax/ajax.php", {action: 'get_prev', product_id: product_id, category_id: category_id},
        function(product_id) {
            current_product_id = product_id;
            $("#product_content_area").load("../ajax/ajax.php", {action: 'load_product',product_id: current_product_id,category_id: category_id}, function(data) {
                $("#product_content_area").fadeIn(900);
            });
            $("#product_name_breadcrumb").load("../ajax/ajax.php", {action: 'get_product_name',product_id: current_product_id});
        }
    );
}

function empty_magnigy_photos() {
    for (i=0;i<11;i++) {
        hgphotos.pop();
    }
}

function load_next_product(product_id, category_id) {
    //alert(product_id+' '+category_id);
    empty_magnigy_photos();
    $("#product_content_area").fadeOut(900);
    if (current_product_id > 0) {
        product_id = current_product_id;
    }
    $.get("../ajax/ajax.php", {action: 'get_next', product_id: product_id, category_id: category_id},
        function(product_id) {
            current_product_id = product_id;
            $("#product_content_area").load("../ajax/ajax.php", {action: 'load_product',product_id: current_product_id,category_id: category_id}, function(data) {
                $("#product_content_area").fadeIn(900);
            });
            $("#product_name_breadcrumb").load("../ajax/ajax.php", {action: 'get_product_name',product_id: current_product_id});
        }
    );
}

function validate_product_request() {
    valid = true;
    if (!$("#first_name").val()) {
        $("#error_first_name").html("First Name is a required field.");
        $("#error_first_name").fadeIn(1200);
        valid = false;
    }
    if (!$("#last_name").val()) {
        $("#error_last_name").html("Last Name is a required field.");
        $("#error_last_name").fadeIn(1200);
        valid = false;
    }
    if (!$("#email").val()) {
        $("#error_email").html("Email is a required field.");
        $("#error_email").fadeIn(1200);
        valid = false;
    }
    if (!$("#confirm_email").val()) {
        $("#error_confirm_email").html("Confirmation Email is a required field.");
        $("#error_confirm_email").fadeIn(1200);
        valid = false;
    }
    if ($("#email").val() != $("#confirm_email").val()) {
        $("#error_email_match").html("Confirmation Email is a required field.");
        $("#error_email_match").fadeIn(1200);
        valid = false;
    }
    if (!$("#profession").val()) {
        $("#error_profession").html("Profession is a required field.");
        $("#error_profession").fadeIn(1200);
        valid = false;
    }
    var security_code = $('#security_code').val();
    //alert(security_code);
    
    $.get("../ajax/ajax.php", {action: 'validate_security_code', security_code: security_code},
        function(return_data) {
            if (trim(return_data) != "valid_code") {
                $("#error_sc").html("The security code was not correct.");
                $("#error_sc").fadeIn(1200);
                valid = false;
            }
        },'text'
    );

    return valid;
}




function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

