$(document).ready(function() {

$('img.attachment-post-thumbnail').one('load',function () {
	var img = $(this);
	var img_width = img.width();
	var img_height = img.height();
	
	// build wrapper
	var wrapper = $('<div class="rounded_wrapper"></div>');
	wrapper.width(img_width);
	wrapper.height(img_height);
	
	// move CSS properties from img to wrapper
	wrapper.css('float', img.css('float'));
	img.css('float', 'none')
	
	wrapper.css('margin-right', img.css('margin-right'));
	img.css('margin-right', '0')

	wrapper.css('margin-left', img.css('margin-left'));
	img.css('margin-left', '0')

	wrapper.css('margin-bottom', img.css('margin-bottom'));
	img.css('margin-bottom', '0')

	wrapper.css('margin-top', img.css('margin-top'));
	img.css('margin-top', '0')

	wrapper.css('display', 'block');
	img.css('display', 'block')

	// IE6 fix (when image height or width is odd)
	if ($.browser.msie && $.browser.version == '6.0')
	{
		if(img_width % 2 != 0)
		{
			wrapper.addClass('ie6_width')
		}
		if(img_height % 2 != 0)
		{
			wrapper.addClass('ie6_height')			
		}
	}

	// wrap image
	img.wrap(wrapper);
	
	// add rounded corners
	img.after('<div class="tl"></div>');
	img.after('<div class="tr"></div>');
}).each(function(){
	if(this.complete) $(this).trigger("load");
});
	
});
$(document).ready(function() {		
	
	//Execute the slideShow, set 4 seconds for each images
	slideShow(7000);
 
});
 
function slideShow(speed) {
 
 
	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');
 
	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	$('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
	$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
		
	//Display the caption
	$('#slideshow-caption').css({opacity: 0.7, bottom:0});
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over
	$('ul.slideshow').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	
}
function isEmail(str) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(str) ? true : false);
}
function is_alpha(test, v) {

	if (v == "a") var lists = "abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-./ ";
	if (v == "b") var lists = "1234567890abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-!,.?()/ ";
	if (v == "c") var lists = "1234567890_-/.\\ ";
	if (v == "d") var lists = "1234567890";
	else var lists = "1234567890abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-./ ";

	var match = true;
	var found = false;
	var chars = "";

	for (var i = 0; i < test.length; i++) {
		chars = test.charAt( i );
		found = false;
		for (var j = 0; j < lists.length; j++) {
			if(lists.charAt( j ) == chars) {
				found = true;
				break;
			}
		}
		if (!found) {
			match = false;
			break;
		}
	}
	return match;
}
function gallery() {
 
 
	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
 
	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	
 
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	//Hide the caption first, and then set and display the caption
	$('#slideshow-caption').animate({bottom:-70}, 300, function () {
			//Display the content
			$('#slideshow-caption h3').html(title);
			$('#slideshow-caption p').html(desc);				
			$('#slideshow-caption').animate({bottom:0}, 500);	
	});		
 
	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');
 
}
 
/* ]]> */
function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};

$(document).ready(function() {
var twitter = $('#twittermodul .textwidget').html();
$('#twittermodul .textwidget').rdy(function() {
	$.getScript("http://twitter.com/statuses/user_timeline/"+twitter+".json?callback=twitterCallback2&count=5");
});
});
function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0, len=twitters.length; i<len; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  var twitter = $('#twittermodul .textwidget').html();
  $('#twittermodul .textwidget').html('<ul>'+statusHTML.join('')+'</ul><div id="footertwit"><a href="http://twitter.com/'+twitter+'" title="Follow us on Twitter">Follow us on Twitter</a></div>');
}
$(document).ready(function(){
	$('#newslet').submit(function(){
		var vname = $('#namenews').val();
		var vemail = $('#emailnews').val();
		var vademail = $('#ademail').val();
		if(isEmail(vademail)){
		if(vemail == '' || vemail == 'your Email here'){
			alert('Please enter your email address.');
			return false;
		} else if(!isEmail(vemail)){
					alert('This e-mail address is not valid.\r\nPlease enter a valid email address (e.g. myname@domain.com');
					return false;
		} else if(vname == '' || vname == 'your Name here'){
			alert('Please enter your name');
			return false;
		} else{
			var html = $.ajax({
						type: "POST",
						url: "wp-content/themes/aiesec/js/server.php",
						data: "email="+vemail+"&name="+vname+"&ademail="+vademail+"&action=getnewsletter",
						async: false
					}).responseText;
					if(html.replace('\r\n','') == 'trimis') {
						alert('Newsletter subscription has been successfully.');
						return false;
				} else if(html.replace('\r\n','') == '') {
					alert('Subscribe to the newsletter could not be completed, please try again.');
					return false;
				}
				alert('ERROR: Please try again.');
			return false;
		}
		
	} else {
		alert('ERROR: Please try again.');
		return false;
	}
	
	});
	$('#contact').submit(function(){
		var vemail = $('#email').val();
		var vnume = $('#namecontact').val();
		var vtelefon = $('#telefon').val();
		var ademail = $('#ademail').val();
		var vmsg1 = $('#msg').val();
		var vmsg2 = $('#details').val();
		if(vemail == '' || vemail == 'your Email here'){
			alert('Please enter your email address.');
			return false;
		} else if(!isEmail(vemail)){
					alert('This e-mail address is not valid.\r\nPlease enter a valid email address (e.g. myname@domain.com)');
					return false;
		} else if(vnume == '' || vnume == 'your Name here'){
			alert('Please enter your name');
			return false;
		} else if(vtelefon == '' || vtelefon == 'your Phone number'){
			alert('Please enter your phone number');
			return false;
		} else if(!is_alpha(vtelefon, 'd') || vtelefon.replace(/[^0-9]/g,'').length != 10){
			alert('This phone number is not valid\r\nPlease enter a valid phone number (e.g. 07xxxxxxxx)');
			return false;
		} else {
			var html = $.ajax({
						type: "POST",
						url: "wp-content/themes/aiesec/js/server.php",
						data: "ademail="+ademail+"&email="+vemail+"&nume="+vnume+"&telefon="+vtelefon+"&mesaj1="+vmsg1+"&mesaj2="+vmsg2+"&action=getcontact",
						async: false
					}).responseText;
					if(html.replace('\r\n','') == 'introdus') {
						$('#contact')[0].reset();
						alert('Email was successfully sent! Thank you!');
						return false;
				} else if(html.replace('\r\n','') == '') {
					alert('The email could not be sent, please try again!');
					return false;
				}
			return false;
		}
	});
});
function setupDemos() {

    Shadowbox.setup("a.nobord", {
        gallery:        "mustang",
        continuous:     true,
        counterType:    "skip"
    });
}

