/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	WEB: 			seleccion-it.es                                                      *
 *	FICHERO: 		default.js                                                           *
 *	DESCRIPCIÓN:            Programación javascript genérica del sitio                           *
 *	AUTOR: 			Alberto Martínez                                                     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

$(document).ready(function(){ 

	/* * * G E N E R A L * * */
	
	Cufon.replace('h2');
	$('a.scrollTo').click(function(){
		var to = $(this).attr('href');
		$.scrollTo(to, 1200);
		return false;
	});
	
	/* * * F O O T E R * * */
	
	// Sidebar Accordion Menu Hover Effect:
 	/*$(".sitemap2 li").hover(function(){
		$(this).stop().animate({ paddingLeft: "5px" }, 100);
	},function () {
		$(this).stop().animate({ paddingLeft: "0" });
	});*/
	
	 
	/* * * N A V I G A T I O N  B A R * * */
	
	$("#main-nav li.top-menu a.main-link").siblings(".sub-links").clone().appendTo("#sub-link-bar-wrapper");
    $("#main-nav li.top-menu a.main-link").each(function(i){ 
		$(this).hover(function(){
			$("#main-nav li.top-menu a.main-link").removeClass("active");
			$(this).addClass("active");
			$("#navigation-sub").animate({height: "40px"});
			$("#sub-link-bar-wrapper .sub-links:not(:eq("+i+"))").hide();
			$("#sub-link-bar-wrapper").children().eq(i).fadeIn();
		}, 
		function () { // jQuery 1.4 
		});  
	});
	
	$("#main-nav li.drop-down").hoverIntent(function(){  
		$(this).children("ul").slideDown(200);
		$(this).children("a").addClass("active2");
	 },
	 function(){  
		$("#main-nav li.drop-down ul").slideUp(200);
		$("#main-nav li.drop-down a").removeClass("active2");
	});
	
	function close(){
		$("#main-nav li a.main-link").removeClass("active");                                                                                       
        $("#sub-link-bar-wrapper .sub-links").fadeOut();
        $("#navigation-sub").animate({height: "15px"});
	}
	var mouseIsOverA = false;
	var mouseIsOverB = false;
	
	var configB = {    
     sensitivity: 50, // number = sensitivity threshold (must be 1 or higher)    
     interval: 1, // number = milliseconds for onMouseOver polling interval    
     over: function(){ mouseIsOverB = true; },   
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: function(){  
		mouseIsOverB = false;
		if (!mouseIsOverA){
			close();
		}  
	 }
	};

	$("#navigation-sub").hoverIntent(configB);
	
	var configA = {    
     sensitivity: 50, // number = sensitivity threshold (must be 1 or higher)    
     interval: 1, // number = milliseconds for onMouseOver polling interval    
     over: function(){ mouseIsOverA = true; },   
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: function(){ 
		mouseIsOverA = false;
		if (!mouseIsOverB){
			close();
		} 
	 }
	};

	$("#main-handle").hoverIntent(configA);
	
	
	/* * * U P L O A D  C V * * */
	
	$('.info_div').click(function() {$(this).fadeOut('slow')});
	

	/*$('ol#listings li a').hover(function(){           
		$(".cover", this).stop().animate({right:'0px'},{queue:false,duration:300});  
		}, function() {  
         $(".cover", this).stop().animate({right:'-260px'},{queue:false,duration:300});  
     }); */
	 
	 
	/* * * G O O G L E  M A P * * */
	
	function load() {
		if (GBrowserIsCompatible()) {
			var map   = new GMap2(document.getElementById("googlemap"));
			var geocoder = new GClientGeocoder();
			var myLatLon = geocoder.getLatLng('Carretera de Canillas 138, Madrid', function(point) {
				if (point){
					map.setCenter(point, 15);
					var baseIcon = new GIcon(G_DEFAULT_ICON);
					baseIcon.shadow = "./img/markershadow.png";
					baseIcon.shadowSize = new GSize(129, 42);
					baseIcon.iconSize = new GSize(101, 42);
					baseIcon.iconAnchor = new GPoint(35, 45);
					baseIcon.infoWindowAnchor = new GPoint(5, 45);

					var customIcon = new GIcon(baseIcon);
					customIcon.image = "./img/marker.png";
					// Set up our GMarkerOptions object
					markerOptions = { icon:customIcon };
					var marker = new GMarker(point, markerOptions);
					map.addOverlay(marker);
				}
				map.addControl(new GSmallMapControl());
			});
		}
	}
	 
	$('#open').click(function() {
		$('#map').fadeIn('slow');
		load();
		return false;
	});
	$('#close').click(function() {
		$('#map').fadeOut('slow');
		GUnload();
		return false;
	});
	
	/* * * C O N S U L T A N T S * * */
	
	$('.toggle').bind("click", function(e){
		var el = $(this).next();
		if ($(this).hasClass('hidden')){
 			$(this).removeClass('hidden').addClass('visible');
 			el.slideDown();
 		} else {
 			$(this).removeClass('visible').addClass('hidden');
 			el.slideUp();
 		}
 		e.preventDefault();
 	});
	
	/* * * N E W S * * */
	
	/*$('#new-author').click(function(ev){
	 //if (ev.detail == 1){
		var nextId = $('#latest-news ~ div:last').attr('id');
		$.post("/seleccion-it/news/recent_news", {'nid' : --nextId}, function(data) {  
			if (data != ''){
				$('#latest-news ~ div:first').fadeOut('slow', function(){
					$(this).remove();
					$('#latest-news ~ div:last').removeClass('omega').after(data);
					$('#latest-news ~ div:last').addClass('omega');
					$('#latest-news ~ div').hide().fadeIn('slow');
				});
			}
		});
		//}
	});*/
	$('#control #older').click(function(){
		var nextId = $('#latest-news ~ div:last').attr('id');
		$.post("/seleccion-it/news/recent_news", {'nid' : --nextId}, function(data) {  
			if (data != ''){
				$('#latest-news ~ div:first').remove();
				$('#latest-news ~ div:last').after(data);
				$('#latest-news ~ div').hide().fadeIn('slow');
			}
		});
		return false;
	});
	$('#control #recent').click(function(){
		var nextId = $('#latest-news ~ div:first').attr('id');
		$.post("/seleccion-it/news/recent_news", {'nid' : ++nextId}, function(data) { 
			if (data != ''){		
				$('#latest-news ~ div:last').remove();
				$('#latest-news ~ div:first').before(data);
				$('#latest-news ~ div').hide().fadeIn('slow');
			}
		});
		return false;
	});

        $('.notification a.close').click(function(){
            $(this).parent().fadeOut();
            return false;
        });
		
	
	
});

function loading(){
    $('body').append('<div id="overlay"><div id="loading"><div></div>');
    $('#overlay').css('height',$(document).height()).css('opacity',0).fadeTo('fast',0.5);
    /*$('#overlay').click(function(){
	$('#overlay').fadeOut('fast',function(){ $(this).remove(); });
    });*/
}
function loaded(){
    $('#overlay').fadeOut('fast',function(){ $(this).remove(); });
}

function notification(text, type){
    $('.notification div').html(text);
    switch(type){
        case 'SUCCESS':
            $('.notification').attr('class', 'notification success');
        break;
        case 'ERROR':
            $('.notification').attr('class', 'notification error');
        break;
        case 'ATTENTION':
            $('.notification').attr('class', 'notification attention');
        break;
        case 'INFORMATION':
            $('.notification').attr('class', 'notification information');
        break;
        default:
            return false;
        }
    $('.notification').fadeIn();
    // Cerrar la notificacion pasado un tiempo.
    window.setTimeout(function(){
        $('.notification a.close').click();
    }, 10000);
    return true;
}

// http://gruposistemas.com/gsplayer
function swf(id,filename, width, height, flashvars, params, attributes)
{
	var arrayFlvars = new Array();
	for(var i in flashvars){ arrayFlvars.push(i+'='+flashvars[i]); }
	flashvars = arrayFlvars.join('&');

	var arrayAttrs = new Array();
	for(var i in attributes){ arrayAttrs.push(i + '="' + attributes[i] + '"'); }
	attributes = arrayAttrs.join(' ');

	var html = '<object id="'+id+'" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash" data="'+filename+'"><param name="movie" value="'+filename+'?'+flashvars+'"></param>';
	var arrayParams = new Array();
	for(var i in params){
		html += '<param name="'+i+'" value="' + params[i] + '" />';
		arrayParams.push(i+'="'+params[i]+'"');
	}
	html += '<param name="flashvars" value="' + flashvars + '" />';
	params = arrayParams.join(' ');
	html += '</object>';
	return html;
}
