// JavaScript Document
$(document).ready(function() {

    $(window).unload(function() {
        return false;
    });
    squishBox();
    // search box maxlength
    $('#dnn_dnnSEARCH_txtSearch').attr('maxlength', '30');



    // login redirect
    function loginRedirect() {
        $('#__EVENTTARGET').remove();
        $('#__VIEWSTATE').remove();
        $('#__EVENTARGUMENT').remove();
        $newurl = $('#yourlensLoginUrl').val();
        if ($newurl.length < 1) {
            $newurl = "test";
        }
        $('#Form').attr('action', $newurl);
        $('#Form').submit();
    }

    $('#yourlensLogin').click(function() {
        loginRedirect();
    });

    // login focus click
    // Form Focus Events
    $('input, textarea, select').focus(function() {
        $selectedInput = this;
    });

    $('#logininput').keypress(function(event) {
        if (event.keyCode == '13') {
            loginRedirect();
        }
    });

    $('#dnn_dnnSEARCH_txtSearch').focus(function(event) {
        if (event.keyCode == '13') {
            $('#dnn_dnnSEARCH_cmdSearch img').click();
        }
    });

    $('#dnn_ctr3313_ViewFindADoctorStart_ZipCode, #dnn_ctr3313_ViewFindADoctorStart_DoctorOrPracticeName').focus(function(event) {
        if (event.keyCode == '13') {
            $('#dnn_ctr3313_ViewFindADoctorStart_FindADoctor').click();
        }  
    });





    //yourlensLoginUrl

    // get current url
    $url = window.location.pathname;
    $url = $url.split("/");

    // make href of root directory
    // /category/123.aspx returns category.aspx
    // /category.aspx returns category.aspx
    $href = $url[1].split(".");
    $href = $href[0];
    if ($href == "" || $href == "home" || $href == "Home" || $href == "default" || $href == "Default") {
        $href = "/";
    } else {
        $href = "/" + $href + ".aspx";
    }


    // breadcrumb bold
    $('#dnn_dnnBREADCRUMB_lblBreadCrumb a:last').css('font-weight', 'bold');

    // remove breadcrumb on home page
    if ($href == "/") {
        $('.breadcrumb').remove();
    }


    // dnn hacks
    $('a[name]').hide();
    $('#dnn_ctr720_ContentPane td[nowrap]').hide();
    $('.SearchInputContent >  table td:first-child').remove();


    // rollover
    // first <li>

    if ($href == "/") {
        $('.top_menu li:first-child a').addClass('activeB');
    } else {

        $('.top_menu li').each(function() {

            $thisref = $('a ', this).attr('href');
            //alert($thisref);
            if ($thisref == $href) {

                $prev = $(this).prev('li');
                $prev.find('a').addClass('activeA');
                $('a ', this).addClass('activeB');
            }

        });
        $('.top_menu li a[href="' + $href + '"]').load(function() {

        });
    }

    /********************
    * LIST STYLE - HYPHEN
    *********************/
    $('.hyphen li').prepend('- ');

    /********************
    * EXPANDABLE MENU's
    *********************/
    $('.expandable h4').each(function() {
        $visible = $(this).next('ul:visible').length;
        if ($visible) {
            $(this).addClass('open');
            $(this).removeClass('closed');
        } else {
            $(this).addClass('closed');
            $(this).removeClass('open');
        }
    });
    $('.expandable h4').click(function() {
        $visible = $(this).next('ul:visible').length;
        if ($visible) {
            $(this).next('ul').slideUp();
            $(this).removeClass('open');
            $(this).addClass('closed');
        } else {
            $(this).next('ul').slideDown();
            $(this).addClass('open');
            $(this).removeClass('closed');
        }
    });

    /********************
    * Eyecare Provider Input boxes - text switching
    *********************/

    $find_zip_id = "#dnn_ctr3313_ViewFindADoctorStart_ZipCode";
    $find_doc_id = "#dnn_ctr3313_ViewFindADoctorStart_DoctorOrPracticeName";

    $cp_doctor = $($find_doc_id).val();
    $cp_zip = $($find_zip_id).val();

    $($find_doc_id).focus(function() {
        $(this).val('');
    });
    $($find_doc_id).focusout(function() {
        $this_val = $(this).val();
        if ($this_val == "") {
            $(this).val($cp_doctor);
        }
    });

    $($find_zip_id).focus(function() {
        $(this).val('');
    });
    $($find_zip_id).focusout(function() {
        $this_val = $(this).val();
        if ($this_val == "") {
            $(this).val($cp_zip);
        }
    });

    $('#Form').submit(function() {
        $cp_doctor2 = $($find_doc_id).val();
        $cp_zip2 = $($find_zip_id).val();
        if ($cp_doctor == $cp_doctor2) {
            $cp_doctor2 = $($find_doc_id).val('');
        }
        if ($cp_zip == $cp_zip2) {
            $($find_zip_id).val('');
        }
    });

});



function squishBox(){	
	var rows = $('.product_squares .row');
	$cnt = rows.length;
	$i = $cnt - 1;
	while($i > 0){
		$left_height = $('#dnn_LEFTPANE').height();
		$main_height = $('#dnn_CONTENTPANE').height();
		if($left_height < $main_height - 100){
			rows.eq($i).hide();
		}
		$i--;
	}
}

