/* verify Crossborders namespace exists */
if (typeof CB === 'undefined') {
	CB = {};
}

CB = {
	
	init : function () {
	}, //end init
	
	// Home
	initHome : function () {
		
		$('#dl #slider .panel:first').fadeIn(1000, function() {
	        $('#dl #slider').cycle({
				fx: 'fade',
				speed: 1000,
				timeout: 6000,
				prev: '#prev',
				next: '#next'
			});
		});
		
		// Pause on hover
		$('#dl #slider').hover(function(e) {
			$('#dl #slider').cycle('toggle');
		});
		
	}, //end home
	
	// Work
	initWork : function () {
		//Back to top
		$(window).scroll(function () {
		    if ( $(window).width() > 1024 || !isiOS ) {
		        if ($(window).scrollTop() > 400) {
		            $('a#back-to-top').fadeIn('fast');
		        } else {
		            $('a#back-to-top').fadeOut('fast');
		        }
		    }
		});
		
		$(window).scroll();
		
		// Smooth scroll
		$('a#back-to-top').smoothScroll();
		
		//Work Links Hover
		$('.work-list li').hoverIntent(
			function() {
				$('span.label', this).slideDown(200);
			},
			function() {
				$('span.label', this).slideUp(200);
			}
		);
		
		// Work Filter
		/*$('.work #content').mobilyselect({
			collection: 'all',
			animation: 'fade',
			duration: 200,
			listClass: 'work-list',
			btnsClass: 'filters',
			btnActiveClass: 'active',
			elements: 'li',
			onChange: function(){},
			onComplete: function(){}
		});*/
		
		// Fade in images
		$("#gallery").preloader();
		
	}, //end work
	
	// Connect
	initConnect : function () {
		//Back to top
		$(window).scroll(function () {
		    if ( $(window).width() > 1024 || !isiOS ) {
		        if ($(window).scrollTop() > 400) {
		            $('a#back-to-top').fadeIn('fast');
		        } else {
		            $('a#back-to-top').fadeOut('fast');
		        }
		    }
		});
		
		$(window).scroll();
		
		// Smooth scroll baby
		$('.subnav a, a#back-to-top').smoothScroll();
		
		// Validate form
		//$("#freeform").validate();
        
		// Team scroll
		function slideme(slideNode) {
	    // Scroll vars
	    var currentPosition = 0;
	    var slideWidth = 615;
	    var slides = $('.slide', slideNode);
	    var numberOfSlides = slides.length;
	
	    // Remove scrollbar in JS
	    $('.slide-wrapper', slideNode).css('overflow', 'hidden');
	
	    // manageControls: Hides and shows controls depending on currentPosition
	    function manageControls(position, leftControl, rightControl){
	        // Hide left arrow if position is first slide
	        if(position===0){ leftControl.hide(); }
	        else{ leftControl.show(); }
	        // Hide right arrow if position is last slide
	        if(position==numberOfSlides-1){ rightControl.hide(); }
	        else{ rightControl.show(); }
	    }
	    
	    // Wrap all .slides with #slideInner div
	
	    var slideOuter = slides.wrapAll('<div></div>')
	    // Float left to display horizontally, readjust .slides width
	    .css({
	        'float' : 'left',
	        'width' : slideWidth
	    });
	
	    var slideInner = slideOuter.parent();
	    slideInner.css('width', slideWidth * numberOfSlides);
	
	    // Insert left and right arrow controls in the DOM
	    var lc = $('<span class="control" id="leftControl">Left</span>');
	    $(slideNode).append(lc);
	    lc.bind('click', function() {
	        currentPosition -= 1;
	        manageControls(currentPosition, lc, rc);
	        // Move slideInner using margin-left
	        slideInner.animate({
	            'marginLeft' : slideWidth*(-currentPosition)
	        });    
	    });
	    var rc = $('<span class="control" id="rightControl">Right</span>');
	    $(slideNode).append(rc);
	    rc.bind('click', function() {
	        currentPosition += 1;
	        manageControls(currentPosition, lc, rc);
	        // Move slideInner using margin-left
	        slideInner.animate({
	            'marginLeft' : slideWidth*(-currentPosition)
	        });
	    });  
	
	    // Hide left arrow control on first load
	    manageControls(currentPosition, lc, rc);
	}
	slideme($("#teamSlide"));
	slideme($("#boardSlide"));
	},
	
	// 404
	init404 : function () {
		// Generate random image
		var images = ['gif-1.gif', 'gif-2.gif', 'gif-3.gif', 'gif-4.gif'];
		$('.b404').css({'background-image': 'url(http://dev.crossborders.tv/assets/gifs/' + images[Math.floor(Math.random() * images.length)] + ')'});
	},
	
	// Google maps
	initMap : function () {
	    var coordInfoWindow;
	    var myLatlng = new google.maps.LatLng(40.745726,-73.995257);
	    var myOptions = {
	    	zoom: 16,
	      	center: myLatlng,
	      	mapTypeId: google.maps.MapTypeId.ROADMAP
	    };

	    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
		coordInfoWindow = new google.maps.InfoWindow();
		coordInfoWindow.content = "Crossborder"
		coordInfoWindow.setContent(coordInfoWindow.content);
		coordInfoWindow.setPosition(myLatlng);
		
		var marker = new google.maps.Marker({
		      position: myLatlng
		  });

		// To add the marker to the map, call setMap();
		marker.setMap(map);
		coordInfoWindow.open(map,marker);
			
	}//end map
	
};
