$(document).ready(function(){
	$(".rollover-jpg").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".jpg","_over.jpg#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.jpg#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.jpg#hover",".jpg");
				$(this).attr("src",oldSrc);
			}
		}
	);
});

$(document).ready(function(){
	$(".rollover-gif").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".gif","_over.gif#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.gif#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.gif#hover",".gif");
				$(this).attr("src",oldSrc);
			}
		}
	);
});


$(document).ready(function(){
	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	); 

});
 
function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
	   return true;
   }
}

function getStatues(username, maxItems)	{
	$.get("/getStatues.php",  function(xml){
		var i = 0;
		var delay = 2500;
		$('li', '#twitter').remove();
		$('item',xml).each(function(i){
			if (i < maxItems) {
				var title = $(this).find("title").text();
				title = title.substring(0, 255)+'...';
				title = title.replace(username+': ','');
				var pubDate = $(this).find("pubDate").text();
				var url = $(this).find("link").text();
				$("#twitter").append("<li><span>NEWS</span>: <a href=\""+url+"\" target=_blank>"+title+"</a></li>");
				i++;
			}
		});
		/*$('#twitter').cycle({ 
			fx:     'scrollLeft', 
			timeout: 5000, 
			next:   '#twitter',
			pause:   0,
			cleartype:  true,
    		cleartypeNoBg:  true
		});*/
	});	 
}
$(document).ready(function(){

	$("ul.topnav li.parents:not(:last-child)").append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.topnav li.drop").mouseover(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
 
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});


$(document).ready(function(){
	//getStatues('codateam', 1); 
});



function getWindowWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
 
function getWindowHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}


$(document).ready(function() {

	$('a').click(function(){
		this.blur();
	});
	
	// smooth hover effects by DragonInteractive
	var hover = hoverEffects();
	hover.init();

});


/**
 * All credit here goes to DragonInteractive and Yuri Vishnevsky
 */
var hoverEffects = function() {
	var me = this;
	var args = arguments;
	var self = {
		c: {
			navItems: '#main-menu li',
			navSpeed: ($.browser.safari ? 600: 350),
			snOpeningSpeed: ($.browser.safari ? 400: 250),
			snOpeningTimeout: 150,
			snClosingSpeed: function() {
				if (self.subnavHovered()) return 123450;
				return 150
			},
			snClosingTimeout: 700 
		},
		init: function() {
			$('.bg', this.c.navItems).css({
				'opacity': 0
			});
			this.initHoverFades()
		},
		subnavHovered: function() {
			var hovered = false;
			$(self.c.navItems).each(function() {
				if (this.hovered) hovered = true
			});
			return hovered
		},
		initHoverFades: function() {
			//$('#navigation .bg').css('opacity', 0);
			$(self.c.navItems).hover(function() {
				self.fadeNavIn.apply(this)
			},
			function() {
				var el = this;
				setTimeout(function() {
					if (!el.open) self.fadeNavOut.apply(el)
				},
				10)
			})
		},
		fadeNavIn: function() {
			$('.bg', this).stop().animate({
				'opacity': 1
			},
			self.c.navSpeed)
		},
		fadeNavOut: function() {
			$('.bg', this).stop().animate({
				'opacity': 0
			},
			self.c.navSpeed)
		},
		initSubmenus: function() {
			$(this.c.navItems).hover(function() {
				$(self.c.navItems).not(this).each(function() {
					self.fadeNavOut.apply(this);
				});
				this.hovered = true;
				var el = this;
				self.fadeNavIn.apply(el);
			},
			function() {
				this.hovered = false;
				var el = this;
				if (!el.open) self.fadeNavOut.apply(el);
			})
		}
	};
	
	return self;
};

 
function sendForm(lang) {
	var firstname = $("#firstname").val();
	var lastname = $("#lastname").val();
	var phone = $("#phone").val();
	var fax = $("#fax").val();
	var email = $("#email").val();
	var job_title = $("#job_title").val();
	var company = $("#company").val();
	var address = $("#address").val();
	var comments = $("#comments").val();
	var subject = $("#subject").val();
	
	if (firstname=='') 		{ if (lang == 'pl') alert('Podaj imie'); else alert('enter firstname'); return false; }
	if (lastname=='') 		{ if (lang == 'pl') alert('Podaj nazwisko'); else alert('enter lastname'); return false; }
	if (phone=='') 		{ if (lang == 'pl') alert('Wpisz telefon konktaktowy'); else alert('enter phone'); return false; }
	if (!validateEmail(email)) 		{ if (lang == 'pl') alert('Podaj email'); else alert('enter email address'); return false; }
	if (subject =='') 		{ if (lang == 'pl') alert('Wpisz temat wiadomości'); else alert('enter subject'); return false; }
	
	
	if (lang == 'pl') 
		input_box = confirm("Czy wszystkie dane sa poprawne?");	
	else 
		input_box = confirm("Is all information correct?");	
	if (input_box == true)	{
		$.ajax({
		   type: "POST",
		   url: "/send_form.php",
		   data: "firstname="+firstname+"&lastname="+lastname+"&phone="+phone+"&fax="+fax+"&email="+email+"&job_title="+job_title+"&company="+company+"&address="+address+"&comments="+comments+"&subject="+subject,
				 
		   // on success
		   error: function(){
			   	if (lang == 'pl')
				alert('Nie mozna wyslac formularza. Prosze spróbowac jeszcze raz.')
				else 
				alert('Error, please try again.')
		   },
		   success: function(data){
			    if (lang == 'pl') {
					if (data == 'OK') {
					$(".contact-form-container").fadeOut(200).addClass('sent').html('Twoja wiadomość została wysłana').fadeIn(500);
						
						$("#firstname").val('');
						$("#lastname").val('');
						$("#phone").val('');
						$("#fax").val('');
						$("#email").val(''); 
						$("#job_title").val(''); 
						$("#company").val('');
						$("#address").val('');
						$("#comments").val('');
						$("#subject").val('');
						
					} else {
						alert('Nie mozna wyslac formularza. Proszę sprobowac jeszcze raz.');
					}
				} else {
			   		if (data == 'OK') {
					$(".contact-form-container").fadeOut(200).addClass('sent').html('Your message has been sent').fadeIn(500);
						$("#firstname").val('');
						$("#lastname").val('');
						$("#phone").val('');
						$("#fax").val('');
						$("#email").val('');
				 		$("#job_title").val('');
						$("#company").val('');
						$("#address").val('');
						$("#comments").val('');
						$("#subject").val('');
						
					} else {
						alert('Error, please try again.')
					}
				}
		   } 
		});
		return true;
	}
	else {	
		return false; 	
	}
} 

function slideSwitch() {
    var $active = $('#twitter li.active');
	
	
	
	
    if ( $active.length == 0 ) $active = $('#twitter li:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#twitter li:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 0, function() {
            $active.removeClass('active last-active');
        });
	var id = $("#twitter li.active").attr('id');
	$(".hpimg").hide();
	id = id.replace("news_","");
	$("#hp_img_"+id).fadeIn(1000);
	
}



$(function() {
    setInterval( "slideSwitch()", 7000 );
}); 



/*jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; */
