/*
 * jQuery custom scripts for BlueStar
 * http://www.bluestarenergy.com/
 *
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2011-06-27 (Mon, 27 June 2011)
 * Revision: 15
 */





/* Custom functions
------------------------------------------------- */
Array.prototype.max = function() {return Math.max.apply(Math, this);};
Array.prototype.min = function(){return Math.min.apply(Math, this);};

function newImage(arg) {
	if(document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if(document.images && (preloadFlag == true)) {
		for(var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function maxHeight() {
	var heights = new Array();
	$(".twocolumn .main, .twocolumn .sidebar").each(function(i) {
		heights[i] = $(this).height();
	});
	return heights.max();
}

function setHeights() {
	var h = maxHeight();
	$(".twocolumn .main, .twocolumn .sidebar").each(function(i) {
		$(this).css("min-height",h+"px");
	});
}

function adjustHeight() {
	var h = maxHeight();
	$(".twocolumn .main, .twocolumn .sidebar").each(function(i) {
		$(this).animate({ 
			height: h+"px"
		}, 250 );
	});
}

jQuery.fn.selectRange = function(start, end) {
	return this.each(function() {
		if(this.setSelectionRange) {
			this.focus();
			this.setSelectionRange(start, end);
		}
		else if(this.createTextRange) {
			var range = this.createTextRange();
			range.collapse(true);
			range.moveEnd("character", end);
			range.moveStart("character", start);
			range.select();
		}
	});
};

jQuery.fn.autoTab = function(event, len, next_field) {
	var phone_field_length = 0;
	if(event == "down") {
		phone_field_length = this.val().length;
	}
	else if(event == "up") {
		if(this.val().length != phone_field_length) {
			phone_field_length = this.val().length;
			if(phone_field_length == len) {
				next_field.focus();
			}
		}
	}
};

jQuery.fn.addAnother = function(to, max) {
	if(this.hasClass("add_email")) {
		var node = '<tr><td><input class="text" type="text" size="20" name="email[name][]"></td><td><input class="text" type="text" size="28" name="email[address][]"></td></tr>';
		var rows = to.find("tr").length;
		if(rows<=max) to.append(node);
		if(rows==max) this.hide();
	}
};

jQuery.fn.bseCycler = function() {
	if(this.hasClass("features")) {
		this.before('<div class="cycle_nav" id="news_nav">').cycle({
			fx:                "fade",
			timeout:           9000,
			pager:             "#news_nav",
			pause:             true,
			pauseOnPagerHover: true,
			cleartype:         true
		});
		
		$("#news_nav a").each(function() {
			$(this).click(function() {$(this).blur();});
			$(this).html("<span>"+$(this).html()+"</span>");
		});
	}
	else if(this.hasClass("magazines")) {
		this.before('<div class="cycle_nav" id="mag_nav">').cycle({
			fx:                "fade",
			timeout:           11000,
			pager:             "#mag_nav",
			pause:             true,
			pauseOnPagerHover: true,
			cleartype:         true,
			after:             function(currSlideElement, nextSlideElement, options, forwardFlag) {
				if($.browser.msie) {
					$(".pngfix").each(function() {
						if($(this).parent().css("display")=="block") {
							$(this).pngFix();
						}
					});
				}
			}
		});
		
		$("#mag_nav a").each(function() {
			$(this).click(function() {$(this).blur();});
			$(this).html("<span>"+$(this).html()+"</span>");
		});
	}
	else if($(this).attr("id") == "choice") {
		this.before('<div class="nav_container"><a id="prev_choice"></a><div class="cycle_nav" id="choice_nav"></div><a id="next_choice"></a></div>').cycle({
			fx:                "fade",
			timeout:           5000,
			pager:             "#choice_nav",
			pause:             true,
			pauseOnPagerHover: true,
			next:              "#next_choice",
			prev:              "#prev_choice",
			cleartype:         true
		});
	}
	else if($(this).attr("id") == "acct_cycler") {
		var nav = [
			'<div class="nav_container">',
//			'<a id="prev_choice"></a>',
			'<div class="cycle_nav" id="acct_cycler_nav"></div>',
//			'<a id="next_choice"></a>',
			'</div>'
		].join("");
		this.before(nav).cycle({
			fx:                "scrollLeft",
			timeout:           5000,
			pager:             "#acct_cycler_nav",
			pause:             true,
			pauseOnPagerHover: true,
//			next:              "#next_choice",
//			prev:              "#prev_choice",
			cleartype:         true
		});
	}
	else {
		this.cycle();
	}
};
/* -------------------------------------------------
                             end custom functions */





/* UI Candy
------------------------------------------------- */
$(document).ready(function(){
	
	// cross-browser placeholder support
	var PLACEHOLDER_SUPPORTED = "placeholder" in document.createElement("input");
	
	if(PLACEHOLDER_SUPPORTED || !$(":input[placeholder]").length) {}
	else {
		$(":input[placeholder]").each(function() {
			var el = $(this);
			var text = el.attr("placeholder");
			function add_placeholder() {if(!el.val() || el.val() === text) {el.val(text).addClass("placeholder_text");}}
			add_placeholder();
			el.focus(function() {
				if(el.val() === text) {el.val("").removeClass("placeholder_text");}
			}).blur(function() {
				add_placeholder();
			});
			el.closest("form").submit(function() {
				if(el.val() === text) {el.val("");}
			}).bind("reset", function() {
				setTimeout(add_placeholder, 50);
			});
		});
	}
	
	if($.fancybox) {
		$("a.fancybox").fancybox({centerOnScroll: true});
		$("a.link-closeWindow").click(function(){$.fancybox.close(); return false;});
		
		$("a.contentOverlay").fancybox({
			autoDimensions		: false,
			autoScale			: false,
			height				: 470,
			overlayOpacity 		: .8,
			padding				: 0,
			scrolling			: "no",
			showCloseButton		: false,
			titleShow 			: false,
			width				: 680
		});	
	}
	
	/* rounded corners */
	if($.browser.msie) {
		$("div.entry, div.types div.current").prepend('<div class="corner" id="bl"></div><div class="corner" id="br"></div><div class="corner" id="tl"></div><div class="corner" id="tr"></div>');
		$("div.types div.renewable, div.types div.traditional").prepend('<div class="corner" id="tl"></div><div class="corner" id="tr"></div>');
		$("div.choices div.renewable").prepend('<div class="corner" id="bl"></div><div class="corner" id="br"></div><div class="corner" id="tr"></div>');
		$("div.choices div.traditional").prepend('<div class="corner" id="bl"></div><div class="corner" id="br"></div><div class="corner" id="tl"></div>');
		
		$("#content div.landingpage em").addClass('ie-strong');
		
		$(".pngfix").each(function() {
			if($(this).parent().css("display")=="block") {
				$(this).pngFix();
			}
		});
		
		$("#footer .pngfix").pngFix();
	}
	
	/* tr:hover fix */
	$("table.data tr").hover(
		function() {$(this).addClass("hover");},
		function() {$(this).removeClass("hover");}
	);
	
	/* add class for IE6 */
	$('input[type="text"],input[type="password"]').addClass("text");
	$('input[type="file"]').addClass("file");
	$(":first-child").addClass("first-child");
	$(":last-child").addClass("last-child");
	
	/* add another links */
	$("a.add_email").click(function() {
		$(this).addAnother($("#emails tbody:last-child"), 10);
		$(this).blur();
		setHeights();
		return false;
	});
	
	/* payment method selector */
	$("select#payment_method").change(function() {
		$("div.payment_detail").slideUp();
		
		switch($(this).val()) {
			case "1": $("#ach").slideDown(500); setTimeout("adjustHeight();", 500); break;
			case "2": $("#credit_card").slideDown(500); setTimeout("setHeights();", 500); break;
			default:
		}
	});
	
	/* buttons */
	$("#continue").hover(
		function() {
			$(this).attr("src", "/images/buttons/continue_95x30_hover.png");
		},
		function() {
			$(this).attr("src", "/images/buttons/continue_95x30.png");
		}
	);
	
	$("#review_agreements").hover(
		function() {
			$(this).attr("src", "/images/buttons/review_165x30_hover.png");
		},
		function() {
			$(this).attr("src", "/images/buttons/review_165x30.png");
		}
	);
	
	$('.readMore').each(function(){
		$(this).data('original-height', $(this).height());
		$(this).prepend('<a href="#" class="trigger"><span class="open">+</span> Read More</a>');			
		$(this).height($(this).children('a.trigger').height()).css('overflow','hidden');
		$(this).children('a.trigger').toggle(function() {
			$(this).parent().animate({
				height	:	$(this).parent().data('original-height')
			}, 1 * 1000);
			$(this).html('<span class="close">-</span> Hide');
			return false;
		}, function() {
			$(this).parent().animate({
				height	:	$(this).height()
			}, 1 * 1000);
			$(this).html('<span class="open">+</span> Read More</a>');
			return false;
		});
	});
	
});

$(window).load(function(){
	setHeights();
});
/* -------------------------------------------------
                                     end UI Candy */





/* Phone Number Validator
------------------------------------------------- */
if(jQuery.validator) {
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please provide a valid phone number &#8212; e.g. (312) 555-5555");
	
	jQuery.validator.addMethod("username", function(value, element) {
        return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
    }, "Your username can only contain letters, numbers, or dashes.");
}
/* -------------------------------------------------
                       end Phone Number Validator */






