/* jQuery Initialization */
$(document).ready(function () {
    // Initialize main menu
    $('.bfw_sub_lvl_2').parent('li').addClass('bfw_sub_lvl_1_item');

    $('li.bfw_top_lvl').hoverIntent( // jquery.hoverIntent.js plugin
		function () { $('.bfw_sub_lvl_1', this).show(); },
		function () { $('.bfw_sub_lvl_1', this).hide(); }
	);
    $('.bfw_sub_lvl_1_item').hoverIntent( // jquery.hoverIntent.js plugin
		function () { $('.bfw_sub_lvl_2', this).show(); $(this).addClass('current'); },
		function () { $('.bfw_sub_lvl_2', this).hide(); $(this).removeClass('current'); }
	);

    // Log in toggle
    $('#bfw_btn_log_in').click(function () {
        $('#bfw_log_in_box').toggle();
        return false;
    });

    // Clear search value on focus
    var defaultMessage = $('#bfw_search_text').val();
    //var defaultMessage = "enter your search phrase";
    $('#bfw_search_text').focus(function () {
        var searchText = $(this).val();
        if (searchText == defaultMessage) {
            $(this).val('').css('color', '#333333');
        }
    });
    $('#bfw_search_text').blur(function () {
        var searchText = $(this).val();
        if (searchText == '') {
            $(this).val(defaultMessage).css('color', '#999999');
        }
    });

    // Dynamic Bookcover Icons
    //
    // This script automatically adds media icons to the specified bookcovers.
    //
    // How to use:
    // Add a class name 'bfw_thumb' to any <img> tag in HTML to activate this script and use one of the following format types as 'title' attribute.
    //
    // Available formats:
    // as_testbank, audiovrtn, blog, cd, combbinding, download, ebook, hardback, hardcover_ie, im, looseleaf, paperback, present, prtl_class, puzzle, spiral, tradeup, transp, video, website
    //
    // Example:
    // <img class="bfw_thumb" title="ebook" src="cover.jpg" alt="Example 1" />
    // <img class="bfw_thumb" title="looseleaf" src="cover2.jpg" alt="Example 2" />

    var imagePath = '/catalog/css/resources/common/images/icons/'; // path to the icons

    // Call this function to dynamically add media icons to the bookcovers
    function addThumb(mediaType, coverHeight, coverWidth, target) {
        var filename = 'thumb_media_' + mediaType + '.gif'; // icon file name
        var thumbWidth = 22;
        var thumbHeight = 23;

        ////put thumbnail bottom right
        //        var offsetX = (-thumbWidth + 3); // (icon width) + (offset amount)
        //        var offsetY = coverHeight + 4 - thumbHeight + 3; // (bookcover height) + (borders) - (icon height) + (offset amount)

        ////put thumbnail top left
        if (coverWidth == 0) { coverWidth = 65 }; // Hard-Coded as JQuery cannot figure out the height/width if image is invisible.
        var offsetX = (-coverWidth - 5); // (icon width) + (offset amount)
        var offsetY = -4; // (bookcover height) + (borders) - (icon height) + (offset amount)


        var thumb = new Image();
        // places media icon next to the bookcover image then adds a class name. CSS handles the positioning of the icon.
        $(thumb).insertAfter(target)
			.attr({ src: imagePath + filename, alt: mediaType, title: mediaType })
			.addClass('bfw_thumb')
			.css({ 'width': thumbWidth + 'px', 'height': thumbHeight + 'px', 'margin-top': offsetY + 'px', 'margin-left': offsetX + 'px', 'z-index': 500 });
    }

    function addAllThumbs() {
        $('img.bfw_cover').each(function () {
            var imgHeight = $(this).height(); // get height of current bookcover
            var imgWidth = $(this).width(); // get height of current bookcover

            var format = $(this).attr('title');
            var selector = $(this);
            if (format != null && format != "") {
                addThumb(format, imgHeight, imgWidth, selector);
            }
        });
    }

    //    function addAllThumbs() {
    //        $('img.bfw_cover').each(function () {
    //            $(this).evenIfHidden(function (element) {
    //                var imgHeight = element.height(); // get height of current bookcover
    //                var imgWidth = element.width(); // get height of current bookcover

    //                var format = element.attr('title');
    //                var selector = element;
    //                if (format != null && format != "") {
    //                    addThumb(format, imgHeight, imgWidth, selector);
    //                }
    //            });
    //        });
    //    }

    //    function addAllThumbs() {
    //        $('img.bfw_cover').each(function () {
    //            var imgHeight = $(this).evenIfHidden(function (element) {
    //                return element.height();
    //            });
    //            var imgWidth = $(this).evenIfHidden(function (element) {
    //                return element.width();
    //            });

    //            alert('imgWidth = ' + imgWidth);
    ////            alert('imgHeight = ' + imgHeight);

    //            var format = $(this).attr('title');
    //            var selector = $(this);
    //            if (format != null && format != "") {
    //                addThumb(format, imgHeight, imgWidth, selector);
    //            }
    //        });

    //    }


    // Looks for images with 'bfw_cover' class name and adds the icons
    $(window).load(function () {
        addAllThumbs();
    });
});

function Login() {
    var email = document.getElementById('bfw_log_in_email');
    var pwd = document.getElementById('bfw_log_in_password');

    //check to see if the login values have been entered
    if (email.value == '') {
        alert('Please enter your email address.');
        return;
    }

    if (pwd.value == '') {
        alert('Please enter your password.');
        return;
    }

    //construct the URL
    var loginURL = '/bookbag/login/autologin.aspx?UserEmail=' + email.value + '&UserPassword=' + pwd.value + '&autologin=1';

    window.open(loginURL);
    location.reload(true);
}

function ShowProductResourcesColorBox(currentItem, seoName) {
    var divDataName = '#divValueResource_' + seoName;
    $(currentItem).colorbox({ inline: true, href: divDataName, innerWidth: '650px', scrolling: false });
}

function ShowProductValuePackagesColorBox(currentItem, bookkey) {
    var divDataName = '#divValueTitle_' + bookkey;
    $('#bfw_available_options .bfw_link').colorbox({ inline: true, href: divDataName, innerWidth: '700px' });
}

function ShowViewDemoColorBox(currentItem, seoName) {
    var divDataName = '#divCMSViewDemo_' + seoName;
    $(currentItem).colorbox({ inline: true, href: divDataName, innerWidth: '475px', title: '' });
}

function ShowProductDetailColorbox(ctrl, URL, width, height) {
    $(".bfw_video_link").colorbox({ href: URL, iframe: true, innerWidth: width, innerHeight: height, scrolling: false });
}

function DownloadFile(id) {
    var rdGroupSel = document.getElementsByName('rdGroup_' + id);
    for (i = 0; i < rdGroupSel.length; i++) {
        if (rdGroupSel[i].checked == true) {
            window.open(rdGroupSel[i].value, 'Download');
            return false;
        }
    }

    alert('Please select an item to download !!!');
    return false;
};
