$(document).ready(function() {
	$('#resultsDiv .sr_item_border, #resultsDiv .sg_item_border').live('click',function() {
        var theLink = $(this).find('a:first').attr('href');
        top.location.href = theLink;
    });

	// show nav submenu 
	$('li.userLoggedIn').hover(function() { 
        $('ul', this).stop(true, true).fadeIn(250);
    }, function() { 
		$('ul', this).fadeOut(250);
    });

	$("select#categoryID").bind('change', function() {
		var mainCatID = $(this).val();
		getSubCats(mainCatID);							 
	});
	
	function getSubCats(mainCatID) {
		$.ajax({ 
			cache: false,
			type: "GET",
			url: "/components/SiteFunctions.cfc?method=GetSubCategory&blank=1&ReturnFormat=json",
			dataType: "json",
			data: {
				"CategoryID": mainCatID,
				"id": "subCategoryID",
				"name": "subCategoryID",
				"class": "required"
			},
			success: function(data) {
				$("div#getSubCats.searchtop").html(data);
			},
			error: function(msg) {
				alert(msg);
			}
		});	
	}

    /* Setup modal window */
    $.superbox();

    /* Setup all tooltip calls */
    $('#showTip').tipsy({
        trigger: 'focus',
        gravity: 's'
    });
    $('a#dialog_link,a#dialog_link2, #tooltip2, #tooltip8, #tooltip9').tipsy({
        trigger: 'hover',
        gravity: 's'
    });
    $('#tooltip3, #tooltip7').tipsy({
        trigger: 'hover',
        gravity: 'n'
    });
	
	$("form#SellForm").validate({
		errorElement: "div",
		wrapper: "label",  // a wrapper around the error message
		errorPlacement: function(error, element) {
		   ErrorMessage(error, element);
		}
	});

    $('#closebtmbox').click(function() {
        runEffect_feedback_btm();
    });

    function runEffect_feedback_btm() {
        var display = $("div#effect_feedback_btm").css("display");

        if (display == "none") {
            var height = 230;

            $("div#effect_feedback_btm").css({
                "height": "0px",
                "opacity": "0"
            }).show();
            $("div#effect_feedback_btm").animate({
                "height": height + "px",
                "bottom": "0px",
                "opacity": "0.99"
            }, 400);
        } else {
            $("div#effect_feedback_btm").animate({
                "height": "0px",
                "bottom": "0px",
                "opacity": "0"
            }, 400, function() {
                $(this).hide();
            });
        }
    }
	
	$("input#dialog_feedback_link").click(function() {
        runEffect_feedback_btm();
        return false;
    });
	
    function sendFeedback(email, comment, lh, url, type) {
        var setType = type;
        $.ajax({
            cache: false,
            type: "GET",
            url: url,
            data: {
                email: email,
                comment: comment,
                lh: lh
            },
            success: function() {
				$("div#feedbackFormDiv_btm").hide();
				$("div#feedbackThankYou_btm").show();
				setTimeout(function() {
					runEffect_feedback_btm();
				}, 1500);

				setTimeout(function() {
					$("div#feedbackThankYou_btm").hide();
					$("input#feedbackEmail_btm").val("");
					$("textarea#feedbackComments_btm").val("");
					$("div#feedbackFormDiv_btm").show();
				}, 2000);
            },
            error: function(msg) {
                alert(msg);
            }
        });
    }

    $("#feedback_form_btm").validate({
        errorElement: "div",
        wrapper: "label",
        submitHandler: function(form) {
            var email = $("input#feedbackEmail_btm").val();
            var comment = $("textarea#feedbackComments_btm").val();
            var lh = $("input[@name='feedbackOpinion_btm']:checked").val();
            var url = '/components/SiteFunctions.cfc?method=fcnFeedbackLeave&blank=1';
            sendFeedback(email, comment, lh, url, 'bottom');
        },
        errorPlacement: function(error, element) {
            ErrorMessage(error, element);
        }
    });	
	
	// run browser info
	getBrowserInfo();
});

/* Function to add to bookmarks
 * Checks browser types and add/show functionality */
function getBrowserInfo() {
	var ua = window.navigator.userAgent;
	var msie = ua.indexOf("MSIE");

	if (ua.indexOf("MSIE") > 0) {
		$("#bookmarkUs").show();
		return "IE";
	} 
	else if (ua.indexOf("Firefox") > 0) {
		$("#bookmarkUs").show();
		return "FF";
	}
	else {
		$("#bookmarkUsMsg").show();
		$("#bookmarkUs").hide();
	}
}

// * Close superbox window */
function closeThisBox() {
    setTimeout(function() {
        $("P.close A").click();
    }, 600);
}

function BookmarkUs(obj, title, url) {
    var getBInfo = getBrowserInfo();
    if (getBInfo == "IE") {
        window.external.AddFavorite(url, title);
        return false;
    }
    if (getBInfo == "FF") {
        window.sidebar.addPanel(title, url, "");
        return false;
    }
    if (getBInfo == "OP") {
        obj.setAttribute('href', url);
        obj.setAttribute('title', title);
        obj.setAttribute('rel', 'sidebar');
        obj.click();
        return false;
    }
}
//cancel feedback btn click
cancelFB = function(pos) {
    $('#close' + pos + 'box').click();
    if (pos == 'top') {
        pos = '';
    } else {
        pos = '_' + pos;
    }
    $('#feedbackComments' + pos).val('');
};

/* Function avoids ajax IE caching issues
 * Appends unique var using a timestamp */
var myTimestamp = function() {
    doNewTimestamp = new Date();
    return doNewTimestamp.getTime();
};

/* Display error messages */
function ErrorMessage(error, element) {
    offset = element.position();
    error.insertBefore(element);
    error.addClass('message').hide().fadeIn();
    error.css('position', 'absolute');
    error.css('left', offset.left + element.outerWidth());
    error.css('top', offset.top);
    error.css('z-index', '500');
}
