// JavaScript Document

// EDIT THESE VALUES IF REQUIRED
var alertText = 'Are you sure you wish to perform this action' + "\n"; //DEFAULT TEXT DISPLAYED ON CONFIRM BUTTONS/LINKS WHEN NO ALT/TITLE
var newWindowLink = false; //OPEN EXTERNAL LINKS BY DEFAULT IN A NEW WINDOW (TRUE/FALSE)?


var woms = new Array(); //CREATE ARRAY OF FUNCTIONS TO LAUNCH ONLOAD

//ONLOAD MANAGER
function womOn(){
  window.onload = womGo;
}

function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}

function womAdd(func){
  woms[woms.length] = func;
}

function CreateBookmarkLink() {

	title = document.title;
	url = window.location.href;
	
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if(window.external) {
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) {
		return true;
	}
	
}

function objToggle(obj) { 

	var obj = document.getElementById(obj);

	if(obj.style.display == "block") { 
		obj.style.display = "none";
	} else { 
		obj.style.display = "block";
	}

}

// DOM ready
$(document).ready(function() {
	
	// login form defaults
	if ($('#login_username').length>0) {
		login_form_defaults();
	}
	
	// home page column selector
	if ($('#home_col_1').length>0) {
		init_home_columns();
	}	
	
	// admin page resource type selector
	if ($('#attach_video').length>0) {
		init_resource_selector();
	}		
	
	// hide specify box in reg form
	if ($('#reg_spec').length>0) {
		$('#reg_spec').css('display', 'none');
		$('#reg_spec_label').css('display', 'none');
		
		// show specify box on reg page when options selected
		$('#reg_q3').change(function() {
									 if ($('#reg_q3').val()=='Issued' || $('#reg_q3').val()=='Other') {
										 $('#reg_spec').css('display', 'block');
										 $('#reg_spec_label').css('display', 'block');
									 }
									 else {
										$('#reg_spec').css('display', 'none');
										$('#reg_spec_label').css('display', 'none'); 
									 };
									 });
		$('#reg_q3').change();
	}		
	
	// add dialog to USA selection from registration form
	if ($('#reg_country').length>0) {
		$('#reg_country').change(function() {
									if ($(this).val()=='USA') {
										$('#UsaConfirmBg').css('display', 'block');
										$('#UsaConfirm').css('display', 'block');
										$('#btn_usa_no').click(function() {
																		$('#UsaConfirmBg').css('display', 'none');
																		$('#UsaConfirm').css('display', 'none');
																		});
										$('#btn_usa_yes').click(function() {
																		window.location = 'http://www.magnumboots.com/us/fieldtesters/'; 
																		});										
									}
									 });
	}
	
});

function login_form_defaults() {
	// add click event/blur to username/password boxes
	$("#login_username").click(function () { 
	  if ($("#login_username").attr('value')=='email') {
		  $("#login_username").attr('value', '');
	  }
	});
	$("#login_username").focus(function () { 
	  if ($("#login_username").attr('value')=='email') {
		  $("#login_username").attr('value', '');
	  }
	});	
	$("#login_username").blur(function () { 
	  if ($("#login_username").attr('value')=='') {
		  $("#login_username").attr('value', 'email');
	  }
	});
	
	$("#login_password").click(function () { 
	  if ($("#login_password").attr('value')=='password') {
		  $("#login_password").attr('value', '');
	  }
	});
	$("#login_password").focus(function () { 
	  if ($("#login_password").attr('value')=='password') {
		  $("#login_password").attr('value', '');
	  }
	});	
	$("#login_password").blur(function () { 
	  if ($("#login_password").attr('value')=='') {
		  $("#login_password").attr('value', 'password');
	  }
	});	
}

function init_home_columns() {
	// remove hrefs from links, replacing with js calls
	$('#home_col_3').attr('href', 'javascript:select_home_column(3);');
	
	// add mouseover/out events
	$('#home_col_1').mouseover(function() {
										flipping = false;
										load_bg(0);
										});
	$('#home_col_1').mouseout(function() {
										flipping = true;
										//load_next_bg();
										});	
	$('#home_col_2').mouseover(function() {
										flipping = false;
										load_bg(1);
										});
	$('#home_col_2').mouseout(function() {
										flipping = true;
										//load_next_bg();
										});	
	$('#home_col_3').mouseover(function() {
										flipping = false;
										load_bg(2);
										});
	$('#home_col_3').mouseout(function() {
										flipping = true;
										//load_next_bg();
										});		
	// preload images
    for (i=0; i<bgs.length; i++) {
      var cacheImage = document.createElement('img');
      cacheImage.src = 'images/'+bgs[i];
    }
	
	// start scrolling bg images
	load_next_bg();
}

current_bg=1;
flipping = true;
to=0;
bgs = Array('homeBG21.jpg', 'homeBG22.jpg', 'homeBG23.jpg');
function load_next_bg() {
	bglinks = Array('http://www.magnumboots.com/ion-mask/','http://www.magnumboots.com/','http://www.magnumboots.com/us/fieldtesters/');
	bgx = Array(40, 20, 510);
	bgy = Array(380, 410, 20);
	bgw = Array(477, 477, 120);
	bgh = Array(30, 30, 70);	
	t = 7; 	// secs
	return;
	clearTimeout(to);
	
	// preload next bg
	next_bg = current_bg+1;
	if (next_bg>=bgs.length) { next_bg=0; }
	
	// set timeout to flip
	to = setTimeout(function() {
						if (!flipping) { return; }
						current_bg = next_bg;
						load_bg(current_bg);
						load_next_bg();
						}, t*1000);
	
}

function load_bg(n) {
	$('.homeBG').css('background', 'url(images/'+bgs[n]+') top left no-repeat');
	$('#moreinfo').css('top', bgy[n]);
	$('#moreinfo').css('left', bgx[n]);	
	$('#moreinfo').css('width', bgw[n]);
	$('#moreinfo').css('height', bgh[n]);		
	$('#moreinfo').attr('href', bglinks[n]);	
}

function select_home_column(n) {
	speed = 200;
	
	// remove error box
	if ($('#errorbox').length>0) {
		$('#errorbox').css('display','none');
	}		
	
	if (n==1) { $("#col_1_holder").animate({"marginTop": "150"}, speed); }
	else { $("#col_1_holder").animate({"marginTop": "480"}, speed); }
	
	if (n==2) { $("#col_2_holder").animate({"marginTop": "150"}, speed); }
	else { $("#col_2_holder").animate({"marginTop": "480"}, speed); }
	
	if (n==3) { $("#col_3_holder").animate({"marginTop": "0"}, speed); }
	else { $("#col_3_holder").animate({"marginTop": "480"}, speed); }	
	
}

function init_resource_selector() {
	// remove hrefs and replace with js calls	
	$('ul.attach a.video').attr('href', 'javascript:select_resource_type(\'video\');');
	$('ul.attach a.photo').attr('href', 'javascript:select_resource_type(\'photo\');');
	$('ul.attach a.link').attr('href', 'javascript:select_resource_type(\'link\');');
	$('ul.attach a.event').attr('href', 'javascript:select_resource_type(\'event\');');
	
	// add close buttons to all panels
	$('.attach_inner').prepend('<a class="closebox" title="Close" href="javascript:closebox();">x</a>');
	
	// look for initial selection
	if ($("#attach_video").css('display') == 'block') { selected_type='video'; }
	if ($("#attach_photo").css('display') == 'block') { selected_type='photo'; }
	if ($("#attach_link").css('display') == 'block') { selected_type='link'; }
	if ($("#attach_event").css('display') == 'block') { selected_type='event'; }
}

selected_type='';
isloading=false;
speed = 300;
function select_resource_type(type) {	
	// already fading, just be patient
	if (isloading) { return; }
	
	// already selected, don't be greedy
	if (selected_type==type) { return; }
	
	isloading=true;
	
	// initial selection, nothing to fade out
	if (selected_type=='') {
		$("#attach_"+type).fadeIn(speed, function() { isloading=false; });
		selected_type = type;
		return;
	}
	
	// clear values before hiding
	$("#res_"+selected_type).val('');

	// fade out current selection, then fade in new item
	$("#attach_"+selected_type).fadeOut(speed, function() {
		$("#attach_"+type).fadeIn(speed, function() { isloading=false; });
	});
	selected_type = type;
}

// close currently selected popup
function closebox() {
	isloading=true;
	$("#attach_"+selected_type).fadeOut(speed, function() { isloading=false; });
	$("#res_"+selected_type).val('');
	selected_type='';
}
