﻿if (Drupal.jsEnabled) {
    $(function() {
        /******************* content share ********************/
        $('.share-with-social-networks').click(function(event) {
            event.preventDefault();
            $(this).blur();
            var html = $(this).parent().children('div.content-share-tools').html();
            var Message = $('div#SystemMessage');
            Message.dialog("option", "width", "200");
            Message.dialog("option", "height", "300");
            Message.dialog("option", "modal", true);
            Message.html(html).dialog('open');
        });
    });


    $(function() {
        $(document).ready(function() {
            $('#SystemMessage').dialog({
                'autoOpen': false,
                'width': 500,
                'height': 450,
                'modal': true,
                'resizable': false,
                'draggable': false,
                'buttons': { 'Close': function() { $(this).dialog("close"); $(this).html(''); } },
                'title': 'Guitar.com'
            });
        });
    });

    /******************* main navigation ***********/
    $(function() {
        $('.drop-down > ul > li').click(function(event) {
            event.stopPropagation();
            var href = $(this).children('a').attr('href');
            window.location = href;
        });
        $('#main-nav > ul > li').click(function(event) {
            event.stopPropagation();
            var href = $(this).children('a').attr('href');
            window.location = href;
        });
    });





    $(function () {
        var closetimer = 0;
        var visited = 0;
        $('ul li.arrow, ul li.arrow-active').bind('mouseover', function () {
            cancelTimer();
            $('.drop-down').hide();
            var id = $(this).attr('id').replace('dropdown-identification-', 'dropdown-');
            var dd = $('#' + id);
            //position the drop-down menu
            var pos = $(this).position();
            var height = $(this).height();
            var heightOffset = 52;
            var widthOffset = 10;
            //position the menu either left, right, or center
            var determinator = 960 - pos.left - dd.width(); //pagewidth - menuitem position - width of the dropdown
            //alert((pos.left - dd.width()) + ',' + pos.left + ',' + dd.width());
            if (determinator > 0) {
                dd.css({ "left": pos.left + "px", "top": (heightOffset) + "px", "z-index": 3999 });
            }
            if (determinator < 0) {
                var newpos = pos.left - dd.width() + $(this).width() + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
                if (newpos > 0) {
                    dd.css({ "left": newpos + "px", "top": (heightOffset) + "px", "z-index": 3999 });
                } else {
                    dd.css({ "left": widthOffset + "px", "top": (heightOffset) + "px", "z-index": 3999 });
                }
            }
            //show the drop-down menu
            dd.show();
        });
        $('ul li.arrow, ul li.arrow-active').bind('mouseleave', function () {
            setTimer();
        });
        $('.single-item').bind('mouseenter', function () { $('.drop-down').hide(); });
        $('.drop-down').bind('mouseenter', function () {
            //grab the menu item
            var parentItem = $(this).attr('id').replace('dropdown-', 'dropdown-identification-');
            $('#' + parentItem).addClass('hoverEffect');
            cancelTimer();
        });
        /* timer functions */
        $('.drop-down').bind('mouseleave', function () {
            var ThisItem = $(this).attr('id');
            //alert($('#' + ThisItem).attr('id'));

            $('#' + ThisItem).bind('mouseleave', function () {
                //alert($(this).attr('id'));
                var parentItem = $(this).attr('id').replace('dropdown-', 'dropdown-identification-');
                //alert(parentItem);
                $('#' + parentItem).removeClass('hoverEffect');
                $(this).hide();
            });

        });
        function setTimer() {
            closetimer = window.setTimeout(hideDropdowns, 500);
        }
        function cancelTimer() {
            if (closetimer) {
                window.clearTimeout(closetimer);
                closetimer = null;
            }
        }
        function hideDropdowns() {
            $('.drop-down').hide();
        }


    });
    /******************* alert boxes ***********/
    $(function() {
        $('.gui-alert').live('click', function(event) {
            event.preventDefault();
            var id = $(this).attr('href');
            $(id).dialog({
                autoOpen: true,
                buttons: {
                    Okay: function() { $(this).dialog('close'); }
                }
            });
        });
    });

    /********************* tabs ********************/
//    $(function() {

//        $(document).ready(function() {
//            $('.gtabs').each(function() {
//                var active = $(this).find('ul.gtabs-menu > li.active');
//                var pane = active.children('a').attr('href');
//                $(pane).show();
//            });

//        });

//        $('ul.gtabs-menu > li > a').live('click', function(event) {
//            $(this).blur(); //remove focus lines
//            event.preventDefault(); //prevent link action
//            var ajax = $(this).attr('ajax');
//            var url = $(this).attr('url');
//            if (url) {
//                window.location = url;
//                return true;
//            }
//            var href = $(this).attr('href');
//            $(this).parent().parent().find('li.active').removeClass('active'); //remove active class from all items
//            $(this).parent().addClass('active'); //add active class
//            if (ajax) //fetch data from ajax call
//            {
//                var url = '/' + ajax;
//                $.getJSON(url, function(data) {
//                    var html = data.toString();
//                    if (html)
//                        $(href).html(data.toString());
//                    else
//                        $(href).html('error');

//                });
//            }
//            $('.gtabs > div').hide(); //hide all panes
//            $(href).show(); //show selected pane
//        });
//    });
    /************ layout global functions ***********/
    $(function() {
        $('#search').bind('click', function() {
            if ($(this).hasClass('emptySearchBox')) {
                $(this).val('');
                $(this).removeClass('emptySearchBox');
            }
            return false; //prevent bubbling up the tree
        });
        $('#search-ajax-results').bind('mouseleave', function() {
            $('#search-ajax-results').fadeOut().html(''); //remove autocomplete results
        });
        $('#search').bind('blur', function() {
            if ($(this).val() == '') {
                $(this).val('输入关键字');
                $(this).addClass('emptySearchBox');
            }

        });
        $('#search-label').click(function(event) {
            event.stopPropagation(); //prevent bubbling down the tree
            var value = $('#search').val();
            if (value) {
                document.forms["search-redirect-form"].submit();
            }
            return false; //prevent bubbling up the tree
        });
        /*** autocomplete *********/
		/*
        $('#search').bind('keyup', function() {
            var value = $(this).val();
            var that = $(this);
            if (value.length > 3) {
                var url = '/g/gpages/ajax-search/' + value;

				//$.getJSON(url, function(data) {
                //    if (data.length > 0) {
                //        var results = $('#search-ajax-results');
                //        var html = data.toString();
                //        var pos = that.parent().position();
                //        var left = pos.left + 5;
                //        var top = pos.top + 39;
                //        results.css({ "left": left + "px", "top": top + "px", "z-index": 3999 });
                //        results.html(html).fadeIn();
                //    }
                //});
				
				 var results = $('#search-ajax-results');
				 var pos = that.parent().position();
				 alert(pos.left);
				 var html = url;
                 var left = pos.left + 5;
                 var top = pos.top + 39;
                 results.css({ "left": left + "px", "top": top + "px", "z-index": 3999 });
                 results.html(html).fadeIn();

            }
        });
		*/
    });

    /************* post an alert message ********************/
    /*
    $('a.user_alert').live('click', function(event) {
        event.preventDefault();
        $(this).blur();
        var form = $(this).attr('href').replace('#', '');
        var post = $('#' + form.toString()).serialize();
        var url = '/ajax/gusers/alert';
        $.post(url, post, function(response) {
            if (response.success) {
                var html = response.message.toString();
                $('#SystemMessage').html(html).dialog('open');
            } else {
                alert('Could not perform this request. Please contact the system administrator: admin@guitar.com.')
            }
        }, "json");
    });
    */


} 

