/**
 * @author Dave
 */

/*
* XHTML 1.0 Strict work around for external links
* For all external links, define rel="external" instead of
* the deprecated target="_blank" attribute
*/
function externalLinks() {
	$$('a[rel="external"]').each(function(a){
		a.writeAttribute("target","_blank");
	});
}

/*
* Easy roll-over enabling for linked images and <input type="image" />
* Add the class "rollOver" to the element to enable the roll-over. Please
* make sure that the file type remains consistent between states (ex. all
* GIF or all JPG). The file name must also remain consistent with the
* exception of the trailing state indicator as follows:
* 
* Idle State:					imagename_i.gif
* Hover State:					imagename_o.gif
* Active (Clicked) State:		imagename_a.gif
* 
* Example:
* <img src="btn_sign-up_i.gif" alt="Sign up now!" class="btn_signUp rollOver" />
* <input type="image" src="images/btn_sign-up_i.gif" class="rollOver" />
*/
function rollOvers() {
	$$('img','input[type="image"]').each(function(i){
		if(i.hasClassName("rollOver")) {
			i.observe("mouseover",function(){
				imgSrc = $(this).readAttribute("src");
				if(imgSrc.indexOf("_i.") != -1) {
					$(this).writeAttribute("src",imgSrc.replace("_i.","_o."));
				}
			}).observe("mouseout",function(){
				imgSrc = $(this).readAttribute("src");
				if(imgSrc.indexOf("_o.") != -1) {
					$(this).writeAttribute("src",imgSrc.replace("_o.","_i."));
				}
				if(imgSrc.indexOf("_a.") != -1) {
					$(this).writeAttribute("src",imgSrc.replace("_a.","_i."));
				}
			}).observe("mousedown",function(){
				imgSrc = $(this).readAttribute("src");
				if(imgSrc.indexOf("_o.") != -1) {
					$(this).writeAttribute("src",imgSrc.replace("_o.","_a."));
				}
			}).observe("mouseup",function(){
				imgSrc = $(this).readAttribute("src");
				if(imgSrc.indexOf("_a.") != -1) {
					$(this).writeAttribute("src",imgSrc.replace("_a.","_i."));
				}
			});
		}
	});
}

function textReplace() {
	$$('input.textReplace').each(function(it){
		it.setStyle({color:"#999"});
		it.observe('focus',function(event){
			elem = Event.element(event);
			if (elem.defaultValue == elem.value) {
				elem.value = "";
				elem.removeAttribute("style");
			}
		}).observe('blur',function(event){
			elem = Event.element(event);
			if (elem.value == "" || elem.value == elem.defaultValue) {
				elem.setStyle({color:"#999"});
				elem.value = elem.defaultValue;
			}
		});
	});
}

function checkBoxFilter(s) {
	s.descendants('.dropList input[type=checkbox]').each(function(c,cIndex){
		c.observe('click',function(){
			if(this.hasClassName('filterAll')) {
				if(this.checked == false) {
					s.descendants('.dropList input[type=checkbox]').each(function(ce,ceIndex){
						ce.checked = false;
					});
				} else {
					s.descendants('.dropList input[type=checkbox]').each(function(ce,ceIndex){
						ce.checked = true;
					});
				}
			} else {
				if(this.checked == false) {
					s.down('input.filterAll').checked = false;
				} else {
					allChecked = true;
					s.descendants('.dropList input[type=checkbox]').each(function(counter,countIndex){
						if(counter.checked == false && !counter.hasClassName('filterAll')) {
							allChecked = false;
						}
					});
					if(allChecked == true) {
						s.down('input.filterAll').checked = true;
					}
				}
			}
			updateFilters(s);
		});
	});
	s.down('a.btn_dropDown').observe('click',function(){
		if(!s.hasClassName('open')) {
			s.addClassName('open');
		} else {
			s.removeClassName('open');
		}
	});
}

function updateFilters(s) {
	if(s.down('input.filterAll').checked == true) {
		s.down('.currentSelection').update(s.down('.filterAllLabel').innerHTML);
	} else {
		checkedCount = 0;
		s.select('.dropList input').each(function(cb){
			if(cb.hasClassName('filterAll') == false && cb.checked == true) {
				checkedCount++;
			}
		})
		if(checkedCount > 1) {
			s.down('.currentSelection').update(checkedCount+' Categories Selected');
		} else if(checkedCount == 1) {
			s.select('.dropList input').each(function(cbz){
				if(cbz.hasClassName('filterAll') == false && cbz.checked == true) {
					s.down('.currentSelection').update(cbz.next('label').innerHTML);
				}
			})
			
		} else if(checkedCount < 1) {
			//s.down('.currentSelection').update('0 Categories Selected');
			s.down('.currentSelection').update(s.down('.filterAllLabel').innerHTML);
		}
	}
}
function initFilterDropDowns() {
	$$("#nav_filter-ads .dropDown").each(function(s,index){
		checkBoxFilter(s);
		updateFilters(s);
	});
	$$("#nav_filter-ads .dropDown li").each(function(liE){
		liE.observe('mouseover',function(event){
			Event.element(event).setStyle({
				background: '#c1a260'
			});
		});
		liE.observe('mouseout',function(event){
			Event.element(event).setStyle({
				background: 'none'
			});
		});
		liE.down('label').observe('mouseover',function(event){
			Event.element(event).up().setStyle({
				background: '#c1a260'
			});
		});
		liE.down('label').observe('mouseout',function(event){
			Event.element(event).up().setStyle({
				background: 'none'
			});
		});
		liE.down('input').observe('mouseover',function(event){
			Event.element(event).up().setStyle({
				background: '#c1a260'
			});
		});
		liE.down('input').observe('mouseout',function(event){
			Event.element(event).up().setStyle({
				background: 'none'
			});
		});
	})
}

function initviewDetailsLinks() {
	$$('.btn_view-details').each(function(ct){
		ct.writeAttribute('href','javascript:void(null);');
	});
}

//User Permissions Script - John 2.18.08
//User Permission Settings for Create a New Campaign
function userPermissions(){
	$$('#client_permissions tr td input').each(function(s){
		s.observe('click', function(event){
			permissions = Event.element(event);
			//get Permissions Type
			var permType = permissions.readAttribute('type');
			//get User ID
			var permID = permissions.readAttribute('id');
			//split Input ID
			var permIDResult = permID.split("_",3);
			//get UserID of parent <tr>
			userIDRow = $('userid_'+permIDResult[1]);
			//setting type values to interger
			switch(permIDResult[2]){
				case "access":
					permLevel = 0;
					break;
				case "view":
					permLevel = 1;
					break;
				case "edit":
					permLevel = 2;
					break;
				default:
					permLevel = 3;
			}
			//check the status of user permissions
			if(permLevel == 0){
				if(permissions.checked == true){
					userIDRow.down('.permissions_access').addClassName("selected");
					userIDRow.down('.permissions_view').addClassName("selected");
					userIDRow.down('.permissions_view input').checked=true;
				}else{
					userIDRow.down('.permissions_access').removeClassName("selected");
					userIDRow.down('.permissions_view').removeClassName("selected");
					userIDRow.down('.permissions_edit').removeClassName("selected");
					userIDRow.down('.permissions_create').removeClassName("selected");
					userIDRow.down('.permissions_view input').checked=false;
					userIDRow.down('.permissions_edit input').checked=false;
					userIDRow.down('.permissions_create input').checked=false;
				}
			}
			if(permLevel == 1){
				if(permissions.checked == true){
					userIDRow.down('.permissions_access input').checked=true;
					userIDRow.down('.permissions_access').addClassName("selected");
					userIDRow.down('.permissions_view').addClassName("selected");
					userIDRow.down('.permissions_view input').checked=true;
					userIDRow.down('.permissions_edit').removeClassName("selected");
					userIDRow.down('.permissions_create').removeClassName("selected");
					userIDRow.down('.permissions_edit input').checked=false;
					userIDRow.down('.permissions_create input').checked=false;
				}else{
					userIDRow.down('.permissions_view').removeClassName("selected");
				}
			}
			if(permLevel == 2){
				if(permissions.checked == true){
					userIDRow.down('.permissions_access input').checked=true;
					userIDRow.down('.permissions_access').addClassName("selected");
					userIDRow.down('.permissions_view').addClassName("selected");
					userIDRow.down('.permissions_view input').checked=false;
					userIDRow.down('.permissions_edit').addClassName("selected");
					userIDRow.down('.permissions_create').removeClassName("selected");
					userIDRow.down('.permissions_edit input').checked=true;
					userIDRow.down('.permissions_create input').checked=false;
				}else{
					userIDRow.down('.permissions_edit').removeClassName("selected");
				}
			}
			if(permLevel == 3){
				if(permissions.checked == true){
					userIDRow.down('.permissions_access input').checked=true;
					userIDRow.down('.permissions_access').addClassName("selected");
					userIDRow.down('.permissions_view').addClassName("selected");
					userIDRow.down('.permissions_view input').checked=false;
					userIDRow.down('.permissions_edit').addClassName("selected");
					userIDRow.down('.permissions_create').addClassName("selected");
					userIDRow.down('.permissions_edit input').checked=false;
					userIDRow.down('.permissions_create input').checked=true;
				}else{
					userIDRow.down('.permissions_create').removeClassName("selected");
				}
			}
		});
	});	
}



/**
 * http://www.boutell.com/newfaq/creating/windowcenter.html
 */
function preview(url, name, w, h) {
	// Fudge factors for window decoration space.
	// In my tests these work well on all platforms & browsers.
	w += 32;
	h += 96;
	wleft = (screen.width - w) / 2;
	wtop = (screen.height - h) / 2;
	
	// IE5 and other old browsers might allow a window that is
	// partially offscreen or wider than the screen. Fix that.
	// (Newer browsers fix this for us, but let's be thorough.)
	if (wleft < 0) {
		w = screen.width;
		wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height;
		wtop = 0;
	}
	var win = window.open(url, "PreviewAdvertisement",
	'width=' + w + ', height=' + h + ', ' +
	'left=' + wleft + ', top=' + wtop + ', ' +
	'location=no, menubar=no, ' +
	'status=no, toolbar=no, scrollbars=no, resizable=no');
	
	// Just in case width and height are ignored
	win.resizeTo(w, h);
	
	// Just in case left and top are ignored
	win.moveTo(wleft, wtop);
	win.focus();
}


function initFeatureSection(){
	/*$('feature-selector_drop-down').observe('change',function(){
		if (!this.value.blank()) {
			window.location.href = '/learn-more/' + this.value;
		}
	});*/
	
	$$('#feature-section_navigation a').each(function(e){
		e.writeAttribute("href","javascript:void(null);").observe('mouseover',function(){
			if (!$('feature-sections').hasClassName('animating') && !$(this).hasClassName('active')) {
				var idParts = this.id.split('_');
				
				$$('#feature-section_navigation a.active')[0].removeClassName('active');
				$(this).addClassName('active');
				$('feature-sections').addClassName('animating');
				
				new Effect.Fade($$('#feature-sections .active')[0], {
					duration: 0.01,
					afterFinish: function(){
						$$('#feature-sections .active')[0].removeClassName('active');
					}
				});
				new Effect.Appear($(idParts[1] + "_" + idParts[2]), {
					duration: 0.01,
					afterFinish: function(){
						$(idParts[1] + "_" + idParts[2]).addClassName('active');
						$('feature-sections').removeClassName('animating');
					}
				});
			}
		});
	});
}



document.observe('dom:loaded', function(){
	externalLinks();
	rollOvers();
	textReplace();
	
	if($('feature-section_container')) {
		initFeatureSection();
	}
});

var currentMenuNav="common_menu_item1", lastMenuNav="common_menu_item1", currentMenuDesc="default_desc", lastMenuDesc="default_desc";
function swapLandingMenuImg(menuItem)
{
	var overY = -31;
	var outY = 0;
	switch(menuItem)
	{
		case 1:
			currentMenuDesc = "desc1";
			currentMenuNav = "common_menu_item1";
			break;
		case 2:
			currentMenuDesc = "desc2";
			currentMenuNav = "common_menu_item2";
			break;
		case 3:
			currentMenuDesc = "desc3";
			currentMenuNav = "common_menu_item3";
			break;
		case 4:
			currentMenuDesc = "desc4";
			currentMenuNav = "common_menu_item4";
			break;
		case 5:
			currentMenuDesc = "desc5";
			currentMenuNav = "common_menu_item5";
			break;
		case 6:
			currentMenuDesc = "desc6";
			currentMenuNav = "common_menu_item6";
			break;
		case 7:
			document.getElementById("desc1").style.display = "block";
			document.getElementById("common_menu_item1").style.top = overY+"px";
			currentMenuDesc="desc1"
			lastMenuDesc="desc1";
			break;
		default:
			document.getElementById("default_desc").style.display = "block";
			break;
	}
	if(lastMenuDesc != currentMenuDesc)
	{
		document.getElementById(lastMenuDesc).style.display = "none";
		document.getElementById(lastMenuNav).style.top = outY+"px";
		document.getElementById(currentMenuDesc).style.display = "block";
		document.getElementById(currentMenuNav).style.top = overY+"px";
	}
	lastMenuDesc = currentMenuDesc;
	lastMenuNav = currentMenuNav;
}

/*-----------------Start Pricing Page Dropdown Menu Functions---------------------*/

var dropDownStatus = "closed";

function dropdownMenu()
{
	if(dropDownStatus == "closed")
	{
		document.getElementById("pricing_dropdown_menu").style.height = "153px";
		document.getElementById("pricing_dropdown_menu").style.background = "url('/img/pricing_dropdown_open.png') 0 0 no-repeat";
		dropDownStatus = "open";
	}
	else if(dropDownStatus == "open")
	{
		document.getElementById("pricing_dropdown_menu").style.height = "28px";
		document.getElementById("pricing_dropdown_menu").style.background = "url('/img/pricing_dropdown_closed.png') 0 0 no-repeat";
		dropDownStatus = "closed";
	}
}

function viewPlan(plan)
{
	document.getElementById("all_plans_heading").style.display = "none";
	document.getElementById("advertiser_plan_heading").style.display = "none";
	document.getElementById("agency_plan_heading").style.display = "none";
	document.getElementById("publisher_plan_heading").style.display = "none";
	document.getElementById("flash_plan_heading").style.display = "none";
	
	switch(plan)
	{
		case 1:
			document.getElementById("all_plans_heading").style.display = "block";
			document.getElementById("all_plans").style.display = "block";
			document.getElementById("pricing_plan1").style.display = "none";
			document.getElementById("pricing_plan2").style.display = "none";
			document.getElementById("pricing_plan3").style.display = "none";
			document.getElementById("pricing_plan4").style.display = "none";
			break;
		case 2:
			document.getElementById("advertiser_plan_heading").style.display = "block";
			document.getElementById("all_plans").style.display = "none";
			document.getElementById("pricing_plan1").style.display = "block";
			document.getElementById("pricing_plan2").style.display = "none";
			document.getElementById("pricing_plan3").style.display = "none";
			document.getElementById("pricing_plan4").style.display = "none";
			break;
		case 3:
			document.getElementById("agency_plan_heading").style.display = "block";
			document.getElementById("all_plans").style.display = "none";
			document.getElementById("pricing_plan1").style.display = "none";
			document.getElementById("pricing_plan2").style.display = "block";
			document.getElementById("pricing_plan3").style.display = "none";
			document.getElementById("pricing_plan4").style.display = "none";
			break;
		case 4:
			document.getElementById("publisher_plan_heading").style.display = "block";
			document.getElementById("all_plans").style.display = "none";
			document.getElementById("pricing_plan1").style.display = "none";
			document.getElementById("pricing_plan2").style.display = "none";
			document.getElementById("pricing_plan3").style.display = "block";
			document.getElementById("pricing_plan4").style.display = "none";
			break;
		case 5:
			document.getElementById("flash_plan_heading").style.display = "block";
			document.getElementById("all_plans").style.display = "none";
			document.getElementById("pricing_plan1").style.display = "none";
			document.getElementById("pricing_plan2").style.display = "none";
			document.getElementById("pricing_plan3").style.display = "none";
			document.getElementById("pricing_plan4").style.display = "block";
			break;
		default:
			document.getElementById("all_plans_heading").style.display = "block";
			document.getElementById("all_plans").style.display = "block";
			document.getElementById("pricing_plan1").style.display = "none";
			document.getElementById("pricing_plan2").style.display = "none";
			document.getElementById("pricing_plan3").style.display = "none";
			document.getElementById("pricing_plan4").style.display = "none";
	}
	document.getElementById("pricing_dropdown_menu").style.height = "28px";
	document.getElementById("pricing_dropdown_menu").style.background = "url('/img/pricing_dropdown_closed.png') 0 0 no-repeat";
	dropDownStatus = "closed";
}

/*-----------------End Pricing Page Dropdown Menu Functions---------------------*/
