/**
 * author: chris@doityourself.com
 * requires jquery.js <www.jquery.com>
 */
var ord = Math.random()*10000000000000000;
var dim_widget_project = 0;

//fix IE 6 image flicker
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

$(document).ready(function() {
  loadLoginButtons();
  loadSearchForm();
  loadNewsletterForm();
  loadContractorForm();
});

function loadLoginButtons() {
  $(".login-btn").click(function() {
    var offset = $(this).offset();
    var box = $("#login-box");
    box.css('left', offset.left);
    box.css('top', offset.top);
    box.show('fast');
    return false;
  });
}

function hideLoginForm() {
  $("#login-box").hide('fast');
}

function loadNavList() {
  $("#nav > ul li").each(function() {
    var $this = $(this);
    var ul = $this.children('ul');
    var html = ul.html();
    var nav_tab = $this.find('.nav-tab');
    
    // add divs for BG images
    ul.replaceWith('<div class="navbox"><div class="topblock"></div><div class="navbody"><div class="t"></div><ul>'+html+'</ul></div><div class="b"><div></div></div></div>');
    var dropdown = $this.children(".navbox");
    
    // position drop down under the tab and show css hidden ul
    if($this.hasClass('current')) {
      dropdown.css('left', '-15px');
    } else {
      dropdown.css('left', (this.offsetLeft-15)+'px');
    }
    dropdown.find('ul').show();
    
    // set hover functions
    $this.hover(function() {
      $("#nav li.over").removeClass('over');
      if(!$this.hasClass('current')) {
        $this.addClass('over');
        $this.find('.topblock').width(nav_tab.width()+23);
      }
      
      dropdown.show();
      dropdown.find(".b").width(dropdown.width()); //fix the width
    },
    function() {
      $this.removeClass('over');
      $this.children(".navbox").hide();
    });
  });
  
  //preload some background images
  $.preloadImages(
    'http://images.doityourself.com/assets/nav-dropbox.png', 
    'http://images.doityourself.com/assets/greybox.png'
  );
}

function logout() {
  if(confirm("Are you sure you want to log out?")) return true;
  else return false;
}

function loadSearchForm() {
  var form = $("#search-bar form");
  form.submit(function() {
    if(this.qt.value == '') {
      alert("Please enter a search term");
      this.qt.focus();
      return false;
    }
    return true;
  });
}

function loadNewsletterForm() {
  defaultEmail = 'email address';
  form = $(".newsletter form");
  form.submit(function(){
    email = this.ea.value
    if(email!=defaultEmail && email!='') {
      return true;
    } else {
      alert("Please enter your email address");
      this.ea.focus();
      return false;
    }
  });

  input = form.find(".input-txt");
  input.val(defaultEmail);
  input.focus(function(){
    if(this.value==defaultEmail)
      this.value = '';
  });
  input.blur(function(){
    if(this.value=='')
      this.value = defaultEmail;
  });
}

function loadContractorForm() {
  defaultZip = 'ZIP Code';
  form = $(".find-contractor form");
  form.submit(function(){
    zipcode = this.reqZip.value;
    if(zipcode!=defaultZip && zipcode!='') {
      return true;
    } else {
      alert("Please enter a valid zip code");
      this.reqZip.focus();
      return false;
    }

    return true;
  });

  input = form.find(".input-txt");
  input.val(defaultZip);
  input.focus(function(){
    if(this.value==defaultZip)
      this.value = '';
  });
  input.blur(function(){
    if(this.value=='')
      this.value = defaultZip;
  });
}

function popUp(mypage,myname,w,h,scroll,resizable,pos) {
  if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
  if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
  else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
  settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable='+resizable+'';
  win1=window.open(mypage,myname,settings);
  win1.focus(myname);
}

//jQuery plugins
jQuery.fn.extend({
 id: function() {
   return this[0].id;
 }
});

jQuery.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};

jQuery.log = function(message) {
  if(window.console) {
     console.debug(message);
  } else {
     alert(message);
  }
};

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function DimWidgetHideAll(project_count) {
	for (var i = 0; i < project_count; i++) {
		document.getElementById('dim-widget-project-' + String(i)).style.display = 'none';
	}
}

function DimWidgetGoRight(project_count) {
	if (project_count <= 1) { return false; }
	DimWidgetHideAll(project_count);
	if (dim_widget_project < project_count - 1)
		dim_widget_project++;
	else
		dim_widget_project = 0;

	document.getElementById('dim-widget-project-' + String(dim_widget_project)).style.display = 'block';
}

function DimWidgetGoLeft(project_count) {
	if (project_count <= 1) { return false; }
	DimWidgetHideAll(project_count);
	if (dim_widget_project > 0)
		dim_widget_project--;
	else
		dim_widget_project = project_count - 1;

	document.getElementById('dim-widget-project-' + String(dim_widget_project)).style.display = 'block';
}