/*
 * SimpleModal Searchbox Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2008 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: searchbox.js 170 2008-12-04 19:03:12Z emartin24 $
 *
 */
var searchUrl = '/searchnet';


function searchMain(keyword)
{
    $.get(searchUrl+'/ajax/1/curchannel/'+searchBox.curchannel, function(data){
         // create a modal dialog with the data
    searchBox.type = 'keyword';
         searchBox.keyword = keyword;
         $(data).modal({
            close: false,
            position: [top],
            overlayId: 'searchbox-overlay',
            containerId: 'searchbox-container',
            onOpen: searchBox.open,
            onShow: searchBox.show,
            onClose: searchBox.close,
                opacity: 85
         });
      });
      
    //  searchBox.search(dialog);
}
$('.divSearchIcon').click(function(){
    searchBox.curchannel = '';
    if(idChannelCurrent!=undefined)
            searchBox.curchannel = idChannelCurrent;
    searchBox.keyword = '';
    searchBox.channel = '';
    searchBox.type = 'keyword';
    $.get(searchUrl+'/ajax/1/curchannel/'+searchBox.curchannel, function(data){
         $(data).modal({
            close: false,
            position: [top],
            overlayId: 'searchbox-overlay',
            containerId: 'searchbox-container',
            onOpen: searchBox.open,
            onShow: searchBox.show,
            onClose: searchBox.close,
                opacity: 85
         });

      });
});
function searchTag(keyword,channelId)
{
    $.get(searchUrl+'/ajax/1/curchannel/'+searchBox.curchannel, function(data){
         // create a modal dialog with the data
         searchBox.keyword = keyword;
         searchBox.channel = channelId;
         searchBox.type = 'tags';
         $(data).modal({
            close: false,
            position: [top],
            overlayId: 'searchbox-overlay',
            containerId: 'searchbox-container',
            onOpen: searchBox.open,
            onShow: searchBox.show,
            onClose: searchBox.close,
                opacity: 85
         });
      });
}

var searchBox = {
    keyword: '',
    type:'',
    channel: '',
    curchannel:'',
    sort: 'date',
    order: 0,
    default_text: '',
   open: function (dialog) {
      var h = 711;
      //$('.jsselect').selectbox({debug: false});
      var title = $('#searchbox-container .searchbox-title').html();
      $('#searchbox-container .searchbox-title').html('Loading...');
        searchBox.default_text = 'input text';
      dialog.overlay.fadeIn(200, function () {
         dialog.container.fadeIn(200, function () {
            $('#searchbox-container .searchbox-content').hide();
            dialog.data.fadeIn(200, function () {
               $('#searchbox-container .searchbox-content').animate({
                  height: h
               }, function () {
                  $('#searchbox-container .searchbox-title').html(title);
                  $('#searchbox-container form').fadeIn(200, function () {
                     $('#searchbox-container #searchbox-name').focus();
                     // fix png's for IE 6
                     if ($.browser.msie && $.browser.version < 7) {
                        $('#searchbox-container .searchbox-button').each(function () {
                           if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
                              var src = RegExp.$1;
                              $(this).css({
                                 backgroundImage: 'none',
                                 filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
                              });
                           }
                        });
                     }
                  });
               });
            });
         });
      });

      if(searchBox.type =='')searchBox.type = $('#searchtype').val();
      if (searchBox.keyword!=''){
          searchBox.search(dialog,0);
      } else {
          searchBox.keyword = $('#searchbox-container .searchbox-input').val();
      }
   },
   show: function (dialog) {
        searchBox.update(dialog);

        $('#searchbox-container .searchbox-input').focus(function(){
                if($(this).val()==searchBox.default_text)$(this).val('');
        });

   },
   close: function (dialog) {
      $('#searchbox-container .searchbox-content').fadeOut(200
, function () {
         dialog.data.fadeOut(200, function () {
            dialog.container.fadeOut(200, function () {
               dialog.overlay.fadeOut(200, function () {
                  $.modal.close();
               });
            });
         });
      });
   },
   error: function (xhr) {
      alert(xhr.statusText);
   },
   update: function(dialog){
       $('.jsselect').selectbox({inputClass: "selectbox1",containerClass: "selectbox1-wrapper",  debug: false});
       $('#searchbox-container .searchbox-error').click(function(){
           $(this).hide();
           $('#searchbox-container .searchbox-input').focus();
           });
        $('.modalCloseX').click(function(){
                searchBox.close(dialog);
        });
        $('th.date').click(function(){
                searchBox.channel = '';
            if(searchBox.sort=='date'){
                searchBox.order = !searchBox.order;
            } else {
                searchBox.sort = 'date';
                searchBox.order = false;
            }
            searchBox.search(dialog,false);
        });
        $('th.title').click(function(){
                searchBox.channel = '';
            if(searchBox.sort=='title'){
                searchBox.order = !searchBox.order;
            } else {
                searchBox.sort = 'title';
                searchBox.order = false;
            }
            searchBox.search(dialog,false);
        });
        $('th.speacker').click(function(){
                searchBox.channel = '';
            if(searchBox.sort=='speacker'){
                searchBox.order = !searchBox.order;
            } else {
                searchBox.sort = 'speacker';
                searchBox.order = false;
            }
            searchBox.search(dialog,false);
        });
        $('th.channel').click(function(){
                searchBox.channel = '';
            if(searchBox.sort=='ministry'){
                searchBox.order = !searchBox.order;
            } else {
                searchBox.sort = 'ministry';
                searchBox.order = false;
            }
            searchBox.search(dialog,false);
        });
      $('#searchbox-container .searchbox-button').click(function (e) {
         e.preventDefault();
         searchBox.type = 'keyword';
         if(searchBox.getKeyword())
            searchBox.search(dialog,false);
        });
        $('#searchbox-container form').submit(function (e) {
         e.preventDefault();
         searchBox.type = 'keyword';
         if(searchBox.getKeyword())
            searchBox.search(dialog,false);
        });
        $('#searchbox-container .page').click(function (e){
            e.preventDefault();
            searchBox.search(dialog,$(this).attr('id'));
        });
    },
    search: function(dialog, page){


            $('#searchbox-container .searchbox-error').hide();
            $('#searchbox-container .searchbox-loading').fadeIn(200, function () {
//		curChannel=0;
		    if(page){
			curChannel=$('#idChannelPrev').val();
		    } else {
			curChannel=$('#network').val()
		}
                    $.ajax({
                            url: searchUrl+'/ajax/1/s/'+searchBox.type+'/curchannel/'+searchBox.curchannel+'/k/'+searchBox.keyword+'/pg/'+page+'/sort/'+searchBox.sort+'/order/'+searchBox.order+'/channel/'+searchBox.channel+'/network/'+curChannel,
                            data: {/*ajax:1, s:searchBox.type, curchannel:searchBox.curchannel, k:searchBox.keyword, pg:page, sort : searchBox.sort, order:searchBox.order, channel:searchBox.channel, network:curChannel*/},
                            type: 'get',
                            complete: function (xhr) {
                                if(xhr.responseText!='error'){
                                    $('#searchbox-container .searchbox-content').html(xhr.responseText);
                                    searchBox.update(dialog);
                                }
                                else {
                                    $('.searchbox-loading').hide();
                                    $('#keyword').html(searchBox.keyword);
                                    $('.searchbox-error').show();
                                }
                            }
                    })
            });
    },

   showError: function () {
      $('#searchbox-container .searchbox-message')
         .html($('<div class="searchbox-error">').append(searchbox.message))
         .fadeIn(200);
   },
   getKeyword: function(){
        searchBox.keyword = $('#searchbox-container .searchbox-input').val();
            if (searchBox.keyword =='' | searchBox.keyword == searchBox.default_text){
                alert('You must enter keyword');
                $('#searchbox-container .searchbox-input').focus();
                return false;
            }
            return true;
    }
};
