var ajaxViewQS = '';

$(document).ready(function() {
	
		if($('#overlay').find('div.messages').length > 0){
			setTimeout((function(){ 
				$('#overlay').find('div.messages').slideDown('slow');
				$(".edit-pass[name='name']").focus();
				$('.edit-pass').css({'border':'1px #f69044 solid'});
			}),500);
		}

	
	
	// hub roll over
	$('#hub-link').hover(function() {
		$('#log-state').css({'background':'url(/img/nav-hub-loggedin-over.gif) 0 1px no-repeat'});
	},
	function() {
		$('#log-state').css({'background':'url(/img/nav-hub-loggedin.gif) 0 1px no-repeat'});;
	});
	
	
	
	if ($('#carousel').length > 0)
	{
	
		//new homepage logo effect
		jQuery('#carousel').jcarousel({
	    wrap: 'circular',
			auto:1,
			scroll:5,
			visible:5,
			easing:"linear",
			animation:20000,
      itemVisibleInCallback: {onBeforeAnimation: carousel_itemVisibleInCallback},
      itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
	   });
	}
  
	//buttons
	$('#contact-mail-page input#edit-submit').after('<div id="submit-contact-send"></div>');
	
	$('#submit-contact-send').click(function(){
		$("form#contact-mail-page").submit();
	});
	
	$('#user-register input#edit-submit').after('<div id="submit-new-reg"></div>');
	
	$('#submit-new-reg').click(function(){
		$("form#user-register").submit();
	});
	
	$('#user-pass input#edit-submit').after('<div id="submit-email-pass"></div>');
	
	$('#submit-email-pass').click(function(){
		$("form#user-pass").submit();
	});
	
	$('#user-profile-form input#edit-submit').after('<input id="submit-user-pref" type="button"/>');
		
	$('#submit-user-pref').click(function(){
		$("form#user-profile-form").submit();
	});
	
	$('#user-pass-reset input#edit-submit').attr('value', '');
	
  $('#cancel-button').click(function(){
    history.back();
  });

  // Add icons on library page
  if ($('.library-icon').length == 0) {
    $('.views-field-upload-fid-1').before('<img class="library-icon" src="/img/icon-library-list.gif" alt="icon" />');  
  }
  
  // Add dots on newsroom
  if ($('.view-id-news_view li').each(function() {
    $(this).find('div.field-content p:last').append('...');
  }));    
  
  /* Library organization *****************************************************/
  // Filter
  if ($('.library-filter').length > 0) {
    var si = 0;
    if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
      $.each(Drupal.settings.views.ajaxViews, function(i, settings) {
        $.each(settings, function (key, setting) {
          if (si == 0) {
            ajaxViewQS += key + '=' + setting;
          } else if (si > 0) {
            ajaxViewQS += '&' + key + '=' + setting;
          }
          si++;
        });
      });
    }

//console.log('d: ajaxViewQS: ' + ajaxViewQS);


    // add css
    $('#sub-nav').css('background', 'url(/img/bg-filter.gif) no-repeat scroll 100% 20px');

    // Gradient
    $('.gray-gradient').gradient({
      from: 'd4d4d4',
      to: 'ffffff',
      direction: 'horizontal'
    });

    // When clicking BY CATEGORY OR BY YEAR filtering
    $('.filter-category p input').bind('click', function () {     
      var count = 0;
      $('.filter-category p input').each(function () {
        if ($(this).attr('checked')) {
          count = 1;
        }
      });
      if (count == 0) {
        alert('You must have at least one category selected.  Select one or more categories to proceed.');
        return false;
      }
      var year = $('#selected-year').val();
      requestLibraryByFilter(year); 
    });      
    
    // Search
    $('#submit-search').bind('click', function () {
      if ($('#keys').val() == '') {
        return;
      }
      var qs = 'keys=' + $('#keys').val();
      var url = '';      
      
      startProcessing();
      //url = '/views/ajax?view_name=white_paper_view&view_display_id=block_1&view_path=node/314&view_dom_1=1&pager_element=0&' + qs;
      url = '/views/ajax?' + ajaxViewQS + '&' + qs;
      $.get(url, null, resultDetail);
    });    
    
    // pagination
    /*
    if (jQuery.browser.safari) {
      Drupal.attachBehaviors(Drupal.behaviors.ViewsAjaxView);  
    }
    */
  }
});

var t;
// send request library by filter
function requestLibraryByFilter(year) {
  clearTimeout(t);
  var qs = $('#library-form').serialize();
  var url = '';
  
  $('#selected-year').val(year);
        
  if (year != 'all') {
    qs += '&created[min]=' + year + '-01-01&created[max]=' + year + '-12-31';
  }    
  
  //url = '/views/ajax?view_name=white_paper_view&view_display_id=block_1&view_path=node/314&view_dom_1=1&pager_element=0&' + qs;
  url = '/views/ajax?' + ajaxViewQS + '&' + qs;
  t = setTimeout(function() { changeUrl(url, qs) }, 700);
}

function changeUrl(url, qs) {
  startProcessing();
  $.get(url, null, resultDetail);
}

/*
function bindPagination() {
//alert('bindPagination');
  $('ul.pager a').unbind().bind('click', function(e) {
//alert('you clicked a pagination');
    e.preventDefault();
    var url = $(this).attr('href');
    if (url.indexOf('view_name') == -1) {
      url += '&view_name=white_paper_view&view_display_id=block_1&view_path=node/314&view_dom_1=1';
    }
    url = url.replace('/library', 'views/ajax');
    $.get(url, null, resultDetail);
  });
}
*/

var resultDetail = function (data) {
  var result = Drupal.parseJson(data);
  $('#main-content').html('<h1>i3 Forum Library</h1>' + result.display);
  $('#main-content').removeClass('library-overlay');
  if ($('.view-content').length == 0) {
    $('#main-content').append('<div class="no-result-library">There are no results.</div>')
  }
  //bindPagination();
  Drupal.attachBehaviors(Drupal.behaviors.ViewsAjaxView);  

/*
  var isMSIE6 = 0;
  var isMSIE7 = 0;
  var isMSIE8 = 0;
  
  if (jQuery.browser.msie) {
    if (jQuery.browser.version.substr(0,1) == "6") {
      isMSIE6 = 1;
    } else if (jQuery.browser.version.substr(0,1) == "7") {
      isMSIE7 = 1;
    } else if (jQuery.browser.version.substr(0,1) == "8") {
      isMSIE8 = 1;
    }
  } 
  if ((isMSIE6.toString() == '0') && (isMSIE7.toString() == '0') && (isMSIE8.toString() == '0')) {
//console.log('not IE');  
    //bindPagination();
  } else {    
    //bindPagination();  
alert('IE');
    Drupal.attachBehaviors(Drupal.behaviors.ViewsAjaxView);  
  }
*/  
}


function startProcessing() {
  $('.view-white-paper-view .views-row-odd, .view-white-paper-view .views-row-even').css('background', 'url(/img/icon-library-list.gif) no-repeat 20px 60%');
  $('div.view-id-white_paper_view div.view-content div.item-list ul li').css('border-bottom', 'none');
  $('div.view-id-white_paper_view div.view-content div.item-list ul li').css('padding-bottom', '1px');
  $('div.view-content div.item-list ul li.views-row-first').css('border-top', 'none');
  $('div.view-content div.item-list ul li.views-row-first').css('padding-top', '21px');
  $('.no-result-library').css('background', 'none');
  $('.no-result-library').css('border-top', 'none');
  $('.no-result-library').css('border-bottom', 'none');
  $('.no-result-library').css('padding', '41px 0 41px 20px');
 
  $('#main-content').addClass('library-overlay');
  $('#main-content').prepend('<div id="loading-processing"><p>UPDATING CONTENT</p><img src="/img/loading.gif" width="208px" height="13px" alt="" /></div>');
}

