/* -----------------------------------------------------------------------

  Inertia Interactive Media
  info@inertiaim.com

----------------------------------------------------------------------- */

var application;
$(function() {
	application = {

		scope: {
			container: $('#container'),
				header: $('#header'),
				nav: $('#nav'),
				content: $('#content'),
			footer: $('#footer')
		},

		pages : {
			howSolarWorks : function() {
				// replace placeholder image with Flash content
				  var flashvars = { };
				  var params = {
				    menu: "false",
				    wmode: "transparent",
				    bgcolor: "#ffffff;",
				    allowScriptAccess: "sameDomain",
				    allowFullScreen: "false"
				  };
				  var attributes = { };

				  swfobject.embedSWF("../flash/how-solar-works.swf", "flash-how-solar-works", "670", "450", "9.0.0", "../flash/expressInstall.swf", flashvars, params, attributes);
			},
			financialBenefits : function() {
				// prepare table
				$('.after_solar').prev().not('.after_solar').addClass('before_solar');
			},

			process : function() {
				$s.content.find('ol.process li').hover(function() {
					$(this).addClass('hover');
				}, function() {
					$(this).removeClass('hover');
				});
			},

			freeConsultation : function() {
				if ($("#consult_average_bill").val() != '')
				{
					temp = $("#consult_average_bill").val().substr(1);
				}
				else
				{
					temp = 300;
				}
				
				$("#average_bill_slider").slider({
					range: "min",
					value: temp,
					min: 0,
					max: 1000,
					step: 50,
					slide: function(event, ui) {
						$("#consult_average_bill").val('$' + ui.value);
					}
				});

				$("#consult_average_bill").val('$' + $("#average_bill_slider").slider("value"));
			},
			
			financeForm : function() {
				if ($("#finance_average_bill").val() != '')
				{
					temp = $("#finance_average_bill").val().substr(1);
				}
				else
				{
					temp = 300;
				}
				
				$("#average_bill_slider").slider({
					range: "min",
					value: temp,
					min: 0,
					max: 1000,
					step: 50,
					slide: function(event, ui) {
						$("#finance_average_bill").val('$' + ui.value);
					}
				});

				$("#finance_average_bill").val('$' + $("#average_bill_slider").slider("value"));

			},

			californiaPayback : function() {
				$("a.zoom").fancybox();
			},

			contact : function() {
				var createMap = function(el, ll) {
					el.googleMaps({
					    scroll: false,
				        markers: {
				            latitude: ll[0],
				            longitude: ll[1]
				        }
				    });					
				};
				$('.map').each(function() {
					var id = '#'+$(this).attr('id');
					var el = $(id);
					
					if($(this).attr('id') == 'map1')
					{
					    var ll = [37.928357, -122.12144];
					}
					else
					{
					    var ll = [37.928357, -122.12144];
					}
//					var address = $(this).text();
//					createMap(el, ll);

//					$(this).click(function() {
//						window.open("http://maps.google.com?q="+address, "map", "menubar=yes,width=1024,height=768,toolbar=yes");
//					});
				});
			},

			faq : function() {
				var touchHeight = function() {
					$('.el_bottom').height('auto').height(44);
				};
				$('ul.faq')
					.addClass('showhide')
					.children('li').each(function() {
						var $this = $(this);
						var title = $(this).find('.title');
						var content = $(this).find('.content').hide();

						title.toggle(function() {
							$this.addClass('open');
							content.slideDown('normal', touchHeight);
						}, function() {
							$this.removeClass('open');
							content.slideUp('normal', touchHeight);
						});
					});
			},

			init : function() {

				var s = {
					ucFirst: function(str) {
						return str.charAt(0).toUpperCase() + str.substring(1);
					},		
					camelCase: function(str) {
						var list = str.split('-');
						if (list.length == 1) return list[0];			
						var camel = (str.indexOf('-') == 0) ? s.ucFirst(list[0]) : list[0];
						for (var i = 1; i < list.length; i++) {
							camel += s.ucFirst(list[i]);
						}
						return camel;
					}
				}

				// run page's function
				var page = $('body').attr('class').match(/[^ ]*$/).toString();
				var fxn = 'application.pages.'+s.camelCase(page);
				var type = eval('typeof '+fxn);

				if (type == 'function') {
					eval(fxn+'()');
				}

			}
		},

		prepareSite : function() {
			// prevent ie background flickering
			try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {};
		},

		prepareFooter : function() {
			var wrapper = $('<div class="content_wrap"></div>');
			$s.footer.find('.content').wrap(wrapper);
		},

		prepareNavigation : function() {
			var items = $s.nav.children('li');
			// suckerfish
			items.each(function() {
				$(this).hover(function() {
					$(this).addClass('hover');
				}, function() {
					$(this).removeClass('hover');
				});
			});

			// link z-indices
			items.each(function(i) {
				$(this).css('z-index', items.length-i);
			});
		},

		applyCorners : function() {
			$('.corner_container').each(function() {
				$(this).markupCorners();
			});

			// content top and bottom
			if ($('body').is('.content')) {
				$s.content
					.wrapInner('<div class="content_wrap clear"></div>')
					.append('<div class="background el_top"></div><div class="background el_bottom">');
			}

			// navigation module
			$s.content.find('#sidebar .navigation ul').markupCorners(['tr','br']);

			// process list
			$s.content.find('ol.process li').markupCorners();

			// form
			$s.content.find('.page_form').markupCorners();
		},

		applyTooltips : function() {
			$('.tooltip').each(function() {
				$(this).qtip({
					position: {
						corner: {
							target: 'topMiddle',
							tooltip: 'bottomMiddle'
						}
					},
					style: { 
						width: 280,
						padding: 12,
						background: '#73b521',
						color: '#ffffff',
						border: {
							width: 2,
							radius: 3,
							color: '#A2D959'
						},
						tip: {
							corner: 'bottomMiddle',
							size: {
								x: 12,
								y: 7
							}
						}
					},
					show: 'mouseover',
					hide: 'mouseout'
				});
			});
		},

		init : function() {
			application.prepareSite();
			application.prepareFooter();
			application.prepareNavigation();
			application.applyCorners();
			application.applyTooltips();
			application.pages.init();
		}
	}

	var $s = application.scope;
	application.init();
});


jQuery.fn.extend({
	markupCorners : function(o) {
		if (typeof o == 'undefined') {o = ['tl', 'tr', 'bl', 'br']}
		var e = $(this);
		var corner = $('<div class="corner"><!-- --></div>');
		var corners = {
			tl: corner.clone().addClass('top left top_left'),
			tr: corner.clone().addClass('top right top_right'),
			bl: corner.clone().addClass('bottom left bottom_left'),
			br: corner.clone().addClass('bottom right bottom_right')
		}
		e.addClass('corner_container');
		$.each(o, function(i, c) {
			e.append(corners[c]);
		});
	},
	minHeight : function(height) {
		if ($.browser.msie && (typeof window.XMLHttpRequest == "undefined")) {$(this).height(height);}
		else {$(this).css('min-height', height)}
	},
	equalizeCols : function() {
		var max_height = 1;
		this.each(function() {
			if ($(this).outerHeight() > max_height) {max_height = $(this).outerHeight();}
		});
		this.each(function() {
			$(this).minHeight(max_height);
		});
	}
});

