Drupal.behaviors.allthelooksSearchPhoto = function (context) {

  var current_url = window.location.href;
  $('.view-filters .list .list_icon').click(function() {
      if (!$(this).hasClass('active')){
	$(this).addClass('active');
        $('.view-filters .grid .grid_icon').removeClass('active');
	var reload_url = get_search_page_url(current_url,'view','list');
	window.location.href = reload_url;
    }
  });
  $('.view-filters .grid .grid_icon').click(function() {
    if (!$(this).hasClass('active')){
	$(this).addClass('active');
	$('.view-filters .list .list_icon').removeClass('active');
	var reload_url = get_search_page_url(current_url,'view','grid');
	window.location.href = reload_url;
    }
  });
  $('.filter-added').change(function() {
	var range_value = $(this).val();
	var reload_url = get_search_page_url(current_url,'range',range_value);
	reload_url = get_search_page_url(reload_url,'page', 0);
	window.location.href = reload_url;
  });
  $('.filter-rating').change(function() {
	var sort_value = $(this).val();
	var reload_url = get_search_page_url(current_url,'sort',sort_value);
	reload_url = get_search_page_url(reload_url,'page', 0);
	window.location.href = reload_url;
  });
  $('.filter-display').change(function() {
	var items_value = $(this).val();
	var reload_url = get_search_page_url(current_url,'items',items_value);
	reload_url = get_search_page_url(reload_url,'page', 0);
	window.location.href = reload_url;
  });
  //auto scroll to photo when click love it/hate it or thumb
   if($('div').hasClass('lookbook-tools') ){
    var page_pos = $('.lookbook-tools').position();
    $(window).scrollTop(page_pos.top);
   }

  // Tool tip for icons in search page
  $(".photo_rate_ico").hover(
    function () {
        $(this).append($("<div id='icon_hover' >Rate this Photo</div>"));
      }, 
      function () {
        $(this).find("#icon_hover:last").remove();
      }
  );
  $(".photo_add_lbook_ico").hover(
    function () {
        $(this).append($("<div id='icon_hover' >Add to Lookbook</div>"));
      }, 
      function () {
        $(this).find("#icon_hover:last").remove();
      }
  );
  $(".photo_send_to_ico").hover(
    function () {
        $(this).append($("<div id='icon_hover' >Send to Friend</div>"));
      }, 
      function () {
        $(this).find("#icon_hover:last").remove();
      }
  );
}

function get_search_page_url(page_url, str_search, str_replace){
  var reload_url =  page_url;
  var str_replace =  str_search + '=' + str_replace;
  str_index = page_url.indexOf(str_search);
  str_view_index = page_url.indexOf(str_search + '=');
    if (str_view_index == -1){
	if (page_url.indexOf('?') == -1)
	  reload_url = page_url + '?' + str_replace;
 	else
	  reload_url = page_url + '&' + str_replace;
    }else {	
        //var string_start = str_view_index + str_search.length + 1;
	var string_start = str_view_index;
        var last_part_str = page_url.substring(string_start , page_url.length , page_url.indexOf('&'));
        var is_middle = last_part_str.substring(0,last_part_str.indexOf('&'));
        if (is_middle){
 	  reload_url = page_url.replace(last_part_str.substring(0,last_part_str.indexOf('&')), str_replace);
        }else{
	  reload_url = page_url.replace(page_url.substring(string_start, page_url.length), str_replace);
        }
   }

  return reload_url;
};
function searchbox_reset(item_id){
  var loc = new String(document.location);
  var prefix = (loc.indexOf('mylifetime.com') != -1) ? '/all-the-looks' : ''; 
  var httphost = Drupal.settings.atl.httphost;
  var request_url = 'http://' + httphost + prefix + '/allthelooks_search/searchbox/reset_section/' + item_id;
  //var request_url = 'http://localhost/test/atl/reset.htm';
  //alert (request_url);return false;

    jQuery.get(
	request_url ,
	{},
	function(content){
	  searchbox_get_reset_content (content,item_id);
	},
	'json'
       );
};

function searchbox_reset_all() {
	$('#search_navigation').children().each(function() {
		var search_id = $(this).attr('id');
		searchbox_reset(search_id.substring(9));
	});
	$('.clear_all').hide();
}

function searchbox_get_reset_content (content, item_id){
	
  if ( item_id != 'all'){
    $('#nav_item-'  + item_id + ' .inner_block_body ul').remove();
	$('#nav_item-'  + item_id + ' .clear_block').remove();
	$('#nav_item-'  + item_id + ' .inner_block_body>h3').html(content['title']);
	$('#nav_item-'  + item_id + ' .inner_block_body').append(content['data']);
  }else{
	  
 	$('#block-allthelooks_search-allthelooks_search_searchbox #search_navigation').remove();
	$('#block-allthelooks_search-allthelooks_search_searchbox .menu_body_text .clear_all').remove();
	
	
	
	$('#block-allthelooks_search-allthelooks_search_searchbox .search_menu_body').append(content['data']);
  }
  Navigation.init(); // initialize after content changes. (else the expand/collapse in 2nd level won't work)
  Drupal.behaviors.autocomplete() //reinitialize autocomplete on the page
};

function allthelooks_search_get_lookbook(nid){
  //alert (nid);
  $('#add-photo-form.modal-form input[@name=photo_nid]').val(nid);
  $('#add-lookbook-form').hide();
  $('#add-photo-form .form-middle').css('height','160px');
  $('#add-photo-form').center();
  $('#add-photo-form').show();
  $('#add-photo-form .form-display').show();
  return false;
}


/*
 * Drupal's autocomple overwrite
 */
if(typeof Drupal.jsAC!= 'undefined') {
	var submitOverwrite = false;

	Drupal.jsAC.prototype.hidePopup = function (keycode) {
	  // Select item if the right key or mousebutton was pressed
	  if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
	    this.input.value = this.selected.autocompleteValue;
	    submitOverwrite = true;
	    $('#' + $('#' + this.input.id).parent('div').parent('div').parent('form').attr('id')).submit();
	  }
	  
	  // Hide popup
	  var popup = this.popup;
	  if (popup) {
	    this.popup = null;
	    $(popup).fadeOut('fast', function() { $(popup).remove(); });
	  }
	  
	  this.selected = false;
	  
	}
	
	Drupal.autocompleteSubmit = function () {
		if(submitOverwrite) {
			return true;
		}
		return $('#autocomplete').each(function () {
		    this.owner.hidePopup();
		  }).size() == 0;
	}
}
