/* if ((navigator.userAgent.indexOf('iPhone') !=-1) || (navigator.userAgent.indexOf('iPod') !=-1)) {
	var cssNode = document.createElement('link');
	cssNode.setAttribute('rel', 'stylesheet');
	cssNode.setAttribute('type', 'text/css');
	cssNode.setAttribute('href', 'themes/mediacontour/css/iphone.css');
	document.getElementsByTagName('head')[0].appendChild(cssNode);
}

if ((navigator.userAgent.indexOf('iPhone') !=-1) || (navigator.userAgent.indexOf('iPod') !=-1)) {
	document.location = "http://apple.com";
} */

var page = {
	ready: false,
	init : function() {
	
//	    jQuery.getFeed({
//	        url: '/blog/feed',
//	        success: function(feed) {
//	    		alert(feed);
//	        
//	            jQuery('#result').append('<h2>'
//	            + '<a href="'
//	            + feed.link
//	            + '">'
//	            + feed.title
//	            + '</a>'
//	            + '</h2>');
//	            
//	            var html = '';
//	            
//	            for(var i = 0; i < feed.items.length && i < 5; i++) {
//	            
//	                var item = feed.items[i];
//	                
//	                html += '<h3>'
//	                + '<a href="'
//	                + item.link
//	                + '">'
//	                + item.title
//	                + '</a>'
//	                + '</h3>';
//	                
//	                html += '<div class="updated">'
//	                + item.updated
//	                + '</div>';
//	                
//	                html += '<div>'
//	                + item.description
//	                + '</div>';
//	            }
//	            
//	            jQuery('#result').append(html);
//	        }    
//	    });

	
		if($('#right_sidebar').length > 0) {
			var Cases = CaseStudies.GetRandom(2);
			$(Cases).each(function() {
				$('<li>').html($('<a>').attr('href', this.link).html($('<img>').attr('src', this.image))).appendTo('#right_sidebar ul');
			});
		};
		if ($('#the_slide').length > 0) {
			$('#slide_in').animate({ marginLeft: "0px" }, 600, function() {
				$('#slide_in_buttons img[alt=Begin Tour]').click(function() {
					$('#slide_in').animate({ marginLeft: "945px" }, 600, function() {
						$('#start').animate({ marginLeft: "-945px"}, 600, function() {
							$('#promo_nav').fadeIn(600, function() {
								$('#slider').css({'visibility':'visible', 'margin-left':'945px'}).hide().fadeIn(function() {
									$(this).animate({ marginLeft: "0px" }, 1000, function() {
										page.slider.animate(0);
										page.ready = true;
									});
								});
							});
							$(this).remove();
						});
					});
					return false;
				});
			});
		}
		$('a[rel=pop]').attr('target', '_blank');
	},
	slider : {
		tabs: ['visibilitycontent', 'designcontent', 'usabilitycontent', 'processcontent', 'portfoliocontent'],
		animate: function(to) {
			to = (to > 4) ? 0 : (to < 0) ? 4 : to;
			var sliderLeft = 189 * to;
			var promoLeft = -945 * to;
			var currentLeft = (promoLeft / 945).toFixed(2);
			if (currentLeft == parseInt(currentLeft)) {
				var url = $('#'+page.slider.tabs[Math.abs(currentLeft)]).children('div').children('a').attr('href');
				var currentTab = page.slider.tabs[Math.abs(currentLeft)];
				$('#learn_more > a').attr('href', url);
				$('#promo_box').stop().animate({marginLeft: promoLeft}, 600);
				$('#slider').stop().animate({marginLeft: sliderLeft}, 600, function() {
					$('#' + currentTab).addClass('selected').children('div').children('.link').fadeIn(1000).attr('style', 'display: block');
				});
			}
			$(page.slider.tabs).each(function() {
				if (this != currentTab) {
					$('#' + this).removeClass('selected').children('div').children('.link').fadeOut();
				}
			});
		},
		load: function() {
			for(var i=0; i<page.slider.tabs.length; i++)
			{				
				$('#'+page.slider.tabs[i]).children('div').children('h2, p').click(function() {
					for(var i=0; i<page.slider.tabs.length; i++) {
						if(page.slider.tabs[i] == $(this).parent('div').parent('div').attr('id')) {
							page.slider.animate(i);
						}
					}
				});
				$('#'+page.slider.tabs[i]).hover(function() {
					if (page.ready) {
						$(this).animate({
							opacity: 1.0
						}, 1400, function(){
							for (var i = 0; i < page.slider.tabs.length; i++) {
								if (page.slider.tabs[i] == $(this).attr('id')) {
									page.slider.animate(i);
								}
							}
						});
					}
				}, function() {
					$(this).stop();
				});
			};
			$('#next a').click(function() {
				for (var i=0; i < $('#slider_content').children('div').length; i++) {
					if ($('#slider_content').children('div').eq(i).hasClass('selected')) {
						page.slider.animate(i + 1);
					}
				}
			});
			$('#previous a').click(function() {
				for (var i=0; i < $('#slider_content').children('div').length; i++) {
					if ($('#slider_content').children('div').eq(i).hasClass('selected')) {
						page.slider.animate(i - 1);
					}
				}
			});
		}
	},
	contact: {
		load: function() {
			function initialize() {
			  if (GBrowserIsCompatible()) {

				 // Create our "tiny" marker icon
			  	var tinyIcon = new GIcon();
			  	tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
			  	tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			  	tinyIcon.iconSize = new GSize(12, 20);
			  	tinyIcon.shadowSize = new GSize(22, 20);
			  	tinyIcon.iconAnchor = new GPoint(6, 20);
			  	tinyIcon.infoWindowAnchor = new GPoint(5, 1);
		
			  	// Set up our GMarkerOptions object literal
			  	markerOptions = { icon:tinyIcon };

			    var map = new GMap2(document.getElementById('map_canvas'));
			    var marker = new GMarker(new GLatLng(34.11434361586855, -118.24966102838516), markerOptions);
			    map.setCenter(new GLatLng(34.11434361586855, -118.24966102838516), 13);
			    map.addControl(new GSmallMapControl());
			    map.addOverlay(marker);
			    
			    // The following line makes the map Earth-enabled by adding the
				// "Earth" button to the map type control.  Note that you still
				// need to add a map type control (GMapTypeControl,
				// GMenuMapTypeControl, or GHierarchicalMapTypeControl) to the
				// the map (as is done below) for the "Earth" button and the rest
				// of the map type buttons to show up at all.
				// Also, be sure you are loading v=2.x of the Maps API and not
				// v=2, otherwise the G_SATELLITE_3D_MAP map type will not
				// work properly.
				map.addMapType(G_SATELLITE_3D_MAP);
				
				map.addControl(new GHierarchicalMapTypeControl());
			  }
			};
			initialize();
			window.onunload = function() {
				GUnload();
			};
			$('form').submit(function() {
				form.errors.clear();
				form.validate.empty($('#first_name, #last_name, #company, #email, #phone'));
				form.validate.email($('#email'));
				form.validate.phone($('#phone'));
				return form.isValid;
			});
		}
	},
	casestudies: {
		load: function() {
			$('li.next').click(function() {
				var next = (new Number($('#collateral_slide ul').css('margin-left').replace('px', '')) / -545) + 1;
				page.casestudies.animate(next);
			});
			$('li.previous').click(function() {
				var prev = (new Number($('#collateral_slide ul').css('margin-left').replace('px', '')) / -545) - 1;
				page.casestudies.animate(prev);
			});
		},
		animate : function(to) {
			var slideLength = $('#collateral_slide li').length;
			to = (to > slideLength - 1) ? 0 : (to < 0) ? slideLength - 1 : to;
			var sliderLeft = -545 * to;
			var currentLeft = (sliderLeft / 545).toFixed(2);
			if (currentLeft == parseInt(currentLeft)) {
				$('#collateral_slide ul').stop().animate({marginLeft: sliderLeft}, 600, function() {
					var alt = $(this).children('li').eq(to).children('img').attr('alt');
					$('#collateral h3').html(alt);
				});
			}
		}
	},
	leftSidebar : {
		load : function() {
			$('li.header').each(function() {
				if($(this).children('ul').children('li').length > 6) {
					$(this).children('.more').css('display', 'block');
				} else {
					$(this).children('ul').css('height', $(this).children('ul').children('li').length * 32.5);
					$(this).children('.more').css('display', 'none');
				}
			});
			$('.more').click(function() {
				if ($(this).hasClass('more')) {
					page.leftSidebar.showMore(this);
				} else {
					page.leftSidebar.showLess(this);
				}
			});
		},
		showMore : function(obj) {
			var height = $(obj).siblings('ul').children('li').length * 32.5;
			$(obj).removeClass('more').addClass('less').html('Less').siblings('ul').animate({ height: height + 'px' });
		},
		showLess : function(obj) {
			var height = 6 * 32.5;
			$(obj).removeClass('less').addClass('more').html('More').siblings('ul').animate({ height: height + 'px' });
		}
	},
	portfolio: {
		load: function() {
			$('.hover_trigger').hover(function() {
				$(this).css('z-index', 3);
				$(this).parent().css('z-index', 1);
				$(this).siblings('.hover_box').css({'visibility':'visible','display':'none'}).fadeIn();
			}, function() {
				$(this).css('z-index', 0);
				$(this).parent().css('z-index', 0);
				$(this).siblings('.hover_box').css('visibility', 'hidden');
			});
			if ($('.portfolio_slide').length > 6) {
				$('.portfolio_nav li.next, .portfolio_nav2 li.next').click(function() {
					var next = (new Number($('#portfolio_content').css('margin-left').replace('px', '')) / -725) + 1;
					page.portfolio.animate(next);
				});
				$('.portfolio_nav li.previous, .portfolio_nav2 li.previous').click(function() {
					var prev = (new Number($('#portfolio_content').css('margin-left').replace('px', '')) / -725) - 1;
					page.portfolio.animate(prev);
				});
			} else {
				$('.portfolio_nav li.next, .portfolio_nav2 li.next, .portfolio_nav li.previous, .portfolio_nav2 li.previous').hide();
			}
		},
		animate: function(to) {
			var slideLength = $('#portfolio_content ul.portfolio_slide').length;
			to = (to > slideLength - 1) ? 0 : (to < 0) ? slideLength - 1 : to;
			var sliderLeft = -725 * to;
			var currentLeft = (sliderLeft / 725).toFixed(2);
			if (currentLeft == parseInt(currentLeft)) {
				$('#portfolio_content').stop().animate({marginLeft: sliderLeft}, 600);
			}
		}
	}
};
