//Browser Sniffing
var fullVersion = navigator.appVersion;
var ie = fullVersion.match('MSIE');
var ie6 = fullVersion.match('MSIE 6.0');
var ie7 = fullVersion.match('MSIE 7.0');

/*-----------------------------------------------------------
	IE6 Message
-----------------------------------------------------------*/
var warning = '<div id="ie-warning">';
	warning +='<p>This browser is no longer fully supported, some elements may not display correctly.</p>';
	warning +='</div>';
	
if(ie6) {
	$('body').append(warning); //Appends body tag with message for IE 6 users
}	

/*--------------------------------------------------------------
	Navigation Drop Downs
--------------------------------------------------------------*/
	$('#nav ul li').hover(function(){
		$(this).children('div').stop(true,true).show(); //Shows drop downs on main nav
	}, function(){
		$(this).children('div').stop(true,true).hide(); //Hides drop downs on main nav
	});

/*-----------------------------------------------------------
	Product Navigation Sub Menus
-----------------------------------------------------------*/
$('.eml-nav li').hover(function(){
	$(this).children('.product-sub-links').stop(true, true).fadeIn('slow'); //Shows product page's subnav drop downs
}, function(){
	$(this).children('.product-sub-links').stop(true, true).fadeOut(); //Hides product page's subnav drop downs
});

/*--------------------------------------------------------------
	Front page banner animation
--------------------------------------------------------------*/
	$('#banners').cycle({
		 fx: 'fade', 
		 speed: 1000, 
		 timeout: 15000,
		 random: 1
	});

/*-----------------------------------------------------------
	DOM Ready Function
-----------------------------------------------------------*/

$(function(){ // DOM Ready

/*--------------------------------------------------------------
	Global
--------------------------------------------------------------*/
	
	//Sets the opacity level for the overlay div el
	$('.overlay').css({opacity: .50});
	
	//Print Button Function
	$('.print').click(function(){
		window.print();
	});
	
	//Sets Opacity for Modal Class
	/* $('.modal').animate({'opacity': .90}); */

/*--------------------------------------------------------------
	RSS Feed Script for front page
--------------------------------------------------------------*/
	 $('#news-items-container').rssReader({ // Adds rssReader plugin
	    url: '/xml/news.rss',
	    showDescription: 'hide',
	    rows: 5
	});

/*--------------------------------------------------------------
	Screenshot Zoom Functionality 
--------------------------------------------------------------*/
	var emlGallery = { //eMESA Live Gallery Namespace
		
		structure : '<div id="eml-image-large-container"><a title="Close" class="close-red close" href="#"></a></div>'
	};

	//Appends BODY with elements needed for lightbox effect
	
	$('body').append(emlGallery.structure);
	
	//On Click function for screenshot thumbnails
	
	$('.eml-screenshots-list li a, .eml-screenshots-list li img.zoom').click(function(){
		var imgSrc = $(this).parents('li').find('.eml-image-small').attr('src');
		var fullSrc = imgSrc.replace('Thumbnails', 'Full');
		var lrgImg = '<img class="eml-image-large" alt="" src="'+fullSrc+'" />';	
		
		$('#eml-image-large-container').append(lrgImg).fadeIn(250,function(){
		    $('.overlay').fadeIn(500);
		});
		
		return false;
	});
	
	
	function hideLargeScreen() {
	    $('#eml-image-large-container').hide('puff',{},250,function(){$(this).children('.eml-image-large').remove()});
	}
	
	function hideOverlay() {
	    $('.overlay').hide();
	}
	
	//On Click function for large screenshot "Close" button
	
	$('#eml-image-large-container .close').click(function(){
        hideOverlay();
		hideLargeScreen();
	});
	
	$('.overlay').click(function(){
	    hideLargeScreen();
	    hideOverlay();
	});
	
	var SlideShow = {
	    imageArr: null,
	    slide: $('#slideshow-img'),
	    slideTitle: $('#slide-title'),
	    
	    init: function() {
	        SlideShow._makeArray();
	        SlideShow._controls();
	        SlideShow._showSlideShow();
	        $('#total-slides').text(SlideShow.imageArr.length);
	    },
	    
	    _makeArray: function() {
	        //image object to store required data.
	        var imgObj = function() {
	            this.title = null;
	            this.src = null;
	        }
	        
	        //Pushes imgObj data into array
	        SlideShow.imageArr = [];
	        $('img.eml-image-small').each(function(){
	            var images = new imgObj();
	                images.title = $(this).attr('alt');
	            var path = $(this).attr('src');
	                images.src = path.replace('Thumbnails', 'Full');
	            SlideShow.imageArr.push(images);
	        });
	    },
	    
	    _controls: function() {
	        var index = 0;
	        $('#slideshow-previous').addClass('hidden');
	        function goTo(newIndex) {
	            if (newIndex >= 0 && newIndex < SlideShow.imageArr.length) {
        			index = newIndex;
        			SlideShow.slide.attr('src', SlideShow.imageArr[index].src);
        			SlideShow.slideTitle.text(SlideShow.imageArr[index].title);
        			$('#slideshow-next').removeClass('hidden').addClass('shown');
        		}//End if
        		
        		if (index > 0) {
        		    $('#slideshow-previous').addClass('shown');
        		}
        		else if (index < 1) {
        		    $('#slideshow-previous').removeClass('shown').addClass('hidden');
        		}
        		
        		if (index == (SlideShow.imageArr.length -1)) {
        		    $('#slideshow-next').removeClass('shown').addClass('hidden');
        		}
        		$('#current-slide').text(index + 1);
        		
	        }//End goTo()
	        
	        function step(i) {
	            goTo(index + i);
	        }//end step()
	        
	        function transition(index) {
	            var i = index;
	            SlideShow.slide.fadeOut(300, function(){
	                step(i);
	                $(this).fadeIn(300);
	            });
	        }//end transition()
	        
	        
	        $('#slideshow-previous').click(function(){
	            transition(-1);
	            return false;
	        });
	        
	        $('#slideshow-next').click(function(){
	            transition(1);
	            return false;
	        });
	    },
	    
	    _showSlideShow: function() {
	        $('#show-slideshow').click(function(){
	            $('#slideshow-container').fadeIn(500);
	            if(ie7) $('#nav').hide();
	            return false;
	        });
	        
	        $('#slideshow-close').click(function(){
	            if(ie7) $('#nav').show();
	        });
	    }
	};
	
	SlideShow.init();

/*--------------------------------------------------------------
	System & Server Requirements Popups
--------------------------------------------------------------*/
	//Displays System Req 
	$('.open-system-req').click(function(){
		$('#system-requirements').fadeIn('fast');
		
		var sysWidth = $('#system-requirements').width();
		var sysLeft = sysWidth-(sysWidth+(sysWidth/2));
		var sysHeight = $('#system-requirements').height();
		var sysTop = sysHeight-(sysHeight+(sysHeight/2));
		
		if(!ie6)
		{
			$('#system-requirements').css({'margin-left' : sysLeft + 'px', 'margin-top' : sysTop + 'px'});
		}
		return false;
	});

	//Displays Server Req
	$('.open-server-req').click(function(){
		$('#server-requirements').fadeIn('fast');
		
		var serWidth = $('#server-requirements').width();
		var serLeft = serWidth-(serWidth+(serWidth/2));
		var serHeight = $('#system-requirements').height();
		var serTop = serHeight-(serHeight+(serHeight/2));
	
		if(!ie6)
		{
			$('#server-requirements').css({'margin-left' : serLeft + 'px', 'margin-top' : serTop + 'px'});
		}
		return false;
	});


	//Makes close-red Button Fadeout parent el
	$('.close-red').click(function(){
		$(this).parent().fadeOut('fast');
		$('.overlay').hide();
		
		return false;
	});

/*--------------------------------------------------------------
	Contact me
--------------------------------------------------------------*/
//	$('.contact-me-link').click(function(){
//		$('#contact-me-container').fadeIn('fast');
//		$('#how-to-buy-popup').fadeOut('fast');
//		return false;
//	});
//	
//	var contactUrl = escape('https://forms.netsuite.com/app/site/crm/externalleadpage.nl?compid=661496&formid=9&h=2b1521646d641895d61f');
//	
//	$.ajax({
//		url: '/Utilities/proxy.php',
//		data: {Url: contactUrl},
//		success: function(response){
//			$('#contact-me-form').html(response).find('link').remove();
//		},
//		error: function(error){
//		
//		}
//	});
	
	
/*--------------------------------------------------------------
	How To Buy Popup
--------------------------------------------------------------*/
	$('#How-to-Buy').click(function(){
		$('#how-to-buy-popup').fadeIn('fast');
		
		return false;
	});
	
/*-----------------------------------------------------------
	Customer Success Links
-----------------------------------------------------------*/
	$('.single-video').click(function(){
		var url = $(this).attr('href');
		
		window.open(url, 'Video', 'width=640, height=360, toolbar=0, status=0, resizable=false');
		
		return false;
	});
	
	if(!ie6) { // Checks for IE6 first
		
		$('.customer-success-doc').click(function(){
			var url = $(this).attr('href');
		
			$.ajax({
				type: 'GET',
				url: url,
				dataType: 'html',
				success: function(story) {
					$('#story-container div').html(story);
					$('#story-container').fadeIn();
				}
			});
			
			return false;
		});
	}
	
	
	//Smooth Scroll Function
	smoothScroll();
	
}); // End DOM Ready

/*-----------------------------------------------------------
	Demo Video Links
-----------------------------------------------------------*/
	$('.demo-video').click(function(){
		var link = $(this);
		var demoTitle = link.attr('title');
		var demoFile = link.attr('href');
		
		function flashEmbed() {
			if(!ie6) {
				var movie = '/Videos/Demos/UltimateFLVPlayer.swf?videoPath='+demoFile+'&bufferSize=5&autoPlay=true&videoVolume=100&bgColor=0x000000&borderColor=0x151515&buttonsBGColor=0x0D0D0D&buttonElementsColor=0x999999&buttonElementsRollOverColor=0xffffff?imgPath=/theme/images/demo-preview.jpg';
			}
			else if (ie6) {
				var movie = demoFile;
			}
			
			var so = new SWFObject(movie, demoTitle, '853', '480', '9.0.0', '#000000');
			so.addParam("quality", "high");
			so.addParam("menu", "false");
			so.addParam("loop", "false");
			so.addParam("allowFullScreen", "true");
			so.write('demo-player-container');
			
			var closeButton = $('<div id="demo-player-header"><a title="Close" href="#" class="close-demo"></a></div>');
		
			$('#demo-player-container').prepend(closeButton);
			$('#demo-player-container').show();
		}
		
		if(!ie6) {
			flashEmbed();
		}
		
		return false;
	});
	
	$('.close-demo').live('click', function(){
		$(this).parent().parent().hide()
		$(this).parent().parent().html('');
		$('.overlay').fadeOut('normal');
		
		return false;
	});
	
/*-----------------------------------------------------------
    FAQ Smooth Scroll
-----------------------------------------------------------*/	
    
    function smoothScroll() {
        $('.smooth-scroll').click(function() {
            var href = $(this).attr('href');
            console.log(href);
            $('html, body').animate({
                scrollTop: $(href).offset().top
            }, 2000);
            
            return false;
        });
    }


					

