/**
 * 
 *
 * @author Bryan Dease
 * @copyright sodiumhalogen.com, 23 April, 2010
 * Main javascript file for Einstein CMS
 **/

jQuery(document).ready(function ($)
{
	$('#search_term').hint();
	
	if ($('#welcome_hold').length)
	{
		setTimeout(function()
		{
			$.fn.colorbox({inline:true, href:'#welcome_pop',initialWidth:960,transition:'fade',opacity:0.9});
		}, 2000);
	}
	
	$('.close-modal').live('click', function ()
    {
        jQuery.fn.colorbox.close();
    });

	// set up colorbox for selected elements
    $('a.modal').live('click', function ()
    {
        targetURL = $(this).attr('href');
        $.fn.colorbox(
        {
            href: targetURL,
            speed: '100',
            opacity: 0.6,
            innerWidth: '400',
            scrolling: false
        });
		return false;
	});
	
	$('a.modal_video').live('click', function ()
    {
        targetURL = $(this).attr('href');
        $.fn.colorbox(
        {
            href: targetURL,
            speed: '100',
            opacity: 0.6,
            innerWidth: '515',
            scrolling: false
        }, function()
		{
			/* OLD FLOWPLAYER CODE
			var modal_video_location = $('div#video').attr('title');
			$f("video", "http://sodiumhalogen.com/cdn/scripts/flowplayer-3.0.3.swf", {
		        plugins: {
		            controls: {
		                fullscreen: true,
		                backgroundColor: '#4C75AB',
		                backgroundGradient: [0.6,0.3,0,0,0],
		                progressColor: '#2B2F3A',
		                fontColor: '#ffffff',
		                timeColor: '#DFDE7D',
		                buttonColor: '#2B2F3A',
		                sliderColor: '#899597',
		                sliderGradient: 'none',
		                bufferColor: '#ffffff',
		                mute: false
		            }
		        }, clip: {
		            url: modal_video_location,
		            autoPlay: true
		        }
		    });
			*/
		});
		return false;
	});

	if (global.section == 'calendar')
	{
		$('td:has(div.content)').addClass('event');
	}
	
	if ($('div#slider').length)
	{
		$('#slide1').cycle({ 
		    fx:     'scrollRight', 
		    speed:  400, 
		    timeout: 6500, 
		    pager:  '#slide-nav',
		 	pause: true
		});
	}
	
	if ($('div#video_player').length)
	{
		$('div#video_block').prependTo('#content_main');
		var video_location = $('div#video_player').attr('title');
		flowplayer('video_player', {src: 'http://sodiumhalogen.com/cdn/scripts/flowplayer-3.0.3.swf', wmode: 'transparent'}, {
	        plugins: {
	            controls: {
	                fullscreen: true,
	                backgroundColor: '#4C75AB',
	                backgroundGradient: [0.6,0.3,0,0,0],
	                progressColor: '#2B2F3A',
	                fontColor: '#ffffff',
	                timeColor: '#DFDE7D',
	                buttonColor: '#2B2F3A',
	                sliderColor: '#899597',
	                sliderGradient: 'none',
	                bufferColor: '#ffffff',
	                mute: false
	            }
	        }, clip: {
				url: video_location,
	            autoPlay: true
	        }
	    });
	}
	
	if ($('#slideshow').length)
	{
		var onMouseOutOpacity = 0.67;
		$('#thumbs ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});

		// Initialize Advanced Galleriffic Gallery
		var gallery = $('#thumbs').galleriffic({
			delay:                     2500,
			numThumbs:                 6,
			preloadAhead:              10,
			enableBottomPager:         true,
			maxPagesToShow:            7,
			imageContainerSel:         '#slideshow',
			controlsContainerSel:      '#controls',
			captionContainerSel:       '#caption',
			loadingContainerSel:       '#loading',
			renderSSControls:          false,
			renderNavControls:         false,
			enableHistory:             false,
			autoStart:                 false,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
		});
	}
	
	if ($('#contact').length)
	{
		var loader = jQuery('<div id="loader"><img src="' + global.base + 'assets/_images/ajax-loader.gif" alt="loading..." /><\/div>')
			.css({position: 'absolute', bottom: '10px', left: '65px'})
			.appendTo('#contact')
			.hide();

		var v = jQuery('#contact').validate({
			showErrors: function(errorMap, errorList) {
					var validation = this;
					jQuery(errorList).each(function() {
						var error = this;
						validation.settings.highlight(error.element);
					})
			 
					for (var i = 0, elements = this.validElements(); elements[i]; i++) {
						validation.settings.unhighlight(elements[i], validation.settings.errorClass, validation.settings.validClass);
					}
				},
				highlight: function(element) {
					jQuery(element).css('border','1px solid #E84C0D');
					jQuery('#result').next('p').remove();
					jQuery('#result').html('<span>Please fill out all of the fields highlighted in orange</span>');
				},
				unhighlight: function(element) {
					jQuery(element).css('border','1px solid #CCCCCC');
				},
			rules: 
			{
				name: 'required',
				email: {
					required: true,
					email: true
				},
				subject: 'required',
				message: 'required'
			},
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: '#result',
					beforeSubmit: function()
					{
						loader.show();
					},
					success: function(data)
					{
						jQuery('#result').next('p').remove();
						loader.hide();
						jQuery(form).resetForm();
					}
				});
			}
		});
	}
	
});
