﻿$(function () {
    //Tab Navigation
    var selecttab = '#' + $('#selecttab').val();
    $(selecttab).addClass('current');
    $(selecttab).parents('li').addClass('current');
    jQuery('ul.sf-menu').superfish({ pathClass: 'current' });

    if ((QueryStringParameter('searchengine') == 1) || (QueryStringParameter('gclid'))) {
        $('#tab-c-5').hide();
    }

    //Seen Us In An Ad PopUp
    $("a[name=seenus]").mouseover(function (event) { $("#adsearch").show("fast"); });
    $("a[name=seenus]").mouseout(function (event) { $("#adsearch").hide("fast"); });


    $('.BS-Search').each(function () {
        var SearchBox = $('.BS-Search-TextBox', this);
        SearchBox.bind('click', function () { this.select(); });
        $('.BS-Search-Button', this).bind('click', function () { doSearch(SearchBox.val()); });
    });

    $('.BS-EmailSignUp').each(function () {
        var EmailBox = $('.BS-EmailSignUp-TextBox', this);
        EmailBox.bind('click', function () { this.select(); });
        $('.BS-EmailSignUp-Button', this).bind('click', function () { window.location = ('http://www.bettersex.com/createemailprofile.aspx?sc=7HPB&email=' + EmailBox.val()); });
    });
});

function doSearch(val) {
    var searchstr = val;

    if (searchstr.length > 0) {
        var searchstr = searchstr.replace(/ /g, "+");
        var searchstr = searchstr.replace(/\,/g, "");
        var searchstr = searchstr.replace(/\#/g, "");
        var url = $('#EndecaSearchURL').val() + "/search.aspx?st=" + searchstr;
        window.location = url;
    }
    else {
        return false;
    }
}

//function searchBoxKeyPress(e) {

//    if (e.keyCode == 13) {
//        doSearch($('#free_text2').val());
//        e.preventDefault();
//    }
//}

function searchBoxKeyPress(e) {
    if (e.keyCode == 13) {
        doSearch($('#free_text2').val());
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    }
}

function searchKeyPress(e) {
    if (e.keyCode == 13) {
        doSearch($('#ad_text').val());
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    }
}


//TOP TABS 11/15/2011**********************
$(function () {
	var MainMenuSubContent = '/handlers/legacyadvertisements.ashx?adType=Main_Menu_DropDowns';
	
	//temp load all submenus at once //then bind hover event
	$('#TopTabSubMenuHtml').load(MainMenuSubContent,
	function () {
		$(".ttsubmenucontainer").hover(function () {
			showMenuParts($(this).attr('id').replace('DDMenu', ''));
		}, function () {
			hideMenuParts($(this).attr('id').replace('DDMenu', ''));
		});
	});
});


$(function () {
	$(".tt").hover(function () {
		showMenuParts($(this).attr('id').replace('TTMenu', ''));
	}, function () {
		hideMenuParts($(this).attr('id').replace('TTMenu', ''));
	});
});

function showMenuParts(id) {
	
	//some things need to happen regardless of whether there is a menu or not
	var $ttmenu = $('#TTMenu' + id);
    $ttmenu.css('background-position', '0 -27px');

	for (i = 0; i <= 20; i++) {
			if (i != id) {
               toptab_off(i);
			}
		}

	if ($('#DDMenu' + id).length) {
		var $ddmenu = $('#DDMenu' + id);
		var $connectdd = $('#DDMenu' + id + ' .dvConnector');
		var $windowwidth = $(window).width();
		var buffer = 10
		var moveLeft = 0;
		var pos = $ttmenu.offset();
		var width = $ttmenu.width() + 20;
		var height = 26;
		var ddmenuwidth = 200

		//account for missing right side of menu
		if ($('#DDMenu' + id + ' .ttsubmenuright').length) { ddmenuwidth += 200; }
		else {
			$ddmenu.addClass('ttsubmenuSlim');
			$('#DDMenu' + id + ' .ttsubmenuleft').addClass('ttsubmenuleftSlim');
		}

		//move menu left if it's outside the window					
		if ($windowwidth < (pos.left + ddmenuwidth)) { moveLeft = ((pos.left + ddmenuwidth + buffer) - $windowwidth); }

		$ddmenu.css({ "left": (pos.left - moveLeft - 1) + "px", "top": (pos.top + height) + "px", "visibility": "visible" }).show();
		$connectdd.css({ "left": moveLeft + "px", "width": (width) + "px" }).show();
		
		//make sure sub menu is on highest z-index
		$ddmenu.maxZIndex({ inc: 5 });
	}
}

function toptab_off(id) { 
    $('#TTMenu' + id).css('background-position', '0 0');
}

function hideMenuParts(id) {
    setTimeout(function () { //adds a delay (ms) so that menu doesn't flicker on every mouse out
        if ($('#DDMenu' + id).length && $('#TTMenu' + id).ismouseover() == false && $('#DDMenu' + id).ismouseover() == false) {
            toptab_off(id);
            $('#DDMenu' + id).css('visibility', 'hidden');
        }
        //seperated for no drop downs version
        if (!$('#DDMenu' + id).length && $('#TTMenu' + id).ismouseover() == false) {
            toptab_off(id);
        }
    }, 200);
}
