function Ready_up(type){
	var self = this;
	this.root = $('#container');
	this.loaderChoose = function() {		
		self.jaxChoose();

		if (self.root.find('.page_tools').length > 0)
			new Page_tools(self.root.find('.page_tools'));
			
		if (self.root.find('#content').hasClass('home'))
			new Home_page(self.root.find('#content'));
			
    if (self.root.find('.map_chose').length > 0)
      new Map_choose(self.root.find('.map_chose'));
      
    if (self.root.find('.newsletter').length > 0) {
      new Newsletter_form(self.root.find('.newsletter'));
    }
      			
		new Navi(self.root.find('.navi.main'));
		
		new Window_fixer().start_up();;
	}
	this.jaxChoose = function() {	
		External_link.init("class","external_link", "");
		
		if(self.root.find('.expand').length > 0)
			new Expandable(self.root.find('.expand'), 0);
	}
	
  //new Underdevelop_img(this.root);
	
	if (type === 'load') 
		this.loaderChoose();
	else 
		this.jaxChoose();
}

function Window_fixer() {
  var self = this;
  this.root = $('div#container');
  this.fixed = false;
  this.wind = $(window);
  
  this.auto_position_switch = function(e) {
    if (self.wind.width() < 960 && !self.fixed) {
      self.fixed = true;
      self.root.css({'margin-left' : 'auto', 'left': 'auto'});
    } else if (self.wind.width() > 960 && self.fixed) {
      self.fixed = false;
      self.root.removeAttr('style');
    }
  }
  
  this.start_up = function() {
    self.wind.bind('resize', this.auto_position_switch);
    self.wind.trigger('resize');
  }
  
  return this;
}

function Underdevelop_img(element) {
  var self = this;
  this.root = element.find('img');
    
  for (var i = 0; i < this.root.length; i++) {
    if (this.root.eq(i).attr('src').match(/^\/system\//i)) {
      this.root.eq(i).attr('src', 'http://heat.copiousdev.com' + this.root.eq(i).attr('src'))
    }
  }
}

function Map_choose(element) {
  var self = this;
  this.root = element;
  this.root.children('.head').children('h6:last').toggle(function() {
    $(this).html('Hide Map');
    self.root.animate({
      width: '40%'
    }, {duration: 450, 
      easing: "easeOutQuad",
      queue: false
    });
  }, function() {
    $(this).html('Show Map');
    self.root.animate({
      width: '100%'
    }, {duration: 450, 
      easing: "easeOutQuad",
      queue: false
    });
  });
  this.root.children('.head').children('h6:last').click();
}

function Home_page(element) {
	var self = this;
	this.root = element;
	new Carousel(this.root.find('.carousel'), 7, true, 'home_carousel');
}

function setCookie(cookie_name, value, days_around) {
	var expire_date = new Date();
	expire_date.setDate(expire_date.getDate() + days_around);
	document.cookie = cookie_name + '=' + escape(value) + ';expires=' + expire_date.toGMTString();
}

function CookieHandler() {
	this.setCookie = function (name, value, days_around) {
		if (typeof(seconds) != 'undefined') {
			this.date = new Date();
			date.setTime(date.getDate() + days_around);
			this.expires = "; expires=" + date.toGMTString();
		}
		else {
			this.expires = "";
		}
		document.cookie = name + "=" + value + this.expires + "; path=/";
	}
	this.getCookie = function (name) {
		name += "=";
		var carray = document.cookie.split(';');
		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}
		return null;
	}
	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}
}

function Carousel(element, delay, auto, name_of_cookie) {
  var self = this;
  this.root = element;
  this.automate = auto;
  this.delay = delay;
  this.cookie_name = name_of_cookie;
  
  this.cookier = new CookieHandler();
  this.main_items = this.root.children();
  this.on_own = null;
  
  this.root.prepend('<div class="thumb_container"></div>');
  for (var i = 0; i < this.main_items.length; i++) {
    self.root.children('.thumb_container').append('<img src="/images/home/carousel_marker.png" alt="Click to Change Image" />');
    self.root.children('.thumb_container').children().eq(i).data('image', self.main_items.eq(i));
    self.root.children('.thumb_container').children().eq(i).data('iter', i);
  }
  this.thumbs = this.root.children('.thumb_container').children();
    
  this.main_items.not(':first').remove();
  this.large_switch = function() {
    self.root.children().eq(2).fadeIn(800, function() {
      self.root.children().eq(1).remove();
    });
  }
  this.button_over = function(e) {
    $(this).animate({
      opacity: 0.65
    }, {duration: 250, 
      easing: "easeOutQuad",
      queue: false
    });
  }
  this.button_out = function(e) {
    if ($(e.currentTarget).data('iter') == self.active.data('iter')) {
      this.opacity = .8;
    } else {
      this.opacity = .4
    }
    $(this).animate({
      opacity: this.opacity
    }, {duration: 250, 
      easing: "easeOutQuad",
      queue: false
    });
  }
  this.button_on = function(e) {
    if ($(e.currentTarget).data('iter') == self.active.data('iter')) {
      return false;
    }
    self.cookier.setCookie(self.cookie_name, $(this).data('iter'), 10);
    self.root.append($(this).data('image').hide());
    self.active.animate({
      opacity: 0.4
    }, {duration: 500, 
      easing: "easeOutQuad",
      queue: false
    });
    $(this).stop(true);
    $(this).animate({
      opacity: 0.8
    }, {duration: 500, 
      easing: "easeOutQuad",
      queue: false
    });
    self.large_switch();
    self.active = $(this);
    if (self.automate) {
      self.timer_set();
    }
  }
  this.next_up = function() {
    if (self.active.next().is('img')) {
      self.active.next().trigger('click');
    } else {
      self.thumbs.eq(0).trigger('click');
    }
  }
  this.timer_set = function() {
    if (self.on_own != null) {
      clearTimeout(self.on_own);
    }
    self.on_own = setTimeout(function() {
      self.next_up();
    }, self.delay * 1000);

  }
  this.thumbs.click(self.button_on);
  this.thumbs.hover(self.button_over, self.button_out);
  
  this.tempIter = this.cookier.getCookie(self.cookie_name);
  if (!this.tempIter) {
    this.tempIter = 0;
  }
  
  this.active = this.thumbs.eq(this.tempIter);
  this.thumbs.eq((Number(this.tempIter) + 1) % this.main_items.length).trigger('click');
}

function Page_tools(element) {
	var self = this;
	this.root = element;
	new Popn_form(self.root.find('#emailer'));
	new Text_resizer(self.root.find('#text_resize'));
}

function Text_resizer(element) {
	var self = this;
	this.root = element;
	this.current_size = 10;
	this.root.children('a[href="#increse"]').click(function() {
		$('.deskman').css({fontSize: ++self.current_size});
	});
	this.root.children('a[href="#decrese"]').click(function() {
		if (self.current_size > 10)
			$('.deskman').css({fontSize: --self.current_size});
	});
}

function Expandable(element, first_open) {
	var self = this;
	this.root = element;
	this.exander = this.root.find('h3');
	
	this.set_slide = function(target) {
		this.slides = target.next();
		this.temp = this.slides.next();
		target.append('<span>&nbsp;</span>');
		while (!this.temp.is('h3') && this.temp.html()) {
			this.slides = this.slides.add(this.temp);
			this.temp = this.temp.next();
		}
		this.slides.wrapAll('<div class="wrapper"></div>');
		target.toggle(function() {
			$(this).next().slideDown(300, "easeOutQuad");
			$(this).addClass('open');
		}, function() {
			$(this).next().slideUp(300, "easeOutQuad");
			$(this).removeClass('open');
		});
	}
	for (var i = 0; i < this.exander.length; i++)
		this.set_slide(jQuery(this.exander[i]));
	this.root.find('.wrapper').slideUp(0);
	if (typeof(first_open) === "number")
		this.exander.eq(first_open).click();
}

function ToolTip(element, tipper, tip_name, replace) {
	var self = this;
	self.root = jQuery('#jax_content');
	self.root.append(element);
	self.root.children('.toolTip:last').addClass(tip_name);
	var toolTip = self.root.find('.' + tip_name);
	if (replace)
		toolTip.children('.tip_cont').html(tip_name.replace(/_/g, ' '));
	toolTip.show();
	toolTip.css({
		top: (tipper.offset().top - toolTip.outerHeight() + 7),
		left: (tipper.offset().left - (toolTip.outerWidth() * .4) - 30),
		height: toolTip.children('.tip_cont').height() + 4,
		width: toolTip.children('.tip_cont').outerWidth() + 10
	});
	toolTip.children('.tip_cont').css('width', toolTip.width()-6);
	toolTip.hide();
	tipper.hover(
		function() {
			toolTip.show();
		},
		function() {
			toolTip.hide();
		}
	);

}

function Popn_form(element) {
  var self = this;
  this.root = element;  
  this.ajaxed = null;
  this.url = '/contact/refer';
  this.jaxer = $('#jax_content');
  this.form_title = 'Email this Page to a Friend';
  this.boxer = {
    xer : 550,
    yer : 550
  }
  this.filter_response = function(html) {
    var tempHtml = $(html).find('#errorExplanation');
    self.ajaxed.find('#errorExplanation').remove();
    if (tempHtml.hasClass('errorExplanation')) {
      self.ajaxed.prepend(tempHtml);
    }
    else {
      self.ajaxed.html("<h3> Success </h3>");
      self.ajaxed.fadeOut(500, function() {self.ajaxed.dialog('close')});
    }
  }
  
  this.send_data = function() {
    $.ajax({
      url: self.url,
      type: "POST",
      data: self.ajaxed.serialize(),
      async: false,
      success: self.filter_response
    });
  }
  
  this.mybuttons = {
    send: self.send_data
  }
  
  this.create_form = function(html) {
    self.jaxer.append(html);
    self.ajaxed = self.jaxer.children('form:last').addClass('deskman');
    self.ajaxed.dialog({
      modal: true,
      title: self.form_title,
      height: self.boxer.yer,
      width: self.boxer.xer,
      resizable: true,
      buttons: self.mybuttons,
      close: function() {
        self.root.bind('click', self.openForm);
        self.ajaxed.dialog('destory').remove();
      }
    });
  }
  this.openForm = function() {
    self.root.unbind('click', self.openForm);
    $.ajax({
      url: self.url,
      async: false,
      success: self.create_form
    });
  }	
  this.root.bind('click', self.openForm);
}

function Newsletter_form(element) {
  Popn_form.call(this, element);
  this.url = '/newsletter_signup';
  this.form_title = 'Sign Up for the Oregon HEAT Newsletter';
  this.boxer = {
    yer : 200,
    xer : 450
  }
  this.mybuttons = null;
}

$(document).ready(function() {
	new Ready_up('load');
});

/* -----------------------------------------------------------
/*	------------------------- cLone ----------------------------
*/