/*
Bones Scripts File
Author: Eddie Machado

This file should contain any js scripts you want to add to the site.
Instead of calling it in the header or throwing it inside wp-head()
this file will be called automatically in the footer so as not to 
slow the page load.

*/

/* imgsizer (flexible images for fluid sites) */
var imgSizer={Config:{imgCache:[],spacer:"/path/to/your/spacer.gif"},collate:function(aScope){var isOldIE=(document.all&&!window.opera&&!window.XDomainRequest)?1:0;if(isOldIE&&document.getElementsByTagName){var c=imgSizer;var imgCache=c.Config.imgCache;var images=(aScope&&aScope.length)?aScope:document.getElementsByTagName("img");for(var i=0;i<images.length;i++){images[i].origWidth=images[i].offsetWidth;images[i].origHeight=images[i].offsetHeight;imgCache.push(images[i]);c.ieAlpha(images[i]);images[i].style.width="100%";}
if(imgCache.length){c.resize(function(){for(var i=0;i<imgCache.length;i++){var ratio=(imgCache[i].offsetWidth/imgCache[i].origWidth);imgCache[i].style.height=(imgCache[i].origHeight*ratio)+"px";}});}}},ieAlpha:function(img){var c=imgSizer;if(img.oldSrc){img.src=img.oldSrc;}
var src=img.src;img.style.width=img.offsetWidth+"px";img.style.height=img.offsetHeight+"px";img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')"
img.oldSrc=src;img.src=c.Config.spacer;},resize:function(func){var oldonresize=window.onresize;if(typeof window.onresize!='function'){window.onresize=func;}else{window.onresize=function(){if(oldonresize){oldonresize();}
func();}}}}


/* cycle plugin */
//$('#fade').cycle();
$('#fade').cycle({ 
    fx:     'fade', 
    timeout: 2000, 
    //after:   onAfter 
});

/********* Watermark **********/
$(function () {
		$("#searchform #s").watermark("Search for...", "watermark");

		//$("#s").watermark("Search for...");
		$("#searchsubmit").click(
			function () {
				this.form.submit();
			}

		);
	});


/********* BG STRETCHER MAIN.JS **********/
var slide_effect = 'fade';
var slide_direction = 'N';
var slide_mode = 'normal';

$(document).ready(function(){
	
	bindEvents();
		
});

function bindEvents(){
	
	// set options 
	if (slide_effect == 'fade' || slide_effect == 'none') {
		hideObject($('.slide-directions'), 'slide')
	} else {
		if (slide_effect == 'simpleSlide') {
			hideObject($('.mode-randome'), 'none');
			if (slide_mode == 'random') slide_mode = 'normal';	
			if ($('INPUT.direction').index($("INPUT#dir-"+slide_direction.toLowerCase()+"")) > 3) {
				slide_direction = 'N';
			}
			
		} else showObject($('.mode-randome'), 'none'); 

		showObject($('.slide-directions'), 'slide');
		
		if (slide_effect == 'superSlide') showObject($('.super-directions'), 'none'); 
			else hideObject($('.super-directions'), 'none');
	}
		
	if (slide_mode == 'random') {	
		$('#nav').html('');
		hideObject($('.nav-buttons'), 'fade');
	} else showObject($('.nav-buttons'), 'fade');
		
	$('#toggleAnimation').html("<span class='pause'>Pause Animation</span>");
	
	$("select#effect option[value='"+slide_effect+"']").attr("selected", true);
	$("INPUT#dir-"+slide_direction.toLowerCase()+"").attr('checked', 'checked');
	$("INPUT#"+slide_mode+"").attr('checked', 'checked');

	// Play / pause button
	$('#toggleAnimation').unbind('click');
	$('#toggleAnimation').click(function(){
		if ($(this).html() == "<span class='pause'>Pause Animation</span>"){
			$(this).html("<span class='play'>Resume Animation</span>");
			$('BODY').bgStretcher.pause();
		} else {
			$(this).html("<span class='pause'>Pause Animation</span>");
			$('BODY').bgStretcher.play();
		}
	});
	
	// Change Effect
	$('SELECT#effect').unbind('change');
	$('SELECT#effect').change(function(){
		if ($(this).val() == slide_effect) return true;
		slide_effect = $(this).val();
		$('BODY').bgStretcher.sliderDestroy();
		initBgStretcher();
		bindEvents();
		return true;
	});
	
	// Change Direction
	$('INPUT.direction').unbind('change');
	$('INPUT.direction').change(function(){	
		var new_slide_deriction = $('INPUT.direction:checked').attr('id').split('dir-');
		new_slide_deriction = new_slide_deriction[1].toUpperCase();

		if (new_slide_deriction == slide_direction)	return true;
		slide_direction = new_slide_deriction;

		$('BODY').bgStretcher.sliderDestroy();
		initBgStretcher();
		bindEvents();
		return true;
	});
	
	// Change Mode
	$('INPUT.mode').unbind('change');
	$('INPUT.mode').change(function(){	
		if ($(this).attr('id') == slide_mode) return true;
		slide_mode = $(this).attr('id');

		$('BODY').bgStretcher.sliderDestroy();
		initBgStretcher();
		bindEvents();
		return true;
	});
	
	return true;
}

function initBgStretcher(){

	$('BODY').bgStretcher({
		images: ['/wp-content/themes/bones/backgrounds/lewis_03.jpg', '/wp-content/themes/bones/backgrounds/motd_01.jpg', '/wp-content/themes/bones/backgrounds/motd_02.jpg', '/wp-content/themes/bones/backgrounds/piano.jpg', '/wp-content/themes/bones/backgrounds/samlarose_01.jpg'],
		imageWidth: 1024, 
		imageHeight: 768, 
		slideDirection: slide_direction,
		slideShowSpeed: 1000,
		transitionEffect: slide_effect,
		sequenceMode: slide_mode,
		buttonPrev: '#prev',
		buttonNext: '#next',
		pagination: '#nav',
		anchoring: 'left center',
		anchoringImg: 'left center'
	});
}

function hideObject(obj, hide_effect) {
	if (($.browser.msie) && (parseInt(jQuery.browser.version) == 6)) {
			obj.css({position: 'absolute', left: '-100000px'});
		} else {
			if (hide_effect == 'slider') obj.slideUp();
				else  if (hide_effect == 'fade') obj.fadeOut();
					else obj.hide();
		}
	return true;
}
function showObject(obj, hide_effect) {
	if (($.browser.msie) && (parseInt(jQuery.browser.version) == 6)) {
			obj.css({position: 'static', left: '0px'});
		} else {
			if (hide_effect == 'slider') obj.slideDown();
				else  if (hide_effect == 'fade') obj.fadeIn();
					else obj.show();
		}
	return true;
} 



// as the page loads, call these scripts
$(document).ready(function() {

	// add all your scripts here
	
	// show/hide content
	$('#container').show().before('<a href="#" id="toggleBg" class="button">Show/Hide</a>');
	$('a#toggleBg').click(function() {
		$('#container').slideToggle(1000);
		return false;		
	});
	
	//  Initialize Backgound Stretcher	   
		$('BODY').bgStretcher({
			images: ['/wp-content/themes/bones/backgrounds/lewis_03.jpg', '/wp-content/themes/bones/backgrounds/motd_01.jpg', '/wp-content/themes/bones/backgrounds/motd_02.jpg', '/wp-content/themes/bones/backgrounds/piano.jpg', '/wp-content/themes/bones/backgrounds/samlarose_01.jpg'],
			imageWidth: 1024, 
			imageHeight: 768, 
			slideDirection: 'N',
			slideShowSpeed: 1000,
			transitionEffect: 'fade',
			sequenceMode: 'normal',
			nextSlideDelay: 30000,
			buttonPrev: '#prev',
			buttonNext: '#next',
			pagination: '#nav',
			anchoring: 'left center',
			anchoringImg: 'left center'
		});

 
}); /* end of as page load scripts */
