///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var browser = '';
var tempString = '';
var borderCount = 0; //for IE6 support of the stripes border
var showSubscribe = false;
var errrors = 0;
var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var subscribe_timer;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$().ready(function() {
  $('#dialog').jqm();
});

$(document).ready(function(){
	checkBrowser();
	resize();
	$(window).resize(function(){
		 resize();
	});
	//------------------------------------------------------------
	compatibility();
	btns_init();

});
 	    WebFontConfig = {
         custom: { families: [ 'PrimerPrint-Regular' ] }
       };
       (function() {
         var wf = document.createElement('script');
         wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
             '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
         wf.type = 'text/javascript';
         wf.async = 'true';
         var s = document.getElementsByTagName('script')[0];
         s.parentNode.insertBefore(wf, s);
       })();
 





//==================================================================================================================
//check browser
function checkBrowser(){
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) browser = 'IE'+new Number(RegExp.$1); //IE
	else if (navigator.userAgent.indexOf("Firefox")!=-1) browser = 'FF'; //firefox
}
//==================================================================================================================
//when browser resizes
function resize(){
	
	$('#holder').height($('document').height()-140);
	
	if($('body').height()<=650){	
		$('#holder').height($(document).height()-140);
		$('#footer').css({top:650});

	}else{
		$('#holder').height($('body').height()-140);
		$('#footer').css({top:"auto"});
		$('#footer').css({bottom:0});
	}
	//------------------------------------------------------------
	if(browser=='IE6'){
		if(Math.ceil($('body').width()/198) != borderCount){
			borderCount = Math.ceil($('body').width()/198);
			tempString = '<div class="area">';
			for(var i=0;i<=borderCount;++i){
				tempString += '<img src="images/border-slashes.png" width="198" height="22" class="png" />';
			}
			tempString += '</div>';
			$('#topBorder').html(tempString);
			$('#btmBorder').html(tempString);
			$('#topBorder .area, #btmBorder .area').width(borderCount*199);
		}
	}
	//------------------------------------------------------------
}
//==================================================================================================================
//compatibility with various browsers
function compatibility(){
	if(browser=='IE6'){
		$('#topBorder, #btmBorder').css({'background-image': 'none', 'overflow': 'hidden'});
	}
	if(browser=='IE6' || browser=='IE7'){
		$('li.lvl1 a').mouseover(function(){$(this).css('cursor', 'pointer')});
		$('.mockBtn').mouseover(function(){$(this).css('text-decoration', 'none')});
	}
}
//==================================================================================================================
//navigation functionality
function btns_init(){
	$('ul#navi').superfish({delay:200, disableHI:false, autoArrows:false, speed:'medium', animation:{height:'show'}});
	//------------------------------------------------------------
	$('ul#navi li.lvl0').hover(
		function(){
			//$(this).find('a.lvl0').not('a.active').stop().animate({backgroundColor:'#333', color:'#FFF'}, 200);
			$(this).find('a.lvl0').not('a.active').stop().animate({backgroundPosition:'0px -24px', color:'#FFF'}, {duration: 250, easing:'easeOutQuad'});
		},
		function(){
			//$(this).find('a.lvl0').not('a.active').stop().animate({backgroundColor:'#FFF', color:'#333'}, 300);
			$(this).find('a.lvl0').not('a.active').stop().animate({backgroundPosition:'0px 1px', color:'#333'}, {duration: 250, easing:'easeInQuad'});
		}
	);
	//------------------------------------------------------------
	$('ul#footerNavi li').hover(
		function(){
			$(this).find('a').not('a.active').stop().animate({backgroundPosition:'0px -24px', color:'#FFF'} , {duration: 250, easing:'easeOutQuad'});
		},
		function(){
			$(this).find('a').not('a.active').stop().animate({backgroundPosition:'0px 1px', color:'#333'}, {duration: 250, easing:'easeInQuad'});
		}
	);
	//------------------------------------------------------------
	$('#footer a.mailing').click(function(){
		if(showSubscribe!=true) {
			showSubscribe = true;
			if(browser!='IE7'){

				$('#footer #subscribe').css({'bottom': 35});
				$('#footer #subscribe').css({'z-index': 5}).stop().animate({bottom:45, opacity: 1}, 500);
				$('#footer #subscribe').css({'position':'absolute'});
			}
			else{
	
				$('#footer #subscribe').css({'bottom': 35});
				$('#footer #subscribe').css({'z-index': 5}).stop().animate({bottom:40, opacity: 1}, 500);
				$('#footer #subscribe').css({'position':'absolute'});
			}
		}else hideForm();
	});
	//------------------------------------------------------------
	$('#footer #subscribe .close a').click(function(){
		hideForm();
	});
	//------------------------------------------------------------
	$('#footer #subscribe a.mockBtn').click(function(){
		errors = 0;
		if($('#subscribe input.name').val().length==0) {
			errors++;
			$('#subscribe input.name').addClass('error');
		}else{
			$('#subscribe input.name').removeClass('error');
		}
		//...........
		if($('#subscribe input.email').val().length==0) {
			errors++;
			$('#subscribe input.email').addClass('error');
		}else if(regex.test($('#subscribe input.email').val())!=true){
			errors++;
			$('#subscribe input.email').addClass('error');
		}else{
			$('#subscribe input.email').removeClass('error');	
		}
		//...........
		if(errors>0){
			$('#subscribe td.errors').html(errors+' error(s) detected in your form');
		}else{
			subscribe($('#subscribe input.name').val(), $('#subscribe input.email').val());
		}
	});
	//------------------------------------------------------------
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function hideForm(){
	$('#footer #subscribe').stop().animate({bottom:35, opacity: 0}, 300, clearForm);
}
//==================================================================================================================
function clearForm(){
	$('#footer #subscribe').css({'z-index': -1});
	if(browser!='IE7'){
		$('#subscribe table, #subscribe .aligner').css('display', 'inline-block')
	}else{
		$('#subscribe table, #subscribe .aligner').css('display', 'inline');
	}
	$('#subscribe input').val('');
	$('#subscribe input.error').removeClass('error');
	$('#subscribe td.errors').html('');
	$('#subscribe').css('cursor', 'default');
	$('#subscribe').unbind('click');
	$('#subscribe .response').html('').css('display', 'none');
	clearTimeout(subscribe_timer);
	showSubscribe = false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function subscribe(name, email){
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
	else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			$('#subscribe table, #subscribe .aligner').css('display', 'none');
			$('#subscribe .response').fadeIn('fast').html(xmlhttp.responseText);
			$('#subscribe').css('cursor', 'pointer');
			$('#subscribe').click(function(){hideForm();});
			subscribe_timer = setTimeout(hideForm, 4000);
		}
	}
	xmlhttp.open("GET","subscribe.php?name="+name+'&email='+email,true);
	xmlhttp.send();	
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
