var startPos = document.location.href.indexOf("//") + 2;
var len = document.location.href.length;
if (document.location.href.indexOf("#") > 0) len = document.location.href.indexOf("#");
if (document.location.href.indexOf("?") > 0) len = document.location.href.indexOf("?");
len -= startPos;
var pageURL = document.location.href.substr(startPos,len);

function wccSearchMethod(e) {

	e.preventDefault();
	if ($(".search-text").val() != "") location.href = siteURL + "/web-cite-city/search/" + escape($(".search-text").val()) + "/";
	else alert($(".search-text").attr("error"));

}

function hideTops() {

	// fix flash issues
	$('embed, object, select').css({ 'visibility' : 'hidden' });
	
}

function showTops() {
	
	// fix flash issues
	$('embed, object, select').css({ 'visibility' : 'visible' });
	
}

function portfolioNav(e) {
	
	e.preventDefault();
	
	// get my group id
	var myGroupLabel = $(e.currentTarget).attr("href");
	var objects = $('[rel=group-select]');
	
	// check for "all"
	if (myGroupLabel == "") {

		for (var i=0; i<objects.length; i++) $(objects[i]).removeClass('selected');
		$(e.currentTarget).addClass('selected');
		$('.portfolio-sample').fadeIn(300);
		
	} else {
	
		myGroupLabel = myGroupLabel.substr(0,myGroupLabel.length-1);
	
		// get other group ids
		var otherIDs = Array();
		var thisGroupLabel = "";
		for (var i=0; i<objects.length; i++) {
			thisGroupLabel = $(objects[i]).attr("href");
			thisGroupLabel = thisGroupLabel.substr(0,thisGroupLabel.length-1);
			if (thisGroupLabel != myGroupLabel) {
				if (thisGroupLabel != "") $('[id^=' + thisGroupLabel + ']').fadeOut(300);
				$(objects[i]).removeClass('selected');
			}
		}	
	
		$(e.currentTarget).addClass('selected');
		$('[id^=' + myGroupLabel + ']').delay(300).fadeIn(300);

	}
	
}

function mainmenu(){
	$(" .menu_bar_navigation ul ").css({display: "none"}); // Opera Fix
	$(" .menu_bar_navigation li").hover(function(){
		$(this).find('a:first').addClass('hover');
		$(this).find('ul').css({visibility: "visible",display: "none"}).show();
	},function(){
		$(this).find('a:first').removeClass('hover');
		$(this).find('ul').css({visibility: "hidden"});
	});
}
 
$(document).ready(function() {
	
	var allChecked = false;
	
	mainmenu();
	
	$('[rel=group-select]').bind("click",portfolioNav);
	
	$('.fancy').fancybox({
		'titleShow'     : false
	});
	
	$('#check-all').bind("click",function(e) {
	
		e.preventDefault();
		if (!allChecked) $("input[type=checkbox]").attr("checked","checked");
		else $("input[type=checkbox]").attr("checked","");
		
		allChecked = !allChecked;
		
	});
	
	$('.digital_year h2').bind("click",function(e) {
		
		e.preventDefault();
		var container = $(this).parent();


		if ($(container).hasClass("digital_year_expanded")) {
			var content = $(container).children(".digital_year_content");
			$(content).slideUp(600,function() {
				$(container).removeClass("digital_year_expanded");
			});
		} else {
			var content = $(container).children(".digital_year_content");
			$(content).slideDown(600,function() {
				$(container).addClass("digital_year_expanded");
			});
		}
		
	});
	
	$('#download-list div div.exp').each(function(i,e) {
	
		if ($(e).hasClass("expanded")) {
			$(e).children("span").text("-");
		} else {
			$(e).children("span").text("+");
		}
		
		$(e).bind("click",function(ee) { 
			
			ee.preventDefault();
			if ($(this).hasClass("expanded")) {
				
				$(this).siblings("div.issues").slideUp();
				$(this).removeClass("expanded");
				$(e).children("span").text("+");
				
				
			} else {
				
				$(this).siblings("div.issues").slideDown();
				$(this).addClass("expanded");
				$(e).children("span").text("-");

			}
			
		});
		
	});
	
	$("ul.profile-list").children("li").children("a").bind("click",function(e){
		
		e.preventDefault();
		if ($(this).hasClass("expanded")) {
			
			$(this).siblings("ul").slideUp();
			$(this).removeClass("expanded");
			$(this).children("span").children("span.p").fadeIn(100);
			$(this).children("span").children("span.m").fadeOut(100);
			
		} else {
			
			$(this).siblings("ul").slideDown();
			$(this).addClass("expanded");
			$(this).children("span").children("span.p").fadeOut(100);
			$(this).children("span").children("span.m").fadeIn(100);
			
		}
		
	});
	
	$("a.rfp-add").bind("click",function(e){
		
		// don't do normal things
		e.preventDefault();
		
		// have we already added this one?
		if ($(this).attr("href") != "") {	
			
			// ajax add
			$.get($(this).attr("href"),function(data){
				
				// are we error-free?
				if (data == "0") {
										
					// update the add to added
					$("a.rfp-add img").attr("src","i/rfp-added.gif");
					$("a.rfp-add").attr("href","");
					
					// update the view button
					if ($("a.rfp-view:eq(0)").attr("href") == "") {

						$("a.rfp-view img").attr("src","i/rfp-view.gif");
						$("a.rfp-view").attr("href","planner/rfp/");
						
					}
					
				}	
						
			});
			
		}
		
	});
	
	$("a.rfp-view").bind("click",function(e) {
		
		if ($(this).attr("href") == "") return false;
		
	});
	
	$(".profile-rfp-cat-list input").bind("click",function(e){ 
	
		if ($(this).is(':checked')) {
			$(".cat" + $(this).val()).attr("checked","checked");
		} else {
			$(".cat" + $(this).val()).removeAttr("checked");
		}
		
	});
	
	$("form").bind("submit",function(e){
	
		if ($(this).find(".req").length > 0) {
			
			// check for required fields
			var errorText = "";
			$(this).find(".req").each(function(i,ee) {

				// remove error condition
				$(ee).removeClass("error");

				// check for blank
				if ($(ee).val() == "") {
					
					// add error to display
					errorText += $(ee).attr("error") + "\n";
					
					// set error condition
					$(ee).addClass("error");
					
				}
				
			});
			
			if (errorText != "") {
				alert(errorText);
				return false;
			} else {
				return true;
			}
			
		} else {
			
			// no required fields
			return true;
			
		}
		
	});
	
	$("#newsletter-signup").bind("submit",function(e){
	
		if ($("[name=email_address]").val() == "") {
			alert("Please enter a value for the \"Email Address\" field.");
			$("[name=email_address]").focus();
			return false;
		}

		if ($("[name=email_address]").val() != $("[name=email_address_confirm]").val()) {
			alert("Email addresses you entered do not match.");
			return false;
		}		
		
	});
	
	$("#fb").hide().fancybox({
		autoDimensions		: false,
		width				: 800,
		height				: "80%",
		titlePosition		: 'inside',
		onStart				: hideTops,
		onClosed			: showTops
	});
	
	$("#search-button").bind("click",function() {
		customSearchControl.execute($("#search-text").val());
	});

	$(".download-sponsored").fancybox({
		overlayColor: '#000000',
		overlayOpacity: 0.85,
		onStart: hideTops,
		onClosed: showTops
	});
	
	
	$(".download-link").bind("click",function(e) {
		_gaq.push(['_trackPageview', $(this).attr("href")]);
	});
	
	$(".wcc-search-form").bind("submit",wccSearchMethod);	
	$(".wcc-submit-button").bind("click",wccSearchMethod);
	
});
