var IMAGES = 'images/2010/'

function scrollToTop(offset){
	var offset = (offset) ? offset : 0;
	$('html, body').animate({scrollTop:offset}, 'slow'); 
        //usage w/ smoothscroll
//
//          //set the link
//          $('#top-link').topLink({
//            min: 400,
//            fadeSpeed: 500
//          });
//          //smoothscroll
//          $('#top-link').click(function(e) {
//            e.preventDefault();
//            $.scrollTo(0,300);
//          });

}
function toTop() {
        $().UItoTop({ easingType: 'easeOutQuart' });
}

// STRING FUNCTIONS
function checkString(path,arrayNumber) {
	var newPath = trim(path);
	if(arrayNumber == 1) {
		if (!validate(newPath)){
			return false;
		} else {
			return newPath;	
		}
	} else {
		if(newPath.length<1) return ""; else return newPath
	}
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 

    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function validate(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var address = newPath;
   if(reg.test(address) == true) {
      //alert('Invalid Email Address');
      return address;
   }
}
// END STRING FUNCTIONS


// Hero Slider functions
function slideShow(){
	
	slideshowSettings();
	
	// main hero arrows
//	$(".hero_arrow").each(function(){
//		// opacity level
//		var level = .9;
//		$(this).animate({opacity:level});
//		
//		$(this).hover(function(){
//			$(this).stop().animate({opacity:1});
//		}, function(){
//			$(this).stop().animate({opacity:level});
//		});
//		
//	});
		
	// setup button for opening and closing hero slider
	$("#close_slider").click(function(){
		if($('#hero_wrapper').height()>150) {
			squeezeSlider();
		} else {
			openSlider();
		}
	});
	
}

function loadSlideshow () {
	// The first iteration grabs the next increment of photos
	$.post("handlers/get_slideshow.php", {offset:SLIDESHOW_OFFSET, increment:SLIDESHOW_INCREMENT},  function(msg){
		
		$("#hero_slider").html(msg);
		
		if(parseInt(SLIDESHOW_INCREMENT)<100) {
			SLIDESHOW_INCREMENT= 100;
			setTimeout("loadSlideshow();", 1000);	
			
		} else {
			// PAGE_REFRESH global in history.functions.js
			if(PAGE=='home')
				openSlider(1500);
			else
				squeezeSlider();	
		}

		slideshowSettings();

	});
}

function slideshowSettings() {
	$('#hero_slider').after('<div id="nav">').cycle({ 
		fx:        'scrollHorz',
		//direction: 'right', // one of up|down|left|right  default=left
                rev: 0,
		timeout: 11000,
		speed: 	250,
		next:   '#next', 
		prev:   '#prev',
                pager:  '#nav',
                after: onAfter,
                before: onAfter,
                pauseOnPagerHover: 1,
                pagerAnchorBuilder: function(idx, slide) {
                    return '<a href="#" class="hero_buttons"><img class="circle_on" src="images/hero/circle_on.png" width="10" height="10" /><img class="circle_off" src="images/hero/circle_off.png" width="10" height="10" /></a>'; 
                }
	});
        
                function onAfter() {
                      $('.slider').css('width','100%');
                      $('#hero_graphic').css('width','100%');
                      $('#hero_slider').css('width','100%');
                      $('.hero_slider').css('width','100%');
                }
        
        
        
         $("#hero_slider").mouseover(function() {
            $('#hero_slider').cycle('pause');
          }).mouseout(function(){
            $('#hero_slider').cycle('resume');
          });

        
        
        $("#hero_slider").touchwipe({
            wipeLeft: function() {
                $("#hero_slider").cycle("next");
            },
            wipeRight: function() {
                $("#hero_slider").cycle("prev");
            }
        });
        
        $('.hero_buttons').click(function() {
          //$('#hero_graphic').removeAttr('width');
          $('.slider').css('width','100%');
          $('#hero_graphic').css('width','100%');
          $('#hero_slider').css('width','100%');
          $('.hero_slider').css('width','100%');
        });
}

// slider "PORTFOLIO" Functions
function squeezeSlider(){
	$('#hero_bar').attr('src', IMAGES+'view-bar.png');
	$('#hero_wrapper').animate({height:150});
	$('.hero_arrow').hide();
	$('.hero_controls img').animate({opacity:0}, 500);
}

function openSlider(timer){
	if(!timer)
		timer = 500;

	$('#hero_bar').attr('src', IMAGES+'hide-bar.png');
	$('#hero_wrapper').animate({height:540}, timer);
	$('.hero_arrow').fadeIn();
	$('.hero_controls img').animate({opacity:1}, 500);
}
// END slider "PORTFOLIO" Functions

//function init_map() 
//{
//        var map;
//        var stylez = [
//            {
//              featureType: "all",
//              elementType: "all",
//              stylers: [
//                { saturation: -100 } // <-- THIS
//              ]
//            }
//        ];
//
//        
//	var myOptions = {
//		zoom: 10,
//		//center: new google.maps.LatLng(34.155587, -118.8075),
//                center: new google.maps.LatLng(34.07996230865873, -118.50128173828125),
//                mapTypeControlOptions: {
//                     mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'tehgrayz']
//                }
//
//	}
//	
//	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//        var mapType = new google.maps.StyledMapType(stylez, { name:"Grayscale" }); 
//        map.mapTypes.set('tehgrayz', mapType);
//        map.setMapTypeId('tehgrayz');
//
//	setMarkers(map, locals);
//}

function init_map()
{
	var map;
	var lavisual = new google.maps.LatLng(34.07996230865873, -118.50128173828125);

	var stylez = [
	    {
	      featureType: "all",
	      elementType: "all",
	      stylers: [
	        { saturation: -100 }
	      ]
	    }
	];

	var mapOptions = {
	    zoom: 10,
	    center: lavisual,
	    mapTypeControlOptions: {
	         mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'tehgrayz']
	    }
	};

	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

	var mapType = new google.maps.StyledMapType(stylez, { name:"Grayscale" });    
	map.mapTypes.set('tehgrayz', mapType);
	map.setMapTypeId('tehgrayz');

	setMarkers(map, locals);
}

var locals = [
		["La Visual", 34.155587, -118.8075, 8]
];

function setMarkers(map, locations) 
{
	var image = new google.maps.MarkerImage("../images/icons/lavisual_map.png",
		new google.maps.Size(34, 30),
		new google.maps.Point(0,0),
		new google.maps.Point(0, 30));
	var shadow = new google.maps.MarkerImage("../images/icons/lavisual_map_shadow.png",
		new google.maps.Size(37, 32),
		new google.maps.Point(0,0),
		new google.maps.Point(0, 32));
	var shape = {
		coord: [1, 1, 1, 40, 38, 40, 38 , 1],
		type: "poly"
	};
	for (var i = 0; i < locations.length; i++) 
	{
		var local = locations[i];
		var myLatLng = new google.maps.LatLng(local[1], local[2]);
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: map,
			shadow: shadow,
			icon: image,
			shape: shape,
			title: local[0],
			zIndex: local[3]
		});
	};
	
	var infowindow=new google.maps.InfoWindow({content:'<form action="javascript:calcRoute(document.getElementById(\'address\').value);"><div class="mapper" style="height:70px; overflow:hidden;"><table style="height:20px; width:300px; font-size:14px; overflow:hidden; color:#00ADEE;"><tr><td>Address:</td><td><input type="text" id="address" name="address"/></td></tr><tr><td></td><td><input type="submit" style="color:#FFFFFF; background:#00ADEE;" value="Get Directions"/></td></tr></table></div></form>'});
	google.maps.event.addListener(marker, "click", function(){
		infowindow.open(map,marker);
		$(".mapper").parent('div').css('overflow', 'hidden');
		$(".mapper").parent().parent('div').css('overflow', 'hidden');
		
	});	
	
	//end post do
	
}

//for ipad
//function loadCanvasImg(src, container) {
//	 
//	var cnv = document.createElement("canvas"),
//		ctx = cnv.getContext("2d"),
//		image = new Image();
//	
//	image.src = src;
//	
//	image.onload = function() {
//		cnv.width = image.width;
//		cnv.height = image.height;
//		
//		ctx.drawImage(image,0,0);
//
//		var div = container;  
//		div.appendChild(cnv);				
//  	}
//
//}
//for ipad

// jquery preloader function. 
(function($) {
	

	var cache = [];
	$.preLoadImages = function(image_array, callback) {
		if(typeof image_array == 'object' && image_array.length){
			// we have an array
			var images = image_array;
		} else if(typeof image_array == 'string'){
			// we have a string.  make an array of the csv info
			var images = image_array.split(',');
		} else {
			// return fals if 
			return false;
		}
		
		var count = images.length;
		for (var i = count; i--;) {
			var cache_image = document.createElement('img');
			cache_image.src = images[i];
			cache.push(cache_image);
		}					
		
		if(typeof callback == 'function'){
			callback.call(this);
		}
							
	}
	
	
	
})(jQuery)

