function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&amp;");
  for (var i=0;i!=vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) return pair[1];
  }
}

function toggleTag(elm){
	if(elm) {
	    if($(elm).hasClass("active")) {
		    $(elm).removeClass("active");
		    for(var i  = 0; i < arrTags.length; i++)
		        if(arrTags[i] == elm.id){
		            arrTags.splice(i, 1);
		            arrTagsText.splice(i, 1);
		        }
	    }
	    else {
		    $(elm).addClass("active");
		    arrTags.push(elm.id);
		    arrTagsText.push(elm.innerHTML);
	    }
    }
	loadTipsList(1,1);
}

function loadTipsListForLang(pageNumber, orderBy, docId){
    var url = "/composite-"+docId+".htm?rnd="+Math.random()+"&tags="+arrTags.valueOf()+"&nbTags="+arrTags.length+"&categoryId="+categoryId+"&pageNumber="+pageNumber+"&orderBy="+orderBy;
    $("#tipsList").load(url, "", function(){
                            var strSelectedTags = "";
                            for(var i  = 0; i < arrTagsText.length; i++)
                                strSelectedTags += (strSelectedTags == "" ? "" : " + ") + arrTagsText[i];
                            $("#selectedTagsTop").html(strSelectedTags);
			    $("#selectedTagsBottom").html(strSelectedTags);
                            }
                        );
}

function clearSearch()
{
    $("#cloudContainer div p a").removeClass("active");
    arrTags.splice(0, arrTags.length);
    arrTagsText.splice(0,arrTagsText.length);
    loadTipsList(1,1);
}

/*
  automatically add gray note for all input fields
  with @note attribute
  
  if field is left blank and blured - restore gray
  note
*/
function updateForms() {
  jQuery(function($) {
    $("form").submit(function() {
      $("input:password[@encrypt]", this).each( function() {
        if(this.value != '')
          this.value = SHA256(this.value);
      });
    });
    
    $("input:text[@note],textarea[@note]").each(function(){
      if(!this.value && $(this).attr("note")) {
        this.value       = $(this).attr("note");
        this.style.color = "gray";
      }
    })
    .blur(function(){
      if(!this.value && $(this).attr("note")) {
        this.value       = $(this).attr("note");
        this.style.color = "gray";
      }
    })
    .focus(function(){
      if(this.value == $(this).attr("note") && this.style.color == "gray") {
        this.value       = "";
        this.style.color = "black";
      }
    });
    
    $("input:password[@note]").each(function(){
      if(!this.value && $(this).attr("note")) {
        //$(this).attr("type", "text");
        this.value       = $(this).attr("note");
        this.style.color = "gray";
      }
    })
    .blur(function(){
      if(!this.value && $(this).attr("note")) {
        //$(this).attr("type", "text");
        this.value       = $(this).attr("note");
        this.style.color = "gray";
      }
    })
    .focus(function(){
      if(this.value == $(this).attr("note") && this.style.color == "gray") {
        this.value       = "";
        this.style.color = "black";
        //$(this).attr("type", "password");
      }
    });
  });
}

function createRating(name, initial) {
  var s1 = '<input type="radio" name="' + name + '" value="';
  var s2 = '" />';
  var saturn = '';
  for(var i = 1; i<=5; i++)
    saturn += s1 + i + 
      (i==initial?'" checked="checked':'') + 
      (i==1?'" class="first star':(i==5?'" class="last star':'" class="star')) + s2;
  return saturn;
}

function addRating(form, name, initial, settings, callback) {
  if(typeof(settings) != 'undefined' && typeof(settings.enabled) != 'undefined' && !settings.enabled) {
    $(form).replaceWith('<img style="'+$(form).attr("style")+'" src="/images/stars_'+initial+'.gif" />');
  } else {
    $(form).eq(0).find("input").eq(0).before(createRating(name, initial));
    $(form).rating(settings, callback);
  }
}

$(document).ready(function() {
  updateForms();

  if(typeof(sIFR) == "function"){
    sIFR.replaceElement("h1.normal", "/meta_normal.swf", named({sColor : "#191612", sBgColor :"#f8f9f9", sWmode: "transparent"}));
    sIFR.replaceElement("h1.lime", "/meta_normal.swf", named({sColor : "#d3ef19", sBgColor :"#d4f30a", sWmode: "transparent"}));
    sIFR.replaceElement("h3.normal", "/meta_bold.swf", named({sColor : "#191612", sBgColor :"#d4f30a", sWmode: "transparent"}));
    sIFR.replaceElement("h2", "/meta_bold.swf", named({sColor : "#2e2b2b", sBgColor :"#d4f30a", sWmode: "transparent"}));
    sIFR.replaceElement("h3.center", "/meta_bold.swf", named({sColor : "#191612", sBgColor :"#d4f30a", sWmode: "transparent", sFlashVars:"textalign=center"}));
    sIFR.replaceElement("h1.normalSmall", "/meta_normal.swf", named({sColor : "#191612", sBgColor :"#f8f9f9", sWmode: "transparent"}));
    sIFR.replaceElement("h1.banner", "/meta_bold.swf", named({sColor : "#FFFFFF", sBgColor :"#000000", sWmode: "transparent", sFlashVars:"textalign=center"}));
    sIFR.replaceElement("h1.whiteLeft", "/meta_normal.swf", named({sColor : "#FFFFFF", sBgColor :"#000000", sWmode: "transparent", sFlashVars:"textalign=left"}));
  }

  if(typeof(Nifty) == "function") {
    Nifty("div.boxGreen,div.boxWhite,div.boxWhiteSmall,div.boxPink,div.boxImg","small transparent");
    Nifty("div.boxStd","small transparent fixed-height");
    Nifty("div#cloudContainer div","big small fixed-height");
    Nifty("div#footerContainer","small transparent fixed-height");
  }
   $("html").removeClass("sIFR-hasFlash");
});