// JavaScript Document
var rotation_interval = '';

//ready function fills this array for load function
var preload_images = new Array();

$(document).ready(function(){
	
	$('.swap').each(function(i){
		var src = this.src;
		
		if(src.indexOf('_on.')){
		   //
		   src = src.replace('_on.','_off.');	
		}
		else{
		   src = src.replace('_off.','_on.');
		}
							 
		preload_images[i] = this.src;
	});
						   
	$('.swap').mouseover(function(){
		this.src = this.src.replace('_off.','_on.');
	});
	
	$('.swap').mouseout(function(){
		
		//if class is selected we don't want to deselect it
		if(!$(this).hasClass("selected")){
		   this.src = this.src.replace('_on.','_off.');
		}
	});
	
	$('.focusClear').focus(function(){
        
	  //assign a local variable so our function 
	  //will have its own unique original value per item
	  var originalValue = this.value;
	  
	  //first time we always clear
	  this.value = '';
	  
	  //unbind this jquery inline function
	  //because we want to set original value once
	  $(this).unbind('focus');
	  
	  //add an unique onblur per item
	  this.onblur = function(){
		if(this.value == ''){
		   this.value = originalValue;	
		}
		
		//attach the new unique onfocus per item
		this.onfocus = function(){
			 if(originalValue == this.value){
				this.value = '';	
			 }
		  }
	  }
	});

    $('.expand-close').click(function(e){
       var hash = this.hash;

       var status = $(hash).css('display');

       $(hash).toggle();

       if(status != 'none'){
           $(this).text('more');
           scroll(0,0);
       }
       else{
           $(this).text('hide');
       }

       e.preventDefault();
    });

});

$(window).load(function(){
	for(var i=0;i<preload_images.length;i++){
	   var image = new Image();
	   image.src = preload_images[i];	
	}

    image = new Image();
    image.src = '/sites/all/themes/custom/johnstott/images/flash2.jpg';
    image.src = '/sites/all/themes/custom/johnstott/images/flash3.jpg';

    var hash = window.location.hash;

    //code to automatically open container if #hash is present in url
    if(hash != ''){
        //we have a hash
        $('.expand-close').each(function(e){

           //set all expand links to a value of read more
           $(this).text('more');

           //hide description, container div is the href
           $(this.href).css('display','none');

           //if the hash matches the href + -hide, we want this link to say hide,
           //because we will open the corresponding container
           if(this.href.indexOf(hash + '-hide') != -1 ){
               $(this).text('hide');
           }
        });

        //open container with associated hash from url
        $(hash + '-hide').css('display','block');

    }

    rotation_interval = setInterval("rotateImage()",6500);

    $('#flash-button-1').click(function(e){
        $("#flash img:first").each(function(){
           this.src = '/sites/all/themes/custom/johnstott/images/flash.jpg';
           changeDonationLink();
           clearInterval(rotation_interval);
           rotation_interval = setInterval("rotateImage()",6500);
        });

        e.preventDefault();
    });

    $('#flash-button-2').click(function(e){
        $("#flash img:first").each(function(){
           this.src = '/sites/all/themes/custom/johnstott/images/flash2.jpg';
           changeDonationLink();
           clearInterval(rotation_interval);
           rotation_interval = setInterval("rotateImage()",6500);
        });

        e.preventDefault();
    });

    $('#flash-button-3').click(function(e){
        $("#flash img:first").each(function(){
           this.src = '/sites/all/themes/custom/johnstott/images/flash3.jpg';
           changeDonationLink();
           clearInterval(rotation_interval);
           rotation_interval = setInterval("rotateImage()",6500);
        });

        e.preventDefault();
    });


});

function rotateImage(){
    $("#flash img:first").each(function(){
       var src = this.src;

       if(src.indexOf('flash.jpg') != -1){
           this.src = this.src.replace('flash.jpg','flash2.jpg');
           changeDonationLink();
       }

       if(src.indexOf('flash2.jpg') != -1){
           this.src = this.src.replace('flash2.jpg','flash3.jpg');
           changeDonationLink();
       }

       if(src.indexOf('flash3.jpg') != -1){
           this.src = this.src.replace('flash3.jpg','flash.jpg');
           changeDonationLink();
       }
    });
}

function changeDonationLink(){
    $("#give-banner-button").each(function(){

       //more effiecient
       var src = document.getElementById('flash').getElementsByTagName('img')[0].src;

       if(src.indexOf('flash.jpg') != -1){
           this.href = 'http://www.johnstottministries.org/donate/?q=category/project-region/latin-america';
       }

       if(src.indexOf('flash2.jpg') != -1){
           this.href = 'http://www.johnstottministries.org/donate/?q=category/project-region/asia';
       }

       if(src.indexOf('flash3.jpg') != -1){
           this.href = 'http://www.johnstottministries.org/donate/?q=category/project-region/middle-east';
       }

       return false;

    });
}