(function ($) {
    $.floatbox = function (options) {
		//support for jquery 1.0 request by christoph@breidert.net
		var getWidth = function () {
			var version = parseInt($.prototype.jquery.match(/\d/gim)[1]);
			var width;
			if (version > 1) {
				width = $(window).width();
			} else {
				width = document.body.scrollWidth ? document.body.scrollWidth : document.documentElement.scrollWidth;
			}
			return width / 2;
		};
        var settings = $.extend({
            bg : "floatbox-background",
            box : "floatbox-box",
            content : "",
			//button: "<p><a role='button' href='javascript:void(0);' class='close-floatbox'>Close</a></p>",
			desc: "This is a popup box, press esc key to close.",
            fade : false,
			ajax: null,
            bgConfig : {
                position : ($.browser.msie) ? "absolute" : "fixed",
                zIndex: 20,
                width: "100%",
                height: "100%",
                top:  "0px",
                left: "0px",
                backgroundColor: "#000",
                opacity: "0.75",
                display: "none"
            },
            boxConfig : {
                position : ($.browser.msie) ? "absolute" : "fixed",
                zIndex: 21,
                width: getWidth() + "px",
                marginLeft: "-" + (getWidth() / 2) + "px",
                height: "auto",
                top: "30%",
                left: "50%",
                backgroundColor: "#fff",
                display: "none"
            }
        }, options);

        //inserts floatbox and sets its content
        var showBox = function () {
			var content = typeof settings.content === "string" ? settings.content : settings.content.clone();
            //inserts the background element in the document
            $("<div></div>")
                .bind("click", function () {
                    closeBox();
                })
                .attr("id", settings.bg)
                .css(settings.bgConfig)
                .width(($.browser.msie) ? document.body.clientWidth : "100%")
                .height(($.browser.msie) ? document.body.clientHeight : "100%")
                .appendTo("body");
            //inserts the floating box in the document
            $("<div></div>")
                .attr({id: settings.box, role: "alertdialog"}) 
                .html(content)
                .append(settings.button)
                .css(settings.boxConfig)
                .appendTo("body")
                .css("margin-top", "-" + $("#" + settings.box).height() / 2 + "px")
                .find(".close-floatbox").bind("click", function () {
                    closeBox();
                })
                .end();
            //checks if it needs to fade or not
            if (settings.fade) {
                $("#" + settings.bg)
                .fadeIn(200, function () {
                    $("div#" + settings.box).fadeIn(200);
                });
            } else {
                $("#" + settings.bg)
                .show()
                .parent().find("#" + settings.box).show();
            }
			//sets if ajax is needed(already detectets if it is POST or GET)
			if (settings.ajax) {
				$.ajax({
					type: settings.ajax.params === "" ? "GET" : "POST",
					url: settings.ajax.url,
					data: settings.ajax.params,
					
					beforeSend: function () {
						$("#" + settings.box).html(settings.ajax.before);
					},
					
					success: function (data) {
						$("#" + settings.box)
							.html(data)
							.append(settings.button)
							.find(".close-floatbox").bind("click", function () {
								closeBox()
							})
						.end();
						if (settings.ajax.finish) {
							settings.ajax.finish();
						}
					},
					contentType: "html"
				});
			}
        };
        //hides floatingbox and background
        var closeBox = function () {
            if (settings.fade) {
                $("#" + settings.box).fadeOut(200, function () {
                     $("#" + settings.bg).fadeOut(200, function () {
						$("#" + settings.box).remove();
						$("#" + settings.bg).remove();
                    });
                });
            } else {
				//for opera issues hide first and a timeout is needed to remove the elements
				$("#" + settings.box + ",#" + settings.bg).hide();
				setTimeout(function () {
					$("#" + settings.box).remove();
					$("#" + settings.bg).remove();
				}, 500);
            }
        };
        //inits the floatbox
        var init = function () {
            //shows box
            showBox();
			//adds cross browser event to esc key to hide floating box
            $(document).one("keypress", function (e) {
                var escKey = $.browser.mozilla ? 0 : 27;
                if (e.which === escKey) {
                    closeBox();
                }
            })
			.one("keydown", function (e) {
                var escKey = $.browser.mozilla ? 0 : 27;
                if (e.which === escKey) {
                    closeBox();
                }
            });
			//if msie6, adds event to browser scroll to keep floatbox ina fixed position and uses css hack for full background size
	        if ($.browser.msie) {
	            $("body, html").css({height: "100%", width: "100%"});
	            $(window).bind("scroll", function () {
	                $("#" + settings.box).css("top", document.documentElement.scrollTop +  ($(window).height() / 2) + "px");
	            });
	        }
        };
        //starts the plugin
        init();
    };
})(jQuery);

$(document).ready(function() {
	if(document.getElementById('searchresultspaginator_group1')){
		if(document.getElementById('advancedSearch').style.display != 'none'){
			document.getElementById('searchresultspaginator_group1').style.top = '410px';
		}
		$("#showadvanced").click(function(){
			$("#searchresultspaginator_group1").animate({top:('410px')}, 400);
			$("#CI_search_searchedCDinfo").animate({top:('620px')}, 400);
		});
		$("#hideadvanced").click(function(){
			$("#searchresultspaginator_group1").animate({top:('220px')}, 400);
			$("#CI_search_searchedCDinfo").animate({top:('430px')}, 400);
		});
	}
	processHoverimages();
	
	//registrationbutton in regform mouseover
	$("#btnSubmitRegister").hover(
		function(){
			$(this).addClass("btnSubmitRegisterHover");
		}, function(){
			$(this).removeClass("btnSubmitRegisterHover");
		}
	);
});

function openVehicles() {
	var path="ingramPublishing/vehiclesMain.cfm";
	var helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=945,height=958,left=50,top=50";
	window.open(path, "", helpStr);
}

function processHoverimages(){
	$(".hoverimg").hover(function(){
		origimg = $(this).attr("src");
		altimg = $(this).attr("mouseoversrc");
		if(altimg==null){
			altimg = $(this).attr("src");
			altimg = altimg.substring(0, altimg.length-4) + '_over' + altimg.substring(altimg.length-4);
		}
		$(this).attr("origsrc", origimg);
		this.src = altimg;
	},function(){
		this.src = $(this).attr("origsrc");
	});

	$(".hoverdiv").hover(function(){
		tgtclass = $(this).attr("tgtclass");
		$(this).addClass(tgtclass);
	},function(){
		tgtclass = $(this).attr("tgtclass");
		$(this).removeClass(tgtclass);
	});
}

