// Leave $ namespace alone
jQuery.noConflict();

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// shorthand for document.ready
jQuery(function($) { // $ == jQuery in this block
	if(typeof(optishot_images_domain_prefix) == 'undefined')
		var optishot_images_domain_prefix = '';
	
	// Used for image loading/preloading
	$.images = {
		path: optishot_images_domain_prefix + '/images/', // path to images
		folders: {
                    bodyHome: "."
		}
	};
	$.each( $.images.folders, function(section) {
		if ( $('body').is('.' + section) ) {
			$.images.folders.current = section;
			return false;
		}
	});


	$('body')
		.addClass('jsEnabled') // let css know js is enabled
		.check_images_enabled(); // see if images are enabled		


	$('#primary')
		.find('div.section').add_corners('section').end() 			// add corners to div.section
		.find('div.box').add_corners('box').end()					// add corners to div.box
		.find('.buttonSmall, .buttonLarge').make_buttons().end() 	// create the buttons
		.find('div.buttonSmallDisabled').make_buttons_disabled().end()	// create the disabled buttons
		.find('hr.divider').make_dividers().end();					// setup dividers
		


		
$('#secondary')
		.find('.buttonSmall, .buttonLarge').make_buttons().end() 	// create the buttons
		



	$('#nav li ul li ul li') // Link up third level li
		.css('cursor', 'pointer')
		.bind('click', function(event) { //cookie clear code here for clearing tab cookie
								window.location = $('a:first', this).attr('href'); });
	
	if ( $.browser.safari && parseInt( $.browser.version ) <= 418 ) {
		// Safari 2.0.4 needs help positioning the bottom corners on #mainContent
		// due to the buttons size changing after Safari has rended the corners
		// also occurs if images don't have width/height attributes
		$('#bottom').css('bottom', '0px');
		$(window).load(function() {
			$('#bottom').css('bottom', '-6px');
		});
	}
});

// $ != jQuery from here on
jQuery.fn.extend({
	
	// Creates the scalable background image
	scalable_bg: function(options) {
		options = jQuery.extend({
			id: 'scalable_bg',
			img: '',
			exclude: jQuery.browser.msie && parseInt( jQuery.browser.version ) == 6
		}, options || {});
	
		if ( !options.exclude ) {	
			jQuery('<div id="' + options.id + '"><img src="' + options.img + '" /></div>')
				.appendTo(this)
				.bind('mousedown', function() { return false; });
	
			jQuery(window).bind('load resize', function() {
				var prop = jQuery.browser.msie ? 'height' : 'min-height';
				var $img = jQuery('#'+options.id);
				var height = jQuery('#container').height();
				$img
					.css('min-width', 986)
					.css( prop, 1 )
					.css( prop, height );
			});
		}
	
		return this;
	},
	
	// creates necessary markup to create the disabled buttons
	make_buttons_disabled: function () {
		
			return this.each(function() {
			var $this = jQuery(this);
			var className = $this.is('.buttonSmallDisabled') ? 'buttonSmall' : 'buttonLarge';
	
			$this
				.wrap('<span class="' + className + '"><span class="first-child"><span /></span></span>')
				.parent().parent() // .first-child
					.add_corners('button');
			});
	},
	
	
	// creates necessary markup to create the buttons
	make_buttons: function() {
		jQuery.preload( 'bg_button_corners_over.png', 'bg_button_large_over.jpg', 'bg_button_small_over.jpg' );
		
		return this.each(function() {
			var $this = jQuery(this);
			var className = $this.is('.buttonSmall') ? 'buttonSmall' : 'buttonLarge';
	
			$this
				.wrap('<span class="' + className + '"><span class="first-child"><span /></span></span>')
				.parent().parent() // .first-child
					.add_corners('button');
			
			$this
				.bind('click', function(event) { event.stopPropagation(); }) // prevent the event from propagating to the span
				.parent().parent().parent().bind('click', function(event) {
				var ret = $this.triggerHandler('click');
				return ret !== false && $this[0].href && (window.location = $this[0].href) || ret;
			});
		
			// Fix buttons in IE6
			if ( jQuery.browser.msie && parseInt( jQuery.browser.version ) == 6 )
				$this
					.parent().parent().parent() // .butonLarge or .buttonSmall
						.bind('mouseenter mouseleave', function(event) { jQuery(this).toggleClass('hover'); });
		});
	},
	
	make_dividers: function() {
		return this.each(function() {
			jQuery(this)
				.wrap('<div class="divider" />').parent()
					.addClass( this.className )
					.prepend('<span class="left" /><span class="right" />');
		});
	},
	
	// adds the necessary markup to add corners to elements
	add_corners: function(prefix) {
		return this.prepend('<span class="' + prefix + 'TopLeft" /><span class="' + prefix + 'TopRight" /><span class="' + prefix + 'BottomLeft" /><span class="' + prefix + 'BottomRight" />');
	},	
	
	// adds a class of imagesEnabled to the body if images are enbaled
	check_images_enabled: function() {
		jQuery('<img />')
			.bind('load', function() {
				jQuery('body').addClass('imagesEnabled');
				jQuery(this).remove();
			})
			.attr('src',  jQuery.images.path + 'noop.gif?avoidCache=' + Math.random());
		
		return this;
	},
	
	// reverses the array, helpful for inserting a collection of elements
	reverse: function() {
		return this.pushStack(Array.prototype.reverse.call(this, this));
	}
	

});

jQuery.preload = function() {
	var images = jQuery.makeArray(arguments);
	jQuery(window).bind('load', function() {
		jQuery.each( images, function(index, image) {
			var path = jQuery.images.path + '/' + image;
			var img = new Image();
			img.src = path;
		});
	});
};

function RunSifr() { 
	var isOSXFF = checkOSXFF();
	if (typeof(sIFR) != undefined && !isOSXFF && checkValidSifrPage()) {
		var helvetica_neue = {
			src: '/assets/f/helvetica_neue.swf'	
		};
		sIFR.activate(helvetica_neue);
		sIFR.replace (helvetica_neue, {
			selector: '.bodyWhat #primary h1',
			css: [ '.sIFR-root {color: #FFFFFF;}' ],
			wmode: 'transparent',	
			filters: {
				DropShadow: {
					knockout: false,
					distance: 2,
					color: '#96c835',
					strength: 2
					}
				}
		});
			
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyFind #primary h1',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyMyoptishot #primary h1',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodySetup #primary h1',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyAbout #primary h1',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyGeneric #primary h1',
			wmode: 'transparent'
		});
	
		// H2 styles -->
		
		sIFR.replace (helvetica_neue, {
			selector: '.bodyWhat #primary h2',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyFind #primary h2',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyMyoptishot #primary h2',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodySetup #primary h2',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyAbout #primary h2',
			css: [ '.sIFR-root {color: #96c835;}' ],
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: '.bodyGeneric #primary h2',
			wmode: 'transparent'
		});
		
		sIFR.replace(helvetica_neue, { 
			selector: 'h2',
			wmode: 'transparent'
		});
		
		sIFR.replace (helvetica_neue, {
			selector: '.bodyWhat div.watchVideo span',
			css: [ '.sIFR-root {color: #96c835; font-size: 17px;}' ],
			wmode: 'transparent'
		});
				
		sIFR.replace (helvetica_neue, {
		selector: '.boxTitle',
		css: [ '.sIFR-root {color: #96c835; font-size: 15px; text-align: center;}' ],
		wmode: 'transparent'
	});		
		
	}//end if
	return false;
}


// is this page in a section where sIFR should be displayed (not the Shop/Store section)?
function checkValidSifrPage() {
	var isValidSection = true;
	
	if(jQuery('BODY').hasClass('bodyShop'))
		isValidSection = false;
try { console.log('is valid section? ' + (isValidSection ? 'true' : 'false')); } catch(err) {}
	
	return isValidSection;
}

//used to determine if user is running mac os and safari
function checkOSXFF()
{
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
	return false;
}





			
//(C) Stephen Daly
// www.stephendaly.org
// Date: 11/3/2008

// Checks if the browsers is IE or another.
// document.all will return true or false depending if its IE
// If its not IE then it adds the mouse event
if (!document.all)
document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getPosition;
/*
// possibly replace the above code with the following: 
if (document.addEventListener){
	document.addEventListener('mousemove', getPosition, false); 
} else if (document.attachEvent){
	document.attachEvent('mousemove', getPosition);
}
*/

var X = 0
var Y = 0
function getPosition(args) 
{
  // Gets IE browser position
  if (document.all) 
  {
	  try {
	X = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
	Y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
	  } catch(err) { ; }
  }
  
  // Gets position for other browsers
  else 
  {  
    X = args.pageX 
    Y = args.pageY 
  }  
}


