/**
 * ALL JAVASCRIPT NEEDS TO BE IN SEPARATE NAMESPACES
 * grone_main namespace
 */

var grone_main = {

    initHyloSearchBox: function() {
        // use javascript to bind the search box to hylo
        // this also makes sure we have the default search as a fallback
        // in case the client has no javascript!
        var url = window.location.href
        jq('.gr-google-search form').attr('action', '/hylo_results');
        jq('.gr-google-search input[name=SearchableText]').attr('name', 'hyloq');
        var page = '<input type="hidden" name="page" value="' + url + '"/>'
        jq('.gr-google-search form').append(page);
    },

    initNavigationSorter: function() {

        var base = jq('base').attr('href');
        jq("#navigation_list").sortable({
            items: "li",
            update:function(ev, ui) {
                jq.ajax({
                    type: "POST",
                    url: base + "/reorder_navigation",
                    data: "action=sort&" + jq(this).sortable("serialize")
                });
            }
        });
    },

    initPerspectiefSorter: function() {

        var base = jq('base').attr('href');
        jq("#perspectief_list").sortable({
            items: "li",
            update:function(ev, ui) {
                jq.ajax({
                    type: "POST",
                    url: base + "/reorder_perspectief",
                    data: "action=sort&" + jq(this).sortable("serialize")
                });
            }
        });
    },

    highlightWidget: function() {
        jq('#gr-highlight-text .gr-text').hover( function () {

          var index = jq(this).attr('id').substr(8);
          var bg = '.gr-background';
          var selector = '#gr-highlight-text .gr-text';
          var selected = 'selected';

          jq('div[class^="gr-background"]').hide();
          jq(selector).removeClass(selected);
          
          jq(this).addClass(selected);
          jq('#gr-highlight-' + index).show();
        });
    },

    initLanguageSwitcher: function() {
      jq('#gr-utility-menu .language').show();

      var url = new String(window.location).split('/');

      // yes, this is silly, but hey, so is the language switcher
      var lang = 'nl';
      for(i=0; i <= url.length; i++) {
          if(url[i] == 'english' || url[i] == 'deutsch') {
              lang = url[i].substring(0,2);
          }
      }
      if( lang != 'nl' ) {
          jq('.language li').each(function() {
             jq(this).removeClass('active');
          });
          jq('.language li#' + lang).addClass('active');
      }
      jq('#gr-utility-menu .language li a').click(function(event) {
          event.preventDefault();
          jq('#language-switcher-form #lang').val(jq(this).attr('title'));
          jq('#language-switcher-form').submit();
      });
    },

    initMoreItems: function() {

        /* script for more items: without javascript, no image is shown
           and all list items will be displayed */

        jq("img.show-more-items[src='meer.png']").hover(
            function() {
                jq(this).attr('src','meer-hover.png');
            },
            function() {
                jq(this).attr('src','meer.png');
            }
        );

        jq("img.show-more-items[src='minder.png']").hover(
            function() {
                jq(this).attr('src','minder-hover.png');
            },
            function() {
                jq(this).attr('src','minder.png');
            }
        );

        jq("img.show-more-items").click(function() {
            src = jq(this).attr('src');
            if(src.match('^meer')) {
                jq(this).attr('src','minder.png');
                jq(this).next().children('li').show();
                jq(this).next().addClass('show');
                jq(this).hover(
                    function() {
                        jq(this).attr('src','minder-hover.png');
                    },
                    function() {
                        jq(this).attr('src','minder.png');
                    }
                )
            } else {
                jq(this).attr('src','meer.png');
                jq(this).next().children('li').hide();
                jq(this).next().removeClass('show');
                jq(this).hover(
                    function() {
                        jq(this).attr('src','meer-hover.png');
                    },
                    function() {
                        jq(this).attr('src','meer.png');
                    }
                )
            }
        });
    },

    initAdditionalClasses: function() {
        if(jq('#groningen-theme-footer-portlets').length > 0) {
            jq('#groningen-theme-footer-portlets dl.portlet:last').addClass('last');
        }

    },

    initDomeinPage: function() {

        /* set each row height to the largest element in that row */

        function Rows() {}

        var rows = new Rows();

        var colsPerRow = 4;
        var colId = 0;
        var rowId = 0;

        // create rows 'n columns
        jq(".rubriek").each(function() {

            if( colId == colsPerRow ) {
                colId = 0;
                rowId++;
            }

            if( rows[rowId] ) {
                cols = rows[rowId];
                cols.push(jq(this));
                rows[rowId] = cols;
            } else {
                rows[rowId] = [];
                rows[rowId].push(jq(this));
            }

            colId++;
        });

        // loop through rows
        for(row in rows) {

            rubrieken = rows[row];
            maxHeight = 0;

            // discover largest element
            jq(rubrieken).each(function() {
                height = jq(this).height() +10;
                if( height > maxHeight) {
                    maxHeight = height;
                }
            });

            // set all elements to largest element's size
            jq(rubrieken).each(function() {
                jq(this).height(maxHeight);
            });
        }
    },

    initProductView: function() {
        if(jq('body.template-product_view').length > 0) {
            jq('#maakafspraak').prependTo('#inside-portlet-right').show();
            jq('#aanvragendigitaal').prependTo('#inside-portlet-right').show();
        }
    }
}

jq('html').addClass('js');
jq(document).ready(function() {
    grone_main.initAdditionalClasses();
    grone_main.initHyloSearchBox();
    grone_main.initNavigationSorter();
    grone_main.initPerspectiefSorter();
    grone_main.highlightWidget();
    grone_main.initLanguageSwitcher()
    grone_main.initMoreItems();
    grone_main.initDomeinPage();
    grone_main.initProductView();
});

