var alertTimerId = 0;
var activeId = 0;
var fLock = false;
var slideTime = 8000;
var bannerTime = 1500;
$(function(){
		
		/**
	     * Feature link hover
	     */	
		/*
		$('.featureLink').mouseover(
			function(e) {
			var name = $(e.target).attr('name');
			$('#feature0Info').addClass('featureInfo');
			$('#'+ name + 'Info').removeClass('featureInfo');
			},
			function(e) {
				var name = $(e.target).attr('name');
				$('#feature0Info').removeClass('featureInfo');
				$('#'+ name + 'Info').addClass('featureInfo');
				$('#s3').cycle({fx:    'fade', speed:  2500 });
			}
		);

		
		$('.featureLink').mouseover(function(e){
			$('.featureInfo').hide();
			var name = $(e.target).attr('name');
			$('#'+ name + 'Info').show();
		});
	 */
	/* setup fancybox links */
        $('.fancybox').fancybox();
        $('.fancybox_contact_window').fancybox({'type':'iframe', 'autoScale':true, 'width':280});
        
        $('.fancybox_contact_window').click(function(){
            $('.fancybox_contact_window').removeClass('active');
            $(this).addClass('active');
        });
        /* end setup fancybox links */
        
        //$(".corner").corner();
        applyCorner('.corner',5,false);
	function applyCorner(selector,px,top_only)
	{

		if (jQuery.browser.msie && (jQuery.browser.version < 8.9999))
		{
			var corner_settings = {
				  tl: {radius: px},
				  tr: {radius: px},
				  bl: {radius: px},
				  br: {radius: px},
				  antiAlias: true
			};

			var top_corner_settings = {
				tl: {radius: px},
				tr: {radius: px},
				bl: {radius: 0},
				br: {radius: 0},
				antiAlias: true
			};


			try
			{
				if(top_only)
					curvyCorners(top_corner_settings, selector);
				else
					curvyCorners(corner_settings, selector);

			}
			catch(e)
			{
				//alert(e);
			}
		}
		else
		{
			if(top_only)
				$(selector).corner("top "+px+"px");
			else
				$(selector).corner(px+"px");
		}
	};

        


        /* dropdown menu */
        $("#tools").hover(function(){

            $(this).find('ul.dropdown').show();
            //$(this).addClass("hover");
            //$('ul:first',this).css('visibility', 'visible');

        }, function(){

                $(this).find('ul.dropdown').hide();
            //$(this).removeClass("hover");
            //$('ul:first',this).css('visibility', 'hidden');

        });
    /* end dropdown menu */

	setTimeout( "startBanner()", bannerTime );
	
	
	startBanner = function(){
		$('#topBanner').animate({
		    opacity: 1,
		    display: 'block',
		    height: 'toggle',
		    easing: 'easeOutBounce'
		  }, 500, function() {
		    // Animation complete.
		  });
	},

		/**
		 * Feature link click
		 */	

		$('.featureLink').click(
				function(e) {
					resetTimer();
					if(fLock == false){
						fLock = true;
						var type = $(e.target).attr('type');
						var padding = type + "25";
						$('#activeArrow').animate({paddingLeft:padding}, 500, function(){fLock = false;});
						
						if(type != activeId){
							$('#screen'+activeId).fadeOut('fast', function(){$('#screen'+type).fadeIn('fast');});
							activeId = type;
						}
					}
					
				}
		); 

		$(".closeBtn").click(function () {
			$('#topBanner').toggle();
		});
	 
	
		/*$('#featureVideoLink').click(
				function(e) {
					if(fLock == false){
						fLock = true;
						var type = $(e.target).attr('type');
						var padding = type + "75";
						$('#activeArrow').animate({ paddingLeft:padding}, 800, function(){fLock = false;});
						
						if(type != activeId){
							$('#screen'+activeId).fadeOut('fast', function(){$('#screen'+type).fadeIn('fast');});
							activeId = type;	
						}
					}
					
				}
		);*/
		
		
		/**
		 * Timer for the product slideshow
		 */

		timer();
		

		/**
         * DropMenu script
         */
		
		// Drop menu product	
		//productVariable = [];
		//dropMenu($('#productDrop'), $('#dropMenuSolutions'), productVariable);
		blogVariable = [];
		dropMenu($('#blogDrop'), $('#dropMenuNews'), blogVariable);
		
		comVariable = [];
		dropMenu($('#comDrop'), $('#dropMenuCompany'), comVariable);
		
		/*
        visibleBlog = false;
        lockBlog = false;
        
		$('#blogDrop').hover(
				// Mouse Enter
				function () {
					if(visibleBlog == false && lockBlog == false){
						visibleBlog = true;
						lockBlog = true;
						$('#dropMenuBlog').slideDown("fast",function(){		
							lockBlog = false;		
						});
					}
				},
				// Mouse Leave
				function () {
						visibleBlog = false;
						lockBlog = true;
						$('#dropMenuBlog').slideUp("normal",function(){		
							lockBlog = false;		
						});
				}
			);		
		*/

		/**
		 * Smooth scrolling (additional)
		 */
		
		 $('a[href*=#]').click(function() {
			 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			 && location.hostname == this.hostname) {
			   var $target = $(this.hash);
			   $target = $target.length && $target
			   || $('[name=' + this.hash.slice(1) +']');
			   if ($target.length) {
			  var targetOffset = $target.offset().top;
			  $('html,body')
			  .animate({scrollTop: targetOffset}, 900);
			    return false;
			   }
			 }
		 });
		
		
		 /**
		  * Cornify script protected with konami code 
		  */
		 
		 var kkeys = [], konami = "38,38,40,40,37,39,37,39";
		 $(document).keydown(function(e) {
		   kkeys.push( e.keyCode );
		   if ( kkeys.toString().indexOf( konami ) >= 0 ){
		     $(document).unbind('keydown',arguments.callee);
		     $.getScript('http://www.cornify.com/js/cornify.js',function(){
		       cornify_add();
		       $(document).keydown(cornify_add);
		     });          
		   }
		 });		 
		 
	},
	
	/**
	 * Timer for the product slideshow 
	 */
	
	timer = function(){
		alertTimerId = setTimeout( "nextSlide()", slideTime );
	},
	
	nextSlide = function(){
		if(fLock == false){
			fLock = true;
			var type = parseInt(activeId)+1;
			
			if(type > 4 ){
				type = 0;
			}
			var padding = type + "25";
			$('#activeArrow').animate({paddingLeft:padding}, 500, function(){fLock = false;});
			
			if(type != activeId){
				$('#screen'+activeId).fadeOut('fast', function(){$('#screen'+type).fadeIn('fast');});
				activeId = type;	
			}
		}
		timer();
	},
	
	resetTimer = function(){
		clearTimeout ( alertTimerId );
		timer();
	},
	
	dropMenu = function(startElement, dropElement, variables){
		variables[1] = false; // visible
		variables[2] = false; // lock
		variables[3] = false; // immediateClose
        
		startElement.hover(
				// Mouse Enter
				function () {
					if(variables[1] == false && variables[2] == false && variables[3] == false){
						variables[1] = true;
						variables[2] = true;
						dropElement.slideDown("fast",function(){		
							variables[2] = false;
							if(variables[3] == true){
								variables[1] = false;
								variables[2] = true;
								dropElement.slideUp("normal",function(){		
									variables[2] = false;
									variables[3] = false;
								});							
							}
						});
					}
					if(variables[3] == true){
						variables[2] = true;
						dropElement.slideUp("normal",function(){		
							variables[2] = false;
							variables[3] = false;
						});						
					}
				},
				// Mouse Leave
				function () {
					if(variables[2] == false){
						variables[1] = false;
						variables[2] = true;
						dropElement.slideUp("normal",function(){		
							variables[2] = false;
						});
					} else {
						variables[3] = true;
					}
				}
			);
		
		// On click.. open or close
		startElement.click(function(){
			// check the lock
			if(variables[2] == false){
				if(variables[1] == true){
					// is visible, so close
					variables[1] = false;
					variables[2] = true;
					dropElement.slideUp("normal",function(){		
						variables[2] = false;
					});					
				} else {
					// is invisible, so drop
					variables[1] = true;
					variables[2] = true;
					dropElement.slideDown("fast",function(){		
						variables[2] = false;
					});						
				}
			}
		});
		
	}
);

