$(document).ready(init);
function init() {
$(document.body).removeClass('jsOff').addClass("jsOn");
//add hover to menu LIs
if ($.browser.msie) {
	$('#subMenu li').hover(function() { 
		$(this).toggleClass("over"); },function() {
		$(this).toggleClass("over"); 
	});
}
	$('#subMenu').hover(function() { 
		$(this).toggleClass("over"); },function() {
		$(this).toggleClass("over"); 
	});

$('.menuPngDarken').find('a').each( function() { 
	applyPngSpan(document.body.getAttribute('id'),this);
}).end().hover(function() {
	$(this).toggleClass('applyPng');
},function() {
	$(this).toggleClass('applyPng'); 
})

if ((getId() == 'services') || (getId() == 'partners')) {
	var hash = unescape(location.search.substring(1, location.search.length));
	if (hash.length > 0) {
		showPart(hash, null);
	} else { 
		showPart('q1',null);
	}
	$('#subMenu li').click(function(e) {
		if ($(this).is('.current')) { 
			return false;
		};
		var a=$(this).find('a')[0];
		a = a.getAttribute('href',2); a = a.split('#')[1];
		showPart(a,this);
		cancelClick(e);
	});
} else {
	$('#subMenu li').click(function(e) {
		if ($(this).is('.current')) { 
			return false;
		};
		var a=$(this).find('a')[0];
		var h = a.getAttribute("href");  
		var tt = a.getAttribute("target"); //if target -> open in new window, cancel a proper click
		!tt ? location = h : window.open(h); cancelClick(e);
	});
}

if ($('#footer')[0]) {
	$('#footer').find('a[@href]').each( function() { 
		$(this).hover(function() {
		if ($(this).parent().attr("class") == getId()) return;
			$(this).parent().addClass('over').next().addClass('over');
		},function() {
			$(this).parent().removeClass('over').next().removeClass('over'); 
		});
		$(this).click(function() {
		if ($(this).parent().attr("class") == getId()) return false;
		});
	});
}

if (getId()=='contact') {
	$.ajax( {url: 'code/ajax_contact.xml', success: function() {	
	prepareContactForm();
	}})
}

/*@cc_on
document.execCommand("BackgroundImageCache", false, true);
@*/

} // end init


function getId() {
return document.body.getAttribute('id');
};
function applyPngSpan(id,a) {
	var t = $(a);
	if (t.parent().is('.'+id)) {
	var w = t.css('width'); var h = t.css('height'); //need for ie (all)
			t.html('<span style=\'width:'+w+';height:'+h+';\'>'+t.html()+'</span>');
	}
};
function showPart(_this,p) {
// 	scroll(0,0);
	if (document.getElementById('displayed') != null) {
		var t = $(document.getElementById(_this));
		$('#displayed').fadeOut('normal', function() {
			$(this).html(t.html()).fadeIn('normal'); 
		}); 
		$('#subMenu .current').removeClass('current');
		if (p) $(p).addClass('current');
	} else {
		var t = $(document.getElementById(_this));
		if (p==null) { p=t.parent().addClass('current'); }
		var d = $(document.getElementById('faqAns'));// find corresp div(answ)
		divT = document.createElement( 'div' );// create container for showing answ
		$(divT).attr('id','displayed').html(t.html()).insertAfter(d).fadeTo('slow',1);
		var a = $('#subMenu').find('a[@href$=#'+_this+']')[0];
		var aa = $(a.parentNode);
		$(aa).addClass('current');
	}
	
};

function prepareContactForm() {
	if (jQuery.browser.msie) {
		$('#submitBtn').hover(function() { 
		$(this).toggleClass("over"); },function() {
		$(this).toggleClass("over"); });
		$('input').focus(function() {$(this).addClass('ieFocus')});
		$('input').blur(function() {$(this).removeClass('ieFocus')});
		$('textarea').focus(function() {$(this).addClass('ieFocus')});
		$('textarea').blur(function() {$(this).removeClass('ieFocus')})
	};
	if($.browser.safari) {
		$("input").addClass('safari');
		$("textarea").addClass('safari');
	}
		$("input").focus( function() { 
			if($(this).next().is(':visible')) { $(this).next().hide('slow')}
		});
		var options = { beforeSubmit: validate, success: showResponse };
		$('#form1').ajaxForm(options); //initiate Alsup's form plugin
};

//start of contact form module
function validate(formData, jqForm, options) { 
	var form = jqForm[0];
	if (!form.name.value) { $("#nameErr").show(); }
	if (emailCheck(form.email.value)) { $("#emailErr").show(); }
	if (!form.name.value || emailCheck(form.email.value)) return false;
};

function showResponse()  {
	$('#form1').unbind('submit'); $('#submitBtn').unbind('click');
	$('#form1').hide('slow',function() {
	$('#thankYou').fadeIn('slow')});
};

function emailCheck(value) {
	return !value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
};
//END of contact form module

function cancelClick(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
};
