// JavaScript Document

$(document).ready(function() {
  $('#search_bt').css('cursor', 'default');
  if($('.resbytypeitemselected').length!=0 || $('.resbythemeitemselected').length!=0){

    makeSearchActive();
  }
  $('.resbythemeitem, .resbytypeitem').click(function(){
    //alert($('.resbytypeitemselected').length+" "+$('.resbythemeitemselected').length)
    if($('.resbytypeitemselected').length==0 && $('.resbythemeitemselected').length==0){

      $('#search_bt').removeAttr('onclick');
      $('#search_bt').css('cursor', 'default');
      $base_path=$('#base_path').text();
      $('#search_bt img').attr("src",$base_path+"/images/but-go.gif");


    }

    else{
      makeSearchActive();
    }


  });
   function makeSearchActive(){

      $('#search_bt').css('cursor', 'pointer');
       $base_path=$('#base_path').text();
      $('#search_bt img').attr("src",$base_path+"/images/but-go-on.gif");



      $('#search_bt').attr('onclick', 'getSelected()');
    }
//  $('#search_bt img').mouseover('go.src="<?php print base_path() . path_to_theme() ?>/images/but-go-on.gif"');
// $('#search_bt').attr('onclick', getSelected());
});

var base_path = "/";
$('#searchbox').corner("10px");
$('.resbythemeitem').corner("12px");
$('.resbytypeitem').corner("12px");
$('.exrecentlyitem').corner("12px");

$(".exrecentlyitem").each(
  function(i,v)
  {
    initRecentBlockFade($(this));
  }
  );

$(".collectitem").each(
  function(i,v)
  {
    initSpecialBlockFade($(this));
  }
  );

$(".resourceitem").each(
  function(i,v)
  {
    initResourceBlockFade($(this));
  }
  );

function initRecentBlockFade(element)
{
  $(element).hover(function(){
    $(this).animate( {
      backgroundColor:"#ffffff"
    }, 500);
  },function(){
    $(this).animate( {
      backgroundColor:"#dcdbd7"
    }, 500);
  });
}

function initSpecialBlockFade(element)
{
  $(element).hover(function(){
    $(this).fadeTo("fast", 0.5);
  },function(){
    $(this).fadeTo("fast", 1);
  });
}				

function initResourceBlockFade(element)
{
  $(element).hover(function(){
    $(this).animate( {
      backgroundColor:"#f1f2f2"
    }, 300);
  },function(){
    $(this).animate( {
      backgroundColor:"#dcdbd7"
    }, 300);
  });
}
function changeClassByTheme(element){
  //   alert($(element).attr("class"));
  //if ($(element).attr("class") == "resbythemeitem floatl") {
  /*$(element).css("background-color","#FFFFFF");*/
  if (!$(element).hasClass("resbythemeitemselected")) {
    $(element).addClass("resbythemeitemselected");
	$(element).css("background-color","#5D87A1");
	$(element+" a").css("color","#FFFFFF");
  }
  else{
    $(element).removeClass("resbythemeitemselected");
	$(element).css("background-color","#FFFFFF");
	$(element+" a").css("color","#619080");
  }
}
function changeClassByType(element){
  //   alert($(element).attr("class"));
  if (!$(element).hasClass("resbytypeitemselected")) {
  	$(element).css("background-color","#619080");
    $(element).addClass("resbytypeitemselected");
	$(element+" a").css("color","#FFFFFF");
  }
  else {
    $(element).removeClass("resbytypeitemselected");
	$(element).css("background-color","#FFFFFF");
	$(element+" a").css("color","#5D87A1");
	
  }
  return false;
}








function set_ids(num) {
  var arr = new Array(1, 2, 4, 8, 16, 32, 64);

  var ret = 0;
  for( var i=0; i<7; i++)
    ret += num[i] * arr[i];
  return ret;
}

function getSelected(){
  var themes =new Array();
  var types =new Array();
  var spanish = 0;
  var i=0;
  var k=0;

  var arr = new Array(0, 0, 0, 0, 0, 0, 0);
  $( ".resbythemeitemselected" ).each( function() {
    themes[i]= $( this ).attr( "id" );
    if(themes[i] == "theme_12") arr[0] = 1;
    else if(themes[i] == "theme_13") arr[1] = 1;
    else if(themes[i] == "theme_14") arr[2] = 1;
    else if(themes[i] == "theme_15") arr[3] = 1;
    else if(themes[i] == "theme_16") arr[4] = 1;
    i++;
  } );
  spanish=0;
  $( ".resbytypeitemselected" ).each( function() {
    if ($( this ).attr( "id" ) == "spanish")
      spanish=1;
    else {
      types[k] = $( this ).attr( "id" );
      k++;
    }
  } );
  //alert(set_ids (arr));
  //set_ids
  //alert(types[0]);
  //alert(types[1]);
  //alert(themes+"\n"+types+"\n"+spanish);
  document.location.href=base_path+"node/11?themes="+set_ids(arr)+"&types="+types+"&spanish="+spanish;
    
//    if (window.XMLHttpRequest)
//    {// code for IE7+, Firefox, Chrome, Opera, Safari
//        xmlhttp=new XMLHttpRequest();
//    }
//    else
//    {// code for IE6, IE5
//        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//    }
//    xmlhttp.onreadystatechange=function()
//    {
//        if (xmlhttp.readyState==4 && xmlhttp.status==200)
//        {
//           alert("success");
//        }
//
//    }
//    xmlhttp.open("GET","http://localhost/heckmanface/node/11?themes="+themes+"&types="+types,true);
//    xmlhttp.send(); 

}



function browse_hover(elem, img) { 
  if($("#browse_on").val() == 0)
    elem.src = img;
}




function change_hover(element, on_off){
    if (on_off == 1) {	
        $(element).css("background-color","#5D87A1");
		$(element+" a").css("color","#FFFFFF");
	}
    else if(!$(element).hasClass("resbythemeitemselected")) {
        $(element).css("background-color","#FFFFFF");
		$(element+" a").css("color","#619080");
	}
}

function change_type_hover(element, on_off){
    if (on_off == 1) {	
        $(element).css("background-color","#619080");
		$(element+" a").css("color","#FFFFFF");
	}
    else if(!$(element).hasClass("resbytypeitemselected")) {
        $(element).css("background-color","#FFFFFF");
		$(element+" a").css("color","#5D87A1");
	}
}
