
$(document).ready(function(){
	
///Country Selection Slider Panel	
	$(".select_country").click(function(e){	
		 e.preventDefault();
		 
		$('#country_selection').slideDown("slow").click(function(){		
			$("#country_selection").slideUp("slow");			
		});
		
		$('#country_selection a').click(function(){
			$("#country_selection").hide();
			$("#loader").fadeIn('fast');			
		});
	});

	
///Tabs
	
	$(function() {
		$( "#tabs" ).tabs();
	});
	
	
/// wobbly logo	
	$('#logo').hover(function() {
        $(this).stop().animate({ marginTop: '6px' }, 150).css('cursor','pointer');
    },function(){
        $(this).stop().animate({ marginTop: '0px' }, 85).animate({ marginTop: '3px' }, 150);
    });
	
	
/// Search box - header
	$('#quick_query').click(function(){
		$('#quick_query').animate({			
			width: 230			
		}, 500).css({'color':'#034820', 'background':'#BCE0CB url(/website/static/source/img/layout/search_magnify_glass.png) 8px -16px no-repeat'});
	});
	
	$('#quick_query').focusout(function(){
		$('#quick_query').animate({		
			width: 100					
		}, 500).css({'color':'#BCE0CB', 'background':'#057735 url(/website/static/source/img/layout/search_magnify_glass.png) 8px 4px no-repeat'});

	});
	

/// Admin Menu - Add active class

	var path = location.pathname;
	$("#admin_navigation a[href='" + path + "']").each(function() {   
	        $(this).parent().addClass("active");
	});

	
/// prettyPhoto lightbox	

	$("a[rel^='prettyPhoto']").prettyPhoto({
		default_width: 640,
		default_height: 480,
		deeplinking: false,
		social_tools: null
	});
	
	
/// cufon text enhancement
//	Cufon.replace('h1');
//	Cufon.replace('h2');
	
	$(window).load(function() {
		$('.ajax-loader-showcase').hide();
		$('#slider').nivoSlider({
			effect:'fade',
			//controlNavThumbs:true,
		    //controlNavThumbsFromRel:true,
			controlNavThumbs:false,
		    animSpeed:500,
		    pauseTime:8000,
		    controlNavThumbsSearch: '_large.jpg', 
	        controlNavThumbsReplace: '_thumb.jpg'
		}).show();
		
	});
	
	$(window).load(function() {
		$('.slider2').nivoSlider({
			effect:'fade',
			//controlNavThumbs:true,
		    //controlNavThumbsFromRel:true,
			controlNavThumbs:false,
		    animSpeed:500,
		    pauseTime:8000,
		    controlNavThumbsSearch: '_large.jpg', 
	        controlNavThumbsReplace: '_thumb.jpg'
		});
	});

	
	

	
	
/* BEGIN TWITTER JS */	
	/****
	 * Homepage Tweeting Mini Carousel
	 * 
	 *****/	

	$.getJSON('http://twitter.com/status/user_timeline/iesve.json?count=5&callback=?', function(data){
		if ((location.host == 'shields7.iesve.com')||(location.host == 'dev.iesve.com')) return;
		$.each(data, function(index, item){
			$('#twitter_feed_list').append('<li><div class="twitter_feed_tweet"><p>' + item.text.linkify() + '</p><p>' + relative_time(item.created_at) + '</p></div></li>');
		});
		$(function() {
		    $(".twitter_feed_slides").jCarouselLite({
		        btnNext: " .next_tweet",
		        btnPrev: " .prev_tweet",
		        visible: 1,
		        vertical: true,
		        auto: 6500,
		        speed: 2000
		    });
		});
	});	
	
	
	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);
		  
		  var r = '';
		  if (delta <= 60) {
			r = 'a minute ago';
		  } else if(delta < 120) {
			r = 'couple of minutes ago';
		  } else if(delta < (45*60)) {
			r = (parseInt(delta / 60)).toString() + ' minutes ago';
		  } else if(delta < (90*60)) {
			r = 'an hour ago';
		  } else if(delta < (24*60*60)) {
			r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
		  } else if(delta < (48*60*60)) {
			r = '1 day ago';
		  } else {
			r = (parseInt(delta / 86400)).toString() + ' days ago';
		  }		  
		  return r;
		}
		
		String.prototype.linkify = function() {
			linkTarget = '_blank'; 
			return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
				a = m.link(m);
				return a.replace(/<a/, '<a target="'+linkTarget+'"');
			});
		};
		

/* END TWITTER JS */
		
});


function gotoLink (htmlElement) {
	
	var destinationURL = "";
	var target = "";
	
	if(htmlElement.href) {
		destinationURL = htmlElement.href;
		target = htmlElement.getAttribute("target");
	}
	else {
		if(htmlElement.childNodes) {
			if(htmlElement.getElementsByTagName("a")) {
				destinationURL = htmlElement.getElementsByTagName("a")[0].href;
				target = htmlElement.getElementsByTagName("a")[0].getAttribute("target");
			}
		}
	}
	
	if(target == "_blank") {
		window.open(destinationURL);
	}
	else {
		location.href = destinationURL;
	}
}




/**
 * HomePage Action slider
 * 
 */


jQuery.fn.center = function () {
    this.css("position","absolute");
    var left = ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px";
    this.animate({left: left}, function(){
        $(this).css({position: 'static', margin: '35px auto'});
    });
    return this;
};

$(document).ready(function() {
	$('.info_band_box').click(function(){
		   
			$('#info_band_slide_box').show().center();
	
	});
});





/****
 * Homepage Showcase Mini Carousel
 * 
 *****/


$(function() {
    $(".showcase_mini_slide_container").jCarouselLite({
        btnNext: " .next",
        btnPrev: " .prev",
        visible: 5

    });
});




$(document).ready(function() {
$('.showcase_mini_slide_container img').tipsy({fade: true,gravity: 's'});


	$('.showcase_image').hover(
		function(){
			$(this).stop().animate({opacity: "0.5"});		
		},
		function(){
			$(this).stop().animate({opacity: "1"});
		
		});
	});




/****
 * Homepage info_buttons
 * 
 *****/



$(document).ready(function() {
	$('#sld').fadeIn();
	
	$('.info_band_box').click(function(){
		var btn = $(this).attr("rel");
		$('#homepage_intro').children('.slide').hide();
		$('#homepage_intro').children('#sld_'+btn).fadeIn();
		$('.info_band_box').removeClass('selected');
		$(this).addClass('selected');
	});
	
	$('.info_band_box').hover(
			function(){
				//$(this).addClass('current');
				$(this).children('h3').stop().animate({fontSize: "15px", top: "7px", color: "#177DD1"});
				$(this).children('p').stop().fadeTo("slow", 1);
			},
			function(){				
				$(this).children('h3').stop().animate({fontSize: "18px", top: "10px", color: "#555"});
				$(this).children('p').stop().fadeTo("slow", 0);
			});
	
});


/****
 * Showcase Hover Images - list page
 * 
 *****/


$(document).ready(function() {
	
	$('.thumb_img').hover(
		function(){
			$(this).stop().animate({opacity: '0.5' });
		},
		function(){
			$(this).stop().animate({opacity: '1'});			
	});
	
});


/****
 * Showcase Hover Images - list page
 * 
 *****/


$(document).ready(function() {
	
	$('.show_dates').click(function(e){
		e.preventDefault();
		
		var uc_date = $(this).attr("rel");		
		$('#ud_'+uc_date).toggle("slow");
	});
	
	$('.close').click(function(e){
		e.preventDefault();
		$(this).parent().slideUp("slow");
	
	});
	
});






// Wait for window load, then expand the Doclist Block
$(window).load(function() {
	$('.ajax-loader').hide();
	$('.filelist').slideDown();
	$(".expand").click(function(e){
		e.preventDefault();
		var pos = $(this).attr("id");
		pos = pos.substring(1);
		$('#d'+pos).toggle("slow");	
	});
});

$(window).load(function() {
	
	$('.ajax-loader-archive').hide();
    $("#archive_list").show();
	
    $(".archive_title").click(function(){
    	var pos = $(this).attr("id");
    	$('#arch'+pos).toggle('slow');
    });
    
    
    $(".desc_expand").click(function(e){
    	e.preventDefault();
    	var pos = $(this).attr("id");
    	//alert(pos);
    	$('#desc'+pos).toggle('slow');
    	
    	if ($(this).hasClass('open')) {
            $(this).removeClass('open');
        } else{
            $(this).addClass('open');;
        }

    	
    	
    });
    
});



///Quote fader
$(function() {
    $('#fader div:not(:first)').hide();
    $('#fader div').css('position', 'absolute');
   // $('#fader div').css('top', '0px');
 //   $('#fader div').css('left', '0px');
    $('#fader img').each(function() {
        var img = $(this);
        $('<img >').attr('src', $(this).attr('src')).load(function() {
            img.css('margin-left', -this.width / 2 + 'px');
        });
    });

    var pause = false;
    
    function fadeNext() {
        $('#fader div').first().fadeOut().appendTo($('#fader'));
        $('#fader div').first().fadeIn('fast');
    }

    function fadePrev() {
        $('#fader div').first().fadeOut();
        $('#fader div').last().prependTo($('#fader')).fadeIn();
    }

    $('#fader, #next').click(function() {
        fadeNext();
    });

    $('#prev').click(function() {
        fadePrev();
    });

    $('#fader, .button').hover(function() {
        pause = true;
    },function() {
        pause = false;
    });

    function doRotate() {
        if(!pause) {
            fadeNext();
        }    
    }
    
    var rotate = setInterval(doRotate, 7000);
});


$(window).load(function() {
    $('#ve-gaia-slider').nivoSlider({
    		effect: 'sliceDown',
    		animSpeed: 500, 
            pauseTime: 5000,
            captionOpacity: 1
    });
});

$(function() {
	$( ".online_discount" ).wrapInner('<div style=" background: url(/website/static/source/img/layout/online_discount.png) right -1px no-repeat" />');
});

$(function() {
	$('#licence-options :checkbox').click(function(){		
		$(this).parent().toggleClass('active');
	});
});




