/**
 * --------------------------------------------------------------------
 * Custom Library
 * Version: 0,2, 04.03.2010
 * by Jesse Burcsik, jesseburcsik@gmail.com
 *  
 */

// var jQuery = jQuery.noConflict(); 


function resizeBookImages(){
	if (jQuery(".bookhead .book-img img").length) {
	
	jQuery(".bookhead .book-img img").each(
	
		function(){
		
			jQuery( this ).width('');
			jQuery( this ).removeAttr('style');
			jQuery( this ).removeAttr('height');
			jQuery( this ).attr('width','100');			
		}
	
	);
	}

}


function prettyPhotoPage(){
if (jQuery("a[rel^='prettyPhoto']").length ) {
   	jQuery("a[rel^='prettyPhoto']").prettyPhoto();
}
}

function initEcomm(){
	
// Check if you are on the firt eComm page, if so run the tooltip functionality
// More enhacements can be included in this function
	if (jQuery(".cart-product-img img").length) {
		jQuery('.cart-product-img img').tooltip({
    		bodyHandler: function() {
         var bqfp = '';
       	 bqfp = jQuery(this).parent().nextAll('.cart-product-title').html();
       	 bqfp = bqfp + '<br /><br />';
       	 bqfp = bqfp + jQuery(this).parent().nextAll('.cart-product-short-desc').html();
       	 return bqfp; 
    },
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 250
});

	}



// Check for eComm page : Product detailed page If yes then make toolTips
if (jQuery(".single-cart-product-img-zoom").length) {

jQuery('.single-cart-product-img-zoom').tooltip({
    bodyHandler: function() {
        var bqfp = '';
        bqfp = jQuery(this).prev('div').html();
        return bqfp; 
    },
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 250
});

}

}

// END ECOMM FUNCTION

// RANDOMLY DISPLAY A SET OF ELEMENTS FROM A LARGER SET
//
// amountToDisplay = are the number of elements to show ( EXAMPLE  "4" )
// elementsToParseThrough = the class of the set of elements that will will be chosen from ( EXAMPLE "class" )
// theContainer = The selector of the container for the randomly chosen eleemnts  ( EXAMPLE  ".selector" )

function showRandomElements(amountToDisplay, elementsToParseThrough, theContainer){
		var authorImgs = jQuery(elementsToParseThrough);
        jQuery(theContainer).empty();
        for(i=0 ;i<amountToDisplay;i++){
			var random = authorImgs.get(Math.floor(Math.random()*authorImgs.length));
			var theInstance = jQuery.inArray(random,authorImgs);
        	var authorImgs = authorImgs.not(authorImgs[theInstance]);
//        	alert(authorImgs.length);
        	jQuery(theContainer).append(random);
        }
	jQuery(theContainer).append("<div class='clear'></div>");
}


(function($) {
  $.fn.randomDisplayer = function(options) {
  
  	var opts = $.extend({}, $.fn.randomDisplayer.defaults, options);
  
    return this.each(function() {
		var theElements = jQuery( this ).children();
        jQuery( this ).empty();
        for(i=0 ;i<opts.amount;i++){
			var random = theElements.get(Math.floor(Math.random()*theElements.length));
			var theInstance = jQuery.inArray(random,theElements);
        	var theElements = theElements.not(theElements[theInstance]);
//        	alert(authorImgs.length);
        	jQuery( this ).append(random);
        }
//	jQuery( this ).append("<div class='clear'></div>");
    });
  }
  
   $.fn.randomDisplayer.defaults = {amount: 1}
  
})(jQuery);


// ADDS FUNCTOINALITY TO THE ALPHABET LIST ON AUTHORS PAGE
function authorslist(){
       jQuery('.expando').hide();
       jQuery('#expando-a').fadeIn();

	   jQuery('#alpha-list ul li a').click(function(){
	   		var theAlpha = jQuery( this ).html();
	   		var theContent = jQuery( this ).parent().attr('class');
	   	//	alert(theContent);s
	   		//alert(theAlpha);
	   		//var theList = "'#expando-" + theAlpha + "'";
	   		//alert(theList);
	   		jQuery('.expando').hide();
	   		jQuery('#expando-' + theContent).fadeIn();
	   		//alert(theCall);
	   		jQuery('#alpha-list span').html( theAlpha );
	   		});
	   		
	
}


function fixPngs(){
        jQuery(document).pngFix(); 
} 


function thankyou(){
	alert('thank you');
	return false;
}

// Add empty span to bottom of hover
	function addSpanTosideNav(){
		jQuery('#sidenav > a + span > a + span').each(
		
			function(){
				currentData = jQuery( this ).html();
				jQuery( this ).empty();
	 			jQuery( this ).append(currentData + '<div class="extra-subnav">&nbsp;</div>');
	 		}
		);
	}
	
// Choose which side call otus to show.

function chooseCallouts(){
 
    var pageTitle = jQuery('#col-two > h2').text();
    
	if ( pageTitle == 'Writing' | pageTitle == 'الكتابة' ){
		jQuery('#poster-three').hide();
		return false;
	}else if ( pageTitle == 'Reading' | pageTitle == 'القراءة' ){
		jQuery('#poster-two').hide();
		return false;
	}else{
		jQuery('#poster-two').hide();
		jQuery('#poster-three').hide();
		return false;
	}

}	
	
	
function slideSwitch() {
    var $active = jQuery('#slideshow div.active');

    if ( $active.length == 0 ) $active = jQuery('#slideshow div:last');

    var $next =  $active.next('div').length ? $active.next('div')
        : jQuery('#slideshow div:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 800, function() {
            $active.removeClass('active last-active');
        });
}

jQuery(function() {
    setInterval( "slideSwitch()", 5000 );
});

	
/*


// Make CSS style switcher functional

function styleSwitcher(){
jQuery("li#change-text a").toggle(
	function(){
			jQuery("link.mainstyles").attr("href","/rsrc/apes/css/styleslarger.css");
		},
		function(){
			jQuery("link.mainstyles").attr("href","/rsrc/apes/css/styles.css");
		}
	);
}

function toggleStyles(){
	jQuery("li#change-text a").click(function() {
		jQuery("link").attr("href",jQuery(this).attr('rel'));
        return false;
    });
    }


*/



// Remove the vert.css style sheet //
//function removeVertCSS(){
//	jQuery("link[href='/common/css/vert.css']").remove();
//	jQuery("link[href='/common/css/vertie7.css']").remove();
//	jQuery("link[src='/common/js/flymenu.js']").remove();
//} 

//Add spans to first level flyout navs to have IE6 supported rounded corners
function extendVertNav(){

	//	addSpanToLinks();
	addCSStoMenuactive();
	// addLiTosubnav();
	hoverParent();
	hoverActive();
	hoverLi(); // For IE 6
	hasKids();
	
	
	function hoverLi(){
	
		jQuery('#vertnav .menu, #vertnav .menuactive').prev().hover(
			function(){
				jQuery( this ).addClass('top-li-hovered');
			},
			function(){
				jQuery( this ).removeClass('top-li-hovered');
			});

	
	}
	
	
	
	function addCSStoMenuactive(){
		jQuery('#vertnav .menuactive').parent().addClass('menuactiveparent');
	}
	
	// Change the styles of the menu active LI on hover
	
	function hoverActive(){
		jQuery('#vertnav .menuactive').hover(
			function(){
				jQuery( this ).parent().addClass('active-hovered');
			},
			function(){
				jQuery( this ).parent().removeClass('active-hovered');
			}
		);
		
		
		jQuery('#vertnav .menuactive').parent().hover(
			function(){
				jQuery( this ).addClass('active-hovered');
			},
			function(){
				jQuery( this ).removeClass('active-hovered');
			}
		);
		
	
	}
	
	// double check that the menu item is fixe and NOT HOVERED
	
	// make the parent :sudo hovered as its child is being hovered
	function hoverParent(){
		jQuery('#vertnav .menu + ul').hover(
			function(){
				jQuery( this ).prev().addClass('is-being-hovered');
			},
			function(){
				jQuery( this ).prev().removeClass('is-being-hovered');
			}
		);
	}


	// Add an smpty span inside the top level links (to create rounded corners that work in IE)
	function addLiTosubnav(){
		jQuery('#vertnav ul').each(
		
			function(){
				currentData = jQuery( this ).html();
				jQuery( this ).empty();
	 			jQuery( this ).append(currentData + '<li class="bottom-subnav">&nbsp;</li>');
	 		}
		);
	}
	
	
	function hasKids(){
		
		jQuery('.menu + ul, .menuactive + ul').each(
			function(){
				$( this ).prev().addClass('haskids');
			}
		);
	
	}



}

