var global = {
	vars : {
		lastItemId : [],
		scrollable : null,
		rows : null
	},
	init : function() {
		global.scrollable();
		global.key_navigation();

		// start
		global.cat();
		global.order();
		
		//news
		global.news_sub_order();
		global.news_sub_cat();		
		
		global.sort_people();
		
		global.news.hover();
		global.news.expand();
		global.contact.show_map();
		global.replace_round_images();
		global.video.open();
		global.video.close();
		swfobject.embedSWF("/wp-content/themes/gw/inc/swf/clock.swf", "clock", "443", "204", "8.0.0", "expressInstall.swf");
		
		
		if ($('.news-start').length > 0) {
			$('.item').click(function() {
				global.scroll.to(500, 600);
			});
		}
	},
	scrollable : function() {
		if ($('div.scrollable').length > 0) {
			var api = 	$("div.scrollable").scrollable({
							size	  : 1,
							speed	  : 800,
							next	  : '#slider-nav a.next', 
							prev	  : '#slider-nav a.prev',
							easing	  : 'custom',
							//disabledClass : 'disabled',
							api : true,
							clickable : false
						});
			global.vars.scrollable = api;
			$.easing.custom = function (x, t, b, c, d) { 
				var s = 1.70158;  
				if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
				return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
			}
			api.onSeek(function() {
				$('.current_page').html(this.getIndex() + 1);	
				global.vars.rows = api.getVisibleItems().children('.row').length;
				//console.log( api.getItems().length );
				//console.log(api.getVisibleItems());	
			});
			$('.total_page').html(api.getSize());
			global.vars.rows = api.getVisibleItems().children('.row').length;
			
			/*
			var rows;
			$(api.getItems()).each(function(i){
				rows = $(this).find('.item').length;
				//	console.log('1-',row);
			});
			*/
			//console.log(rows);
			
			//$.each(api.getItems(),function(index, item) {
				//console.log( $(item).find('.item').length );
			//});
			
		}
	},
	scroll : {
		fix : function() {
			if (jQuery.browser.safari == true || jQuery.browser.opera == true || jQuery.browser.mozilla == true)
				document.write('<style type="text/css">body {overflow-y: scroll;}</style>');
		},
		get : function() {
			var x = 0, y = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				// Netscape
				x = window.pageXOffset;
				y = window.pageYOffset;
			} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				// DOM
				x = document.body.scrollLeft;
				y = document.body.scrollTop;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				// IE6 standards compliant mode
				x = document.documentElement.scrollLeft;
				y = document.documentElement.scrollTop;
			}
			return [x, y];
		},	
		set : function(array) {
			window.scrollTo(array[0], array[1]);
		},
		to : function(value, speed) {
			$('html, body').animate({scrollTop:value}, speed);
		}
	},
	video : {
		open : function() {
			var url = $('#play-video').attr('rel');
			$('#play-video').click(function() {
				$('#headline').fadeOut('slow', function() {
					$('#header').slideUp(1000, function() {
						$('#nav').css({'position': 'absolute'});
						$('#headline').hide();
						$('#header').css({'background' : '#000'});
						$('#header').slideDown(1000, function() {
							var flashvars = {};
							var params = {
								allowfullscreen : true,
								wmode : 'transparent'				
							};
							//swfobject.embedSWF("http://vimeo.com/moogaloop.swf?clip_id=4610973&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1", "video", "1188", "512", "8.0.0", "expressInstall.swf", flashvars, params);
							swfobject.embedSWF(url, "video", "1188", "512", "8.0.0", "expressInstall.swf", flashvars, params);
							$('#video').fadeIn();
							$('#close-btn').fadeIn('slow');
						});
					});
					
				});
				return false;
			});
		},
		close : function() {
			$('#close-btn').click(function() {
				$(this).fadeOut('fast');
				$('#nav').fadeOut();
				$('#video').fadeOut(1500, function() {
					$('#header object#video').remove();
					$('#header').slideUp(1000, function() {
						$('#nav').removeAttr('style');
						$('#nav').hide();
						$('#header').removeAttr('style');
						$('#header').css({
							'display'	 : 'none',
							'background' : 'url(' + $('#header').attr('_img') + ') no-repeat',
							'height' 	 : '512px'
						});						
						$('#header').slideDown(1000, function() {
							$('#header').append('<div id="video" style="display:none;"></div>');															
							$('#headline').fadeIn();
							$('#nav').fadeIn();
						});
					});
				});
			});
		}	
	},
	key_navigation : function() {
		$(document).keydown(function (e) {
			//var cc = e.keyCode || e.charCode;
			//alert(cc);
			switch (e.keyCode || e.charCode) {
				case 8:
					return false;
					break;
				case 37:
					var next_link = $("a#next_page").attr("href");
					if (!next_link) return;
					if (next_link.length > 0) {
						window.location = next_link;
						return false;
					}
					break;
				case 39:
					var prev_link = $("a#prev_page").attr("href");
					if (!prev_link) return;					
					if (prev_link.length > 0) {
						window.location = prev_link;
						return false;
					}
					break;
				case 49:
					document.location = '/';
					return false;
					break;
				case 50:
					document.location = '/about/';
					return false;
					break;
				case 51:
					document.location = '/news/';
					return false;
					break;
				case 52:
					document.location = '/clients-cases/';
					return false;
					break;
				case 53:
					document.location = '/people/';
					return false;
					break;
				case 54:
					document.location = '/contact-offices/';
					return false;			
					break;
			}
		});
	},
	replace_round_images : function() {
		if ($('img.round-left').length > 0) {
			$('img.round-left').wrap('<div class="image"></div>');
			$('img.round-left').parent().attr('class','image left');
		}
		if ($('img.round-right').length > 0) {
			$('img.round-right').wrap('<div class="image"></div>');
			$('img.round-right').parent().attr('class','image right');
		}
		$('.col-mid div.image').prepend('<div class="overlay"></div>');
		$('.col-right div.image').prepend('<div class="overlay"></div>');
	},
	news : {
		hover : function() {
			$('.item').bind('mouseover', function(e) {
				var div = $(this).children('div');
				var image = $(div[0]).find('span');
				var link  = $(div[1]).children('a');
				//$(image).attr('_state', $(image).attr('class'));
				$(image).attr('class', $(image).attr('lang') + '_hover');
				$(link).css({'background-color':'#00AADC','color':'#FFF'});
			});
			$('.item').bind('mouseout', function(e) {
				var div = $(this).children('div');
				var image = $(div[0]).find('span');
				var link  = $(div[1]).children('a');
				//$(image).removeAttr('class');
				$(image).attr('class', $(image).attr('lang'));
				$(link).removeAttr('style');			
			});
		},
		expand : function() {
			$('.item').live('click', function() {
			//$('.item').bind('click', function(e) {											  
				//$('html, body').animate({scrollTop:666}, 400);
				var hasToolTip = $(this).attr('_tooltip');
				var id = $(this).attr('_id');
				var div = $(this).children('div');
				var desc  = $(div[1]);
				var bgpos = $(this).attr('_order');
				var bgposval;
				var total_rows = $(this).parent().parent().children('.row').length;				

				if ($(desc).find('a').attr('href')) {
					document.location.href = $(desc).find('a').attr('href');
					return false;
				}
				switch (bgpos) {
					case '1':
						bgposval = '0px 0px';
						break;
					case '2':
						bgposval = '0px -331px';
						break;
					case '3':
						bgposval = '0px -662px';
						break;
					case '0':
						bgposval = '0px -993px';
						break;
				}
				
				global.vars.lastItemId.push(id);
				if (global.vars.lastItemId.length > 2 ) global.vars.lastItemId.shift();
				var lastItemId = global.vars.lastItemId[0];
				
				if ($(this).attr('_state') == 'expanded') {										
					$(this).removeAttr('_state');
					$('#tooltip-'+ id + ' .tooltip-content').fadeOut('slow', function() {
						$('.scrollable').animate({'height':'250px'}, 1000);																				  
						$('#tooltip-'+ id).slideUp('slow', function() {
							$(desc).fadeIn('slow');
						});
					});
					return false;
				} else {
					$('#item-' + lastItemId).removeAttr('_state');
					$('#tooltip-' + lastItemId + ' .tooltip-content').fadeOut('slow', function() {
						$('#tooltip-' + lastItemId).slideUp('slow', function() {
							$('#item-' + lastItemId + ' .desc').fadeIn('slow');
						});
					});
					var s = $(this).parent().next();
					$(s).children('div').slideUp('slow');
					if (hasToolTip == 1) {
						$(this).attr('_state', 'expanded');				
						$('#tooltip-'+ id).css({'background-position': bgposval});
						$('.scrollable').animate({'height':'540px'}, 1000);
						$(desc).fadeOut('slow', function() {
							$('#tooltip-'+ id).slideDown('slow', function() {
								$('#tooltip-'+ id + ' .tooltip-content').fadeIn('slow');
							});
							
						});
						return false;
					}
				}
				return false;
			});
		}
	},
	scroll_fix : function() {
		if (jQuery.browser.safari == true || jQuery.browser.opera == true || jQuery.browser.mozilla == true)
			document.write('<style type="text/css">body {overflow-y: scroll;}</style>');
	},
	contact : {
		show_map : function() {
			$('.gmap a').click(function() {
				var src = $(this).attr('rel');
				$('<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + src + '"></iframe>').modal({
					//onOpen: global.contact.open_win,
					//onClose: global.contact.close_win
				});
				return false;
			});
		},
		open_win : function(dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.container.fadeIn('slow', function () {
					dialog.data.hide().fadeIn('slow');	 
				});
			});
		},
		close_win : function(dialog) {
			dialog.data.fadeOut('slow', function () {
				dialog.container.fadeOut('slow', function () {
					dialog.overlay.fadeOut('slow', function () {
						$.modal.close();
					});
				});
			});
		}
	},
	cat : function() {
		$('#news .options .cat a').click(function() {
			$('.scrollable').animate({'height':'250px'}, 1000);
			if ($(this).attr('class') == 'active') return false;												  
			$('#loader').fadeIn('fast');
			$("#news .options .cat a").each(function(i){
				$(this).removeAttr('class');
				if ($(this).attr('rel')) {
					$('[rel=' + $(this).attr('rel') + ']').removeAttr('class');
				}
			});
			$(this).attr('class', 'active');
			if ($(this).attr('rel')) {
				$('[rel=' + $(this).attr('rel') + ']').attr('class', 'active');
			}
			$.ajax({
				type: 'post',
				url: '/wp-content/themes/gw/ajax.php',
				data: {
					s    : 'news',
					cat  : $(this).html().toLowerCase(),
					order: $('#news .options .order a.active').html().toLowerCase()
				},
				success: function(data) {
					$("#news-content .scrollable").animate({opacity: 0}, 300, 'linear', function() {
						global.vars.scrollable.getItemWrap().empty().append(data);
						$('#news-content .scrollable').animate({opacity: 1}, 300, 'linear', function() {
							global.vars.scrollable.begin();
							global.vars.scrollable.reload();
							$('#slider-nav .total_page').html(global.vars.scrollable.getSize());
							global.news.hover();
							$('#loader').fadeOut('fast');
						});
					});
				}
			 });
			return false;
		});
	},
	order : function() {
		$('#news .options .order a').click(function() {
			$('.scrollable').animate({'height':'250px'}, 1000);													
			if ($(this).attr('class') == 'active') return false;
			$('#loader').fadeIn('fast');
			$("#news .options .order a").each(function(i){
				$(this).removeAttr('class');
				if ($(this).attr('rel')) {
					$('[rel=' + $(this).attr('rel') + ']').removeAttr('class');
				}				
			});
			$(this).attr('class', 'active');
			if ($(this).attr('rel')) {
				$('[rel=' + $(this).attr('rel') + ']').attr('class', 'active');
			}
			$.ajax({
				type: 'post',
				url: '/wp-content/themes/gw/ajax.php',
				data: {
					s    : 'news',
					cat  : $('#news .options .cat a.active').html().toLowerCase(),
					order: $('#news .options .order a.active').html().toLowerCase()
				},
				success: function(data) {
					$("#news-content .scrollable").animate({opacity: 0}, 300, 'linear', function() {
						global.vars.scrollable.getItemWrap().empty().append(data);
						$('#news-content .scrollable').animate({opacity: 1}, 300, 'linear', function() {
							global.vars.scrollable.begin();
							global.vars.scrollable.reload();
							$('#slider-nav .total_page').html(global.vars.scrollable.getSize());
							global.news.hover();
							$('#loader').fadeOut('fast');
						});
					});
				}
			 });
			return false;
		});	
	},
	news_sub_cat : function() {
		$('#news-sub .options .cat a').click(function() {
			if ($(this).attr('class') == 'active') return false;												  
			$('#loader').fadeIn('fast');
			$("#news-sub .options .cat a").each(function(i){
				$(this).removeAttr('class');
				if ($(this).attr('rel')) {
					$('[rel=' + $(this).attr('rel') + ']').removeAttr('class');
				}
			});
			$(this).attr('class', 'active');
			if ($(this).attr('rel')) {
				$('[rel=' + $(this).attr('rel') + ']').attr('class', 'active');
			}
			$.ajax({
				type: 'post',
				url: '/wp-content/themes/gw/sort_news.php',
				data: {
					s    : 'news',
					cat  : $(this).html().toLowerCase(),
					order: $('#news-sub .options .order a.active').html().toLowerCase()
				},
				success: function(data) {
					$("#news-content").fadeOut('slow', function() {
						$("#news-content").html(data);
						$('#news-content').fadeIn('slow', function() {
							global.news.hover();
							$('#loader').fadeOut('fast');
						});
					});
				}
			 });
			return false;
		});
	},
	news_sub_order : function() {
		$('#news-sub .options .order a').click(function() {
			if ($(this).attr('class') == 'active') return false;
			$('#loader').fadeIn('fast');
			$("#news-sub .options .order a").each(function(i){
				$(this).removeAttr('class');
				if ($(this).attr('rel')) {
					$('[rel=' + $(this).attr('rel') + ']').removeAttr('class');
				}				
			});
			$(this).attr('class', 'active');
			if ($(this).attr('rel')) {
				$('[rel=' + $(this).attr('rel') + ']').attr('class', 'active');
			}
			$.ajax({
				type: 'post',
				url: '/wp-content/themes/gw/sort_news.php',
				data: {
					s    : 'news',
					cat  : $('#news-sub .options .cat a.active').html().toLowerCase(),
					order: $('#news-sub .options .order a.active').html().toLowerCase()
				},
				success: function(data) {
					$("#news-content").fadeOut('slow', function() {
						$("#news-content").empty().append(data);
						$('#news-content').fadeIn('slow', function() {
							global.news.hover();
							$('#loader').fadeOut('fast');
						});
					});
				}
			 });
			return false;
		});	
	},
	sort_people : function() {
		$('#people .options .cat a').click(function() {
			if ($(this).attr('class') == 'active') return false;
			var scroll_pos = global.scroll.get();
			$('#loader').fadeIn('fast');
			$("#people .options .cat a").each(function(i){
				$(this).removeAttr('class');
				if ($(this).attr('rel')) {
					$('[rel=' + $(this).attr('rel') + ']').removeAttr('class');
				}
			});
			$(this).attr('class', 'active');
			if ($(this).attr('rel')) {
				$('[rel=' + $(this).attr('rel') + ']').attr('class', 'active');
			}
			$.ajax({
				type: 'post',
				url: '/wp-content/themes/gw/sort_people.php',
				data: {
					office    : $(this).html().toLowerCase()
				},
				success: function(data) {
					$("#people-content").fadeOut('slow', function() {
						$("#people-content").empty().append(data);
						//global.scroll.set(scroll_pos);
						$('#people-content').fadeIn('slow', function() {
							global.news.hover();
							$('#loader').fadeOut('fast');
						});
					});
				}
			 });
			return false;
		});
	}	
};
$(document).ready(function(){global.init();});