var ajaxLoader = '<img src="'+webroot+'img/icons/ajax-loader.gif" class="ajax-loader" />';

$(document).ready(function(){
	
	generalFunctions();
	
	$('#share_email').click(function(){
		createOverlay({}, {});
		$('#in_overlay').load($(this).attr('href'), function(){
			overlayAjaxForm();
		});
		return false;
	});
});

function generalFunctions() {
	overlib();
	external();
	
	
	
	
	$(".project").click(function(event){
		
		 var $target = $(event.target);
		window.location.href = $target.closest('div.project').find('a').attr("href");
		return false;
	});
	
	
}

function createOverlay (attr, attr_in) {
	$('#overlay').remove();
	var addto = ' ';
	$.each(attr, function(key, value) { 
		addto = addto+key+'="'+value+'" ';
	});
	var addto_in = ' ';
	$.each(attr_in, function(key, value) { 
		addto_in = addto_in+key+'="'+value+'" ';
	});
	
	$('body').prepend('<div id="overlay"'+addto+'><div id="close_overlay"><a href="#"><img src="'+webroot+'img/icons/close.gif" alt="sluiten" title="sluiten" border="0" /></a></div><div id="in_overlay"'+addto_in+'>'+ajaxLoader+'</div></div>');
	$('#overlay').fadeIn('slow');
	
	$('#overlay #close_overlay a').click(function(){
		$('#overlay').fadeOut('fast', function(){
			$(this).remove();
		});
		return false;
	});
}

function overlib () {
	$("a").tooltip({
		showURL: false,
		delay: 200, 
	    showURL: false,
	    showBody: " - ", 
	    fade: 0
	});
}

function external() {
	$("a[rel='external']").click(function(){
    	window.open($(this).attr('href'));
    	return false;
    });
}

function overlayAjaxForm () {
	$('#overlay #in_overlay form').submit(function(){
		form = $(this);
		form_values = form.serialize();
		$.ajax({
			url: $(this).attr('action'),
			data: form_values,
			type: 'POST',
			success: function(d) {
				$('#overlay #in_overlay').html(d);
				overlayAjaxForm();
			}
		});
		return false;
	});
}

function getId(ele_id) {
	var split = ele_id.split("_");
	return split[1];
}

function nextAssignmentHeader() {
	current_img = $('#slide_items li img:visible');
	current = current_img.parent();
	next = current.next();
	first = $('#slide_items li:first');
	
	//current.children('div.description').animate({top: '368px'}, {"duration": 600, "easing": "easeOutSine", complete: function(){/*complete*/}});
	current.children('img').animate({opacity: 0.0}, {"duration": 800, complete:function(){
		current.hide();
		if (next.length==0) {
			next = first;
		}
		next.show();
		//next.children('div.description').css('top','368px');
		next.children('img').css('opacity',0.0);
		next.children('img').animate({opacity: 1.0}, {"duration": 800});
		//next.children('div.description').animate({top: '290px'}, {"duration": 600, "easing": "easeOutSine", complete: function(){/*complete*/}});
	}});
}

function nextHomeHeader() {
	
	
	current_img = $('#slider li img:visible');
	current = current_img.parent();
	next = current.next();
	first = $('#slider li:first');
	
	current.children('div.description_block').animate({top: '395px'}, {"duration": 600, "easing": "easeOutSine", complete: function(){/*complete*/}});
	
	current.children('img').delay(3000).animate({opacity: 0.0}, {"duration": 800, complete:function(){
	
		
		current.hide();
		if (next.length==0) {
			next = first;
		}
		next.show();
		next.children('div.description_block').css('top','395px');
		next.children('img').css('opacity',0.0);
		next.children('img').animate({opacity: 1.0}, {"duration": 800});
		next.children('div.description_block').animate({top: '319px'}, {"duration": 600, "easing": "easeOutSine", complete: function(){/*complete*/}});
	
	
	}});
}

function createTableSortable(table, action) {
	$(table).sortable({ 
		items: 'tr', 
		placeholder: 'ui-state-highlight', 
		axis: 'y', 
		handle: '.handle',
		items: 'tr:not(.ui-state-disabled)',
		update: function(e, ui){
            $.ajax({
                type: "POST",
                url: webroot+action,
                data: "itemId=&newOrder="+$(table).sortable('toArray'),
                error: function(msg){
                    $("#JQresult").html(msg);
                },
                success: function(msg){
                    $("#JQresult").html(msg);
                }
            });
		}
	});
}

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeTo = 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();
		});
	};
})(jQuery);
