// init.js - load for every page

//document.domain = 'clickmailmarketing.com';

var modalOpts = { overlay: 30, modal: true,  trigger: false };
var popupOpts = { overlay: 70, modal: false, trigger: false };

var onLogo, onQuote, animOn = true, 
	logos = ['accucast.jpg', 'bluehornet.jpg', 'clicktracks.gif', 'easylink.gif', 'emailadvisor.gif', 'emaillabs.jpg', 'exacttarget.gif', 'fax2mail.gif', 'faxREACH.gif', 'fireclick.gif', 'livevox.gif', 'lyris.jpg', 'messageREACH.gif', 'odyssey.gif', 'pgs.gif', 'pivotalveracity.gif', 'protus.gif', 'silverpop.gif', 'sparklist.gif', 'tcn.gif', 'twelve_horses.gif', 'voiceREACH.gif', 'vtrenz.gif', 'xpedite.gif'];



function pageName() {
	var page = document.location.pathname.substring(1);
	var segs = page.split('/');
	return segs[segs.length - 1];
}
function randInt(min, max) {
	return min + parseInt(Math.random() * (max - min));
}



// - - - - - - - -
 
var f, f_err = [];

var page = pageName(), 
	loc  = document.location.toString();

if (!page) 
	page = 'index.html';




// I hate browser sniffing as much as the next guy, but I also 
//  don't like seeing validation errors in the Firefox error console,
//  so browser-specific styles are loaded here dynamically 

var dynStyles = '';

// should this be inside the jquery bit?
var ie6 = $.browser.msie && typeof XMLHttpRequest == 'function';

if (ie6) {
	// ugly, but hey - it's IE6.  It deserves ugliness.
	dynStyles += '* iframe.jqm { position:absolute; top:0; left:0; z-index:-1; width: expression(this.parentNode.offsetWidth+"px"); height: expression(this.parentNode.offsetHeight+"px"); } * html .jqmWindow { position: absolute; top: expression((document.documentElement.scrollTop || document.body.scrollTop) + Math.round(10 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100)+"px"); } * html #wizdiv { top: expression((document.documentElement.scrollTop || document.body.scrollTop) + Math.round(2 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + "px"); } * html div.jqmConfirm { top: expression((document.documentElement.scrollTop || document.body.scrollTop) + Math.round(15 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + "px"); }';
}
if ($.browser.safari) {
	dynStyles += '.sf-shadow ul { -webkit-border-top-right-radius: 17px; -webkit-border-bottom-left-radius: 17px; }';
}

if (dynStyles != '') {
	document.writeln('<style type="text/css"> ' + dynStyles + ' <\/style>');
}




// jquery init stuff

$(function() {
	/*
	if (typeof console == 'undefined') {
		console = { 'log': function(txt){
				window.status = txt; } };
	}
	console.log('doc ready');
	*/

	// make 'ext' links open in new blank window
	$('a[@rel$="ext"]').attr('target', '_blank');
	
	// make logo link to home page etc.
	$('#logo'  ).click( function(){ document.location = 'index.html'; });
	// $('#logos' ).click( function(){ document.location = 'esp.html'; });
	$('#quotes').click( function(){ document.location = 'customers.html'; });
	
	
	// hilight page we're on
	
	var navEl = $('#navUL a[@href$="' + page + '"]');
	//console.log('navEl: ' + typeof navEl);
	
	if (navEl.length > 0) {
		navEl.attr('class', 'on');

		$('.sublinks a[@href$="' + page + '"]').attr('class', 'on');
	
	
		// display subnav we're in and hide from menu system
		var navP = navEl.parents()[1];
		//console.log('navP: ' + navP.id);
		
		//var npParent = $(navP).parents()[0];
		//console.log('npParent: ' + npParent.id);
		
		//alert(typeof navP);
		
		if (!$(navP).is('#navUL')) {
			var npSib = $(navP).siblings()[0];
			//console.log('npSib: ' + npSib.id);
			//$(navP).parent().addClass('skip');
			//$(npParent).find('a').attr('class', 'on');
			$(npSib).attr('class', 'on');
		}
	}
	
	// ok, set up menus
    $('#navUL').superfish();
	
	
	// temporary - popup alert for pages which don't exist yet
	
	$('#navUL a').click(function(){
		$(this).blur();
		
		if ($(this).attr('href') != '#') 
			return true;
		
		alert($(this).text());
		return false;
	});
	
	
	
	
	// I love this effect, but it interferes with the focus in strange ways,
	//  such as stopping the cursor from blinking on FF
	//   maybe try animating just the background / border color?
	/*
    $('.text')
    	.removeAttr('disabled')
    	.fadeTo(0, 0.7)
		.focus(function(){ $(this).fadeTo('slow', 1); })
		.blur(function(){ $(this).fadeTo('slow', 0.7); });
	*/
	
	$('input[type=text],textarea,select')
		.focus(function(){ $(this).animate({backgroundColor: '#fffae8'}, 300); })
		 .blur(function(){ $(this).animate({backgroundColor: '#edf8ff'}, 1000); });




	// override standard alerts with cool jqModal ones
	$('#alert').jqm(modalOpts);
	$('a.alert').click(function() {
		alert('Alert!');
		return false;
	});

	$('#confirm').jqm(modalOpts);
	$('a.confirm').click(function() {
		confirm('Confirm: '+ this.href, this.href);
		return false;
	});
	
	
	// open subnav links in jqm popup
	$('a.iframe').click(function() { popup(this.href); return false; });
	
	
	// expand content area for browsers which don't support min-height
	if (page != 'index.html' && $('#conc').height() < 340) {
		$('#conc').height(340);
	}
	
	// do box opacity thing here cos it supports IE
	$('img.ad').css('opacity',0.8).mouseover(function(){
		$(this).css('opacity',1.0);}).mouseout(function(){
		$(this).css('opacity',0.8);});
	

	//alert('domain = ' + document.domain);
	//alert(loc);
	
	
	// run forms through spiffy new jq validation plug
	$('form').submit(function() { return $.fn.ax_validate(this); });
	
	
	
	// set up animations
	
	onLogo = randInt(0, logos.length);
	
	// preload logo images so safari can size them on the fly
	for (var i = 0;  i < logos.length;  i++) {
		jQuery('<img>').attr('src', '_img/logo/' + logos[i]);
	}
	// setTimeout('nextLogo()', 500);


	//$('#quotes li').show();
	onQuote = randInt(0, $('#quotes li').length);
	nextQuote();
	
	
	
	// turn off anchors on logo clicks
	$('.logos a').click(function() { return false; });
});





// logo animation

function nextLogo() {
	onLogo++;
	if (onLogo >= logos.length) {
		onLogo = 0;
	}

	var im = jQuery('<img>').attr('src', '_img/logo/' + logos[onLogo]);
	$('#logos').html(im);
	//im = $('#logos img')[0];
	//console.log('logo: '+ im.src +'\nheight: '+ $(im).height() +', width: '+ $(im).width());
	
	var dstT = 35 - parseInt($(im).height() / 2) + 'px';
	$(im).css({
		top: dstT, 
		left: 100 - parseInt($(im).width() / 2) + 'px', 
		opacity:0.01});
	$(im).animate({opacity:1.0 /*, top:dstT */}, 2000, '', function(){ setTimeout('outLogo()', 600); });
}

function outLogo() {
	if (!animOn) return;
	
	var im = $('#logos img')[0];
	if (typeof im != 'undefined') {
		//console.log('old logo: '+ im.src +'\nheight: '+ $(im).height() +', width: '+ $(im).width());
		$(im).animate({opacity:0, top:'70px'}, 500, '', nextLogo);
	}
}




function nextQuote() {
	if (!animOn) return;
	
	onQuote++;
	if (onQuote >= $('#quotes li').length) {
		onQuote = 0;
	}
	var el = $('#quotes li:eq(' + onQuote + ')');
	$(el).css({opacity:0.01, top:0, display:'block'});
	$(el).animate({opacity:1}, 1500, '', function(){ setTimeout('outQuote()', 3700); });
}

function outQuote() {
	if (!animOn) return;
	
	var el = $('#quotes li:eq(' + onQuote + ')');
	$(el).animate({opacity:0, top:'90px'}, 300, '', nextQuote);
}




function stopAnim() {
	animOn = true;
	toggleAnim();
}
function toggleAnim() {
	animOn = !animOn;
	if (animOn) {
		$('#animInd').text('on');
		outQuote();
		nextLogo();
	} else {
		$('#animInd').text('off');
	}
}




var urls = {
	'search':  'http://google.com/search?search_query='
	};



function ifsrc(type, extra) {
	
	if (typeof urls[type] != 'undefined') 
		url = urls[type];
	else url = type;
	
	if (typeof extra != 'string') 
		extra = '';
	
	//if ($.browser.msie) 
	//	extra += '" allowtransparency="true';

	return '<iframe frameborder="noborder" id="wiz' + type + '" src="' + url + extra + '" allowtransparency="true" class="wizard"><\/iframe>';
}



function popup(type, str) {
	stopAnim();  // just too much otherwise
	
	if (typeof str != 'string') 
		str = '';
	
	if (type == 'create') {
		str = '?return_url=' + document.location.toString();
		//alert(str);
	}
	
	if (type.match(/\.html$/)) {
		$('.wizard').attr('allowtransparency', false);
		$('#wizdiv').css('background', '#999');
	} else {
		$('.wizard').attr('allowtransparency', true);
		$('#wizdiv').css('background', 'transparent');
	}
	
	$('#waitAnim,.wizard').show();

	$('#wizc').html(ifsrc(type, str));
	$('#wizdiv').jqm(popupOpts).jqmShow();
	
	
	if (type != 'create') 
		$('.jqClose').show();
		
	$('#waitAnim').fadeOut('slow');
	return false;
}


function closePopup(link) {
	$('.wizard').fadeOut('normal', function() {
		$('.jqClose').hide();
		$('#wizdiv').jqm(popupOpts).jqmHide();
		$('.wizard').remove();
		$('#wizc').html('');
	});
	return false;
}





// override javascript native alert & confirm with styled jqm versions

function alert(msg) {
	$('#alert')
		.jqmShow()
		.find('div.jqmConfirmContent')
			.html(msg);
}

function confirm(msg, callback) {
	$('#confirm')
		.jqmShow()
		.find('p.jqmConfirmMsg')
			.html(msg)
		.end()
		.find(':submit:visible')
			.click(function() {
				if (this.value == 'yes') 
					(typeof callback == 'string') ? 
						window.location.href = callback : 
						callback;
				$('#confirm').jqmHide();
			});
}


