//Global Variables
sVars = {
	scrollDelay:		1600,
	easingType:			'easeInOutSine',
	sectionWidth:		4500,
	sectionHeight:	4500,
	pageWidth:			980,
	pageHeight:			2200,
	scrollX:				0,
	scrollY:				0,
	loading:				0,
	base:						0,
	topOffset:			0,
	started:				0
}

sVars.location = {
	"home":								"11",
	"call-tracking":			"10",
	"seo":								"21",
	"web-dev":					"12",
	"web-dev/case-studies":					"12",
	"web-dev/commerce":					"12",
	"ppc":								"01",
	"social-media":								"00",
	"social-media/corporate-videos":								"00",
	"social-media/internet-videos":								"00",
	"social-media/facebook":								"00",
	"social-media/youtube":								"00",
	"case-studies":								"55"
}

sVars.gridArray = ["01","02","10","11","12","20","21","22","55"];
var oSc;

$(document).ready(function(){
	setupPage();
	bindItems();		
	resetPage();
	sVars.oldHash =  document.location.hash;
	var hash = document.location.hash;
	if (hash.length > 2) {
		sVars.started = 1;
		pageLoc = sVars.location[document.location.hash.substr(2,hash.length - 2)];
		if (!sVars.location[document.location.hash.substr(2,hash.length - 2)]) {
				pageLoc = "30";
			}
	} else {	
		pageLoc = sVars.startRel;
	}
	if (jQuery.inArray(pageLoc,sVars.gridArray) > -1 || pageLoc == "00") {
		$('#roi').fadeIn(10);
	} else {
		$('#roi').fadeOut(10);
	}
	sVars.oldRel = pageLoc;
	newPage(pageLoc,hash,1);
});

function pageChange() {
	if (!sVars.noLoad) {
		var hash;
		if (document.location.hash) {
			if (document.location.hash.indexOf('wpcf')>=0)
				return false;
			hash = document.location.hash;
		} else {
			hash = sVars.newPage;
		}
		var del = false;
		if (hash.length > 2) {	
			pageLoc = sVars.location[hash.substr(2,hash.length - 2)];
			if (!sVars.location[hash.substr(2,hash.length - 2)]) {
				pageLoc = "30";
			}
		} else {
			pageLoc = sVars.startRel;
		}
		if (jQuery.inArray(sVars.oldRel,sVars.gridArray) == -1) {
			if (sVars.oldRel == "00") {
			} else {
				del = true;
			}
		}
		if (jQuery.inArray(pageLoc,sVars.gridArray) > -1 || pageLoc == "00") {
			
		} else {
			$('#roi').fadeOut(500);
			del = true;
		}
		sVars.oldRel = pageLoc;
		var newHash =  hash.substr(1,hash.length - 1);
		if (newHash == "/home")
			newHash = "/";
		_gaq.push(['_setAccount', 'UA-10462112-5']);
		_gaq.push(['_trackPageview', newHash]);
		newPage(pageLoc,hash, del);
	}
	sVars.noLoad = false;
}

function newPage(rel,url,delay) {
		var oSc = 0;
		
		$('.nav-link').removeClass('selected');
		if (url.length > 2) {
			$('#' + url.substr(2,url.length - 2)).addClass('selected');
		}
		if (delay != 1) {
			delay = false;
		}
		if ($('#' + rel).length > 0 && rel != "30") {
			sVars.currentPage = $('#' + rel);	
			sVars.loading = 0;
			if (delay == 1 && sVars.started == 1) {
				//sVars.currentPage.css('opacity',0);
				//sVars.currentPage.animate({opacity: 1},{duration: sVars.scrollDelay});
				sVars.currentPage.children('.page').fadeOut(0);
				sVars.currentPage.children('.page').fadeIn(sVars.scrollDelay);
				if (jQuery.inArray(pageLoc,sVars.gridArray) > -1 || pageLoc == "00") {
					$('#roi').fadeIn(sVars.scrollDelay);
				}
			} else {
				sVars.started = 1;
			}
			sVars.currentPage.scrollPage(delay);

		} else {
			sVars.currentPage = $('#' + rel);	
			if ($('#' + rel).length > 0 && sVars.started==1) {
				sVars.currentPage.children('.page').remove();
				sVars.currentPage.html('<div class="page"><div class="page-title"><div class="main-title">Loading...Please Wait</div><div class="subtitle smaller"><img src="/wp-content/themes/oozle/images/loading.gif" /></div></div>');
			} else if( sVars.started==1) {
				$('#wrap').append('<div id="' + rel + '" class="section"><div class="page"><div class="page-title"><div class="main-title">Loading...Please Wait</div><div class="subtitle smaller"><img src="/wp-content/themes/oozle/images/loading.gif" /></div></div></div>');
			}
			if (sVars.started == 1 || document.location.hash) {
					
				sVars.currentPage = $('#' + rel);	
				sVars.loading = 1;
				sVars.scrollYAPI.reinitialise();
				sVars.currentPage.load(url.substr(1,url.length - 1) + ' #wrap .section .page', function() {
					
					sVars.loading = 0;
					sVars.currentPage.children('.page').fadeOut(0);
					sVars.currentPage.children('.page').fadeIn(sVars.scrollDelay);
					if (jQuery.inArray(pageLoc,sVars.gridArray) > -1 || pageLoc == "00") {
						$('#roi').fadeIn(sVars.scrollDelay);
					}
					oSc = sVars.scrollY;
					setTimeout("$('#scroll-inside').height(sVars.currentPage.children('.page').height() + 40 + sVars.topOffset + 130);sVars.scrollYAPI.reinitialise(); sVars.scrollYAPI.scrollToY(oSc, false);sVars.scrollYAPI.scrollToY(0,true);",100);

					bindLinks(sVars.currentPage);
					bindContactForm();
			  });
			} else {
				sVars.currentPage = $('#' + rel);	
				setTimeout("$('#scroll-inside').height(sVars.currentPage.children('.page').height() + 40 + sVars.topOffset + 130);sVars.scrollYAPI.reinitialise();sVars.scrollYAPI.scrollToY(0);",100);
				sVars.loading = 0;		
				sVars.started = 1;
			}
			
			sectionID = sVars.currentPage.attr('id');
			sVars.currentPage.css('position','absolute');
			sVars.currentPage.css('left',sectionID.substr(0,1) * sVars.sectionWidth);
			sVars.currentPage.css('top',sectionID.substr(1,1) * sVars.sectionHeight);	
			sVars.currentPage.css('width',sVars.sectionWidth);
			sVars.currentPage.css('height',sVars.sectionHeight);
			sVars.currentPage.scrollPage(delay);
		}	
		
}

function setupPage() {
	$('body').attr('scroll','no');
	$('#mainWrap').prepend('<div class="scroll" id="scroll"><div class="" id="scroll-inside"></div></div><div class="scroll-hor" id="scrollHor"><div class="" id="scroll-hor-inside"></div></div><div class="scroll-nub" id="scrollNub"></div>');	
	$('#scroll-hor-inside').width(1000);
	$('body').css('overflow','hidden');
	$('body').addClass('has-js');
	sVars.oldPage = "#" + document.location.pathname;
	
	$('#wrap').width(sVars.sectionWidth * 3 + 100);
	$('#wrap').height(sVars.sectionHeight*3 + 100);
	$('#mainWrap').width(getWidth());
	$('#mainWrap').height(getHeight());
	$('#mainWrap').css('overflow','hidden');
	sVars.startRel = $('.section').attr('id');
	sVars.oldRel = $('.section').attr('id');
	
	$('.section').width(sVars.sectionWidth);
	$('.section').height(sVars.sectionHeight);
	$('.section').css('position','absolute');
	$('.section').each(function(index) {
		sectionID = $(this).attr('id');
		$(this).css('left',sectionID.substr(0,1) * sVars.sectionWidth);
		$(this).css('top',sectionID.substr(1,1) * sVars.sectionHeight);
	});

	//Bind Scroll Event To Current Page
	$('#scroll-inside').height($('#scrollHor').height());
	sVars.scrollYAPI = $('#scroll').jScrollPane(({showArrows: true})).data('jsp');
	sVars.scrollXAPI = $('#scrollHor').jScrollPane(({showArrows: true})).data('jsp');
	setInterval ( "checkHash()", 50 );
}

function checkHash() {
	if 	(window.location.hash != sVars.oldHash) {
		sVars.oldHash = window.location.hash;
		pageChange();
	}
	
}
function bindItems() {
	bindScrollBars();
	bindLinks();
	bindWindow();
}

function bindWindow() {
	$(window).resize(function() {
		resetPage();
		$('#wrap').css('left',(sVars.currentPage.position().left + sVars.leftOffset + sVars.scrollX) * -1);
		sVars.left = $('#wrap').position().left - sVars.scrollX;
		$('#nav').css('left',sVars.scrollX * -1);
	});
	$(window).bind("popstate", function(event) {
		if (sVars.started == 1 && sVars.oldPage != "#" + document.location.pathname) {
		sVars.newPage = "#" + document.location.pathname;
		if (sVars.newPage.substr(sVars.newPage.length - 1,1) == '/') {
			sVars.newPage = sVars.newPage.substr(0,sVars.newPage.length - 1);
			if (sVars.newPage.length < 2) {
				sVars.newPage = "#" + '/home';
			}
		}
		sVars.oldPage = sVars.newPage;
		if ($("#" + sVars.location[sVars.newPage.substr(2,sVars.newPage.length - 2)]).length > 0) {
			$("#" + sVars.location[sVars.newPage.substr(2,sVars.newPage.length - 2)]).remove();
		}
		pageChange();
		}
	});	
}

function bindScrollBars() {
	$('#scroll')
			.bind(
				'jsp-scroll-y',
				function(event, scrollPositionY, isAtTop, isAtBottom)
				{
					if (sVars.portfolio == 1) {
						$('.overlay .inside').css('top',scrollPositionY * -1);
						sVars.scrollY = scrollPositionY;
					} else {
						if (sVars.startScroll == 0) {
							sVars.scrollY = scrollPositionY;
							$('#wrap').css('top',sVars.top - scrollPositionY);
							$('#roi').css('top',160 - scrollPositionY);
							$('.secondary').css('top',135 - scrollPositionY);
						} else {
							sVars.scrollY = 0;
						}
					}
				}
			);
	$('#scrollHor')
			.bind(
				'jsp-scroll-x',
				function(event, scrollPositionX, isAtLeft, isAtRight)
				{
					if (sVars.startScroll == 0) {
						sVars.scrollX = scrollPositionX;
						$('#wrap').css('left',sVars.left - scrollPositionX);
						$('#nav').css('left',scrollPositionX * -1);
					} else {
						sVars.scrollX = 0;
					}
				}
			);			
}

function bindLinks(rebind) {
	var selector = 'a';
	//if (rebind)
		//selector = rebind.children('a');
	//else 
	selecter = $('a');
		
	selecter.each(function() {
		if ($(this).attr('href')) {
			
			if (($(this).attr('href').substr(0,1) == "/" || $(this).attr('href').indexOf(window.location.hostname) >=0) && $(this).attr('rel') != 'nofollow' && !$(this).hasClass('nolink') && !$(this).hasClass('external') && ($(this).attr('href').slice(-3) != "jpg" && $(this).attr('href').slice(-3) != "png")){
				$(this).unbind();
				if ($(this).attr('href').indexOf(window.location.hostname) >=0) 
					$(this).attr('href', $(this).attr('href').substr(window.location.hostname.length + 7, $(this).attr('href').length - (window.location.hostname.length + 5)));
				
				if ($(this).attr('href').substr($(this).attr('href').length -1,1)=="/" && $(this).attr('href').length > 2)
					$(this).attr('href',$(this).attr('href').substr(0,$(this).attr('href').length -1));
					
				$(this).click(function(event){

				event.preventDefault();
				if (typeof(window.history.pushState) == 'function') {
					window.history.pushState(null, $(this).attr('href'), $(this).attr('href'));
					sVars.newPage = "#" + $(this).attr('href');
					if ($(this).attr('href') == '/') {
						sVars.newPage = "#" + '/home';
					}
					sVars.oldPage = sVars.newPage;
					pageChange();
				} else {
					window.location.hash = $(this).attr('href');
				}
				return false;
				});
			}
		}
	});	
	
		var ob = document.getElementsByTagName("script");
		for(var i=0; i<ob.length-1; i++){
		if(ob[i+1].text!=null) {
			//eval(ob[i+1].text);
		}
		}
		
	bindAboutLinks();
}


function bindAboutLinks() {


	$('.left-col li a').each(function() {
									  
		if ($(this).attr('href').substr($(this).attr('href').length -1,1)=="/" && $(this).attr('href').length > 2)
			$(this).attr('href',$(this).attr('href').substr(0,$(this).attr('href').length -1));		
			
		if ($(this).hasClass('nolink')) {
			$(this).unbind();
			$(this).click(function(event) {
				event.preventDefault();
				sVars.atHref = $(this).attr('href');
				$('.left-col li').removeClass('current');
				$(this).parent().addClass('current');
				$('#' + sVars.oldRel + ' .right-col').html('<div class="page-title"><div class="main-title">Loading...Please Wait</div><div class="subtitle smaller"><img src="/wp-content/themes/oozle/images/loading.gif" /></div>');
		
				$('#' + sVars.oldRel + ' .right-col').load($(this).attr('href') + ' #rightcol .inside', function(response, status, xhr) {
				  if (status == "error") {
					var msg = "Sorry but there was an error: ";
					$('#' + sVars.oldRel + ' .right-col').html(msg + xhr.status + " " + xhr.statusText);
				  } else {

					$('#rightcol').css('opacity',0);
					$('#rightcol').animate({opacity: 1},{duration: sVars.scrollDelay});
					urlLen = window.location.hostname.length + 7;
					sVars.oldPage = "#" + sVars.atHref.substr(urlLen,sVars.atHref.length - urlLen);
					
					if (sVars.currentPage.children('.page').height() + 40 + sVars.topOffset + 130 > $('#scroll-inside').height())
						$('#scroll-inside').height(sVars.currentPage.children('.page').height() + 40 + sVars.topOffset + 130);
					oSc = sVars.scrollY;
					sVars.scrollYAPI.scrollToY(0, false);
					sVars.scrollYAPI.reinitialise();
					sVars.scrollYAPI.scrollToY(oSc, false);
					sVars.scrollYAPI.scrollToY(oSc + $('#' + sVars.oldRel + ' .right-col').offset().top - 110, true);
					if (typeof(window.history.pushState) == 'function') {
						window.history.pushState(null, sVars.atHref.substr(urlLen,sVars.atHref.length - urlLen), sVars.atHref.substr(urlLen,sVars.atHref.length - urlLen));
					} else {
						sVars.oldHash = "#" + sVars.atHref.substr(urlLen,sVars.atHref.length - urlLen);
						window.location.hash = sVars.atHref.substr(urlLen,sVars.atHref.length - urlLen);
					}
						var newHash =  sVars.atHref.substr(urlLen,sVars.atHref.length - urlLen);
						if (newHash == "/home")
							newHash = "/";
						_gaq.push(['_setAccount', 'UA-12345-1']);
						_gaq.push(['_trackPageview', newHash]);
				  }
				}); 
				return false;
			});
		} else {
			
		}
	});
}

function resetPage() {
		sVars.screenWidth = getWidth();
		sVars.screenHeight = getHeight();
		$('#mainWrap').width(sVars.screenWidth);
		$('#mainWrap').height(sVars.screenHeight);
		sVars.leftOffset = (sVars.sectionWidth / 2 - sVars.screenWidth/2);
		if (sVars.screenWidth < sVars.pageWidth) {
			sVars.leftOffset = (sVars.sectionWidth / 2 - sVars.screenWidth/2) + ((sVars.screenWidth - sVars.pageWidth) / 2);
			$('#roi').css('marginLeft',(-490 - ((sVars.screenWidth - sVars.pageWidth) / 2)));
		} else {
			$('#roi').css('marginLeft',(-490));
		}

		if ($('#scroll-inside').height()>$('#scroll').height() && $('#scroll-hor-inside').width()>$('#scrollHor').width() ) {
			$('#scroll').height(sVars.screenHeight - 18);
			$('#scrollHor').width(sVars.screenWidth - 18);
			$('#scrollNub').show();
		} else {
			$('#scroll').height(sVars.screenHeight);
			$('#scrollHor').width(sVars.screenWidth);
			$('#scrollNub').hide();
		}
		var oScroll = sVars.scrollX;
		sVars.scrollXAPI.reinitialise();
		sVars.scrollXAPI.scrollToX(oScroll, false);
		oScroll = sVars.scrollY;
		sVars.scrollYAPI.reinitialise();	
		sVars.scrollYAPI.scrollToY(oScroll, false);
		if (sVars.portfolio == 1) {
			$('.overlay').height(sVars.screenHeight);
			$('.overlay').width(sVars.screenWidth);	
			$('#scroll-inside').height($('.overlay .inside').height() + 150);
			sVars.scrollYAPI.reinitialise();
			sVars.scrollYAPI.scrollToY(oScroll, false);
		}
}

jQuery.fn.scrollPage = function(delay) {
		sVars.startScroll = 1;
		sVars.currentPage = this;
		if (sVars.loading == 0) {
			oSc = sVars.scrollY;
			setTimeout("$('#scroll-inside').height(sVars.currentPage.children('.page').height() + 40 + sVars.topOffset + 130);sVars.scrollYAPI.reinitialise();sVars.scrollYAPI.scrollToY(oSc, false);sVars.scrollYAPI.scrollToY(0,true);",100);
		}
		if (delay) {
			$('#wrap').css('top',this.position().top * -1 + sVars.topOffset);
			$('#wrap').css('left',(this.position().left + sVars.leftOffset) * -1);
			$('.secondary').animate({top: 135},{queue: false, duration: 50, easing: sVars.easingType});
			sVars.top = $('#wrap').position().top;
			sVars.left = $('#wrap').position().left;
			sVars.startScroll = 0;
		} else {	
			$('.secondary').animate({top: 135},{queue: false, duration: sVars.scrollDelay, easing: sVars.easingType});
			$('#roi').animate({top: 160},{queue: false, duration: sVars.scrollDelay, easing: sVars.easingType});
			$('#wrap').animate({top: this.position().top * -1  + sVars.topOffset, left: (this.position().left + sVars.leftOffset + sVars.scrollX) * -1}, {queue: false, duration: sVars.scrollDelay, easing: sVars.easingType, complete: function(){
																													sVars.top = $('#wrap').position().top; 
																													sVars.left = $('#wrap').position().left + sVars.scrollX; 
																													sVars.startScroll = 0;
																													sVars.started = 1;
																												}});
			sVars.startScroll = 1;
			
		}

		
}

function bindContactForm() {
		try {
			if (typeof _wpcf7 == 'undefined' || _wpcf7 === null)
				_wpcf7 = {};

			_wpcf7 = jQuery.extend({ cached: 0 }, _wpcf7);

			jQuery('div.wpcf7 > form').ajaxForm({
				beforeSubmit: function(formData, jqForm, options) {
					jqForm.wpcf7ClearResponseOutput();
					jqForm.find('img.ajax-loader').css({ visibility: 'visible' });
					return true;
				},
				beforeSerialize: function(jqForm, options) {
					jqForm.find('.wpcf7-use-title-as-watermark.watermark').each(function(i, n) {
						jQuery(n).val('');
					});
					return true;
				},
				data: { '_wpcf7_is_ajax_call': 1 },
				dataType: 'json',
				success: function(data) {
					var ro = jQuery(data.into).find('div.wpcf7-response-output');
					jQuery(data.into).wpcf7ClearResponseOutput();

					if (data.invalids) {
						jQuery.each(data.invalids, function(i, n) {
							jQuery(data.into).find(n.into).wpcf7NotValidTip(n.message);
						});
						ro.addClass('wpcf7-validation-errors');
					}

					if (data.captcha)
						jQuery(data.into).wpcf7RefillCaptcha(data.captcha);

					if (data.quiz)
						jQuery(data.into).wpcf7RefillQuiz(data.quiz);

					if (1 == data.spam)
						ro.addClass('wpcf7-spam-blocked');

					if (1 == data.mailSent) {
						jQuery(data.into).find('form').resetForm().clearForm();
						ro.addClass('wpcf7-mail-sent-ok');

						if (data.onSentOk)
							jQuery.each(data.onSentOk, function(i, n) { eval(n) });
					} else {
						ro.addClass('wpcf7-mail-sent-ng');
					}

					if (data.onSubmit)
						jQuery.each(data.onSubmit, function(i, n) { eval(n) });

					jQuery(data.into).find('.wpcf7-use-title-as-watermark.watermark').each(function(i, n) {
						jQuery(n).val(jQuery(n).attr('title'));
					});

					ro.append(data.message).slideDown('fast');
				}
			});

			jQuery('div.wpcf7 > form').each(function(i, n) {
				if (_wpcf7.cached)
					jQuery(n).wpcf7OnloadRefill();

				jQuery(n).wpcf7ToggleSubmit();

				jQuery(n).find('.wpcf7-acceptance').click(function() {
					jQuery(n).wpcf7ToggleSubmit();
				});

				jQuery(n).find('.wpcf7-exclusive-checkbox').each(function(i, n) {
					jQuery(n).find('input:checkbox').click(function() {
						jQuery(n).find('input:checkbox').not(this).removeAttr('checked');
					});
				});

				jQuery(n).find('.wpcf7-use-title-as-watermark').each(function(i, n) {
					var input = jQuery(n);
					input.val(input.attr('title'));
					input.addClass('watermark');

					input.focus(function() {
						if (jQuery(this).hasClass('watermark'))
							jQuery(this).val('').removeClass('watermark');
					});

					input.blur(function() {
						if ('' == jQuery(this).val())
							jQuery(this).val(jQuery(this).attr('title')).addClass('watermark');
					});
				});
			});

		} catch (e) {
		}
}

function getHeight() {
	var scnHei;
	if (self.innerHeight) {
		scnHei = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		scnHei = document.documentElement.clientHeight;
	} else if (document.body) {
		scnHei = document.body.clientHeight;
	}
	return scnHei;
}

function getWidth() {
	var scnWid;
	if (self.innerWidth) {
		scnWid = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		scnWid = document.documentElement.clientWidth;
	} else if (document.body) {
		scnWid = document.body.clientWidth;
	}
	return scnWid;
}

var newMargin = 0;

function featuresLeft() {
	if (newMargin < 0) {
		newMargin = newMargin + 324;
	
		$('#features').animate({marginLeft: newMargin},{queue: false, duration: 1200, easing: "easeOutBounce"});
		featuresFade();
	}
}

function featuresRight() {
	if (newMargin > ($('.feature').length - 3) * -324) {
		newMargin = newMargin - 324;
	
		$('#features').animate({marginLeft: newMargin},{queue: false, duration: 1200, easing: "easeOutBounce"});
		featuresFade();
	}
}

function featuresFade() {
	$('.feature').each(function(index) {
		var newPos = (index * 324) + newMargin;
		if (newPos < 0 ||  newPos >= 900) {
			$(this).animate({opacity: -1},{queue: false, duration: 800});
		} else {
			$(this).animate({opacity: 1},{queue: false, duration: 800});
		}
	});
}

function openSite(projectName,projectURL,projectDescription, projectScreenshot) {
	$('body').append('<div class="overlay" id="overlay"><div class="inside"><div class="media"><img src="' + projectScreenshot + '" /></div><div class="caption"><div class="title">' + projectName + '<span><a href="' + projectURL + '" target="_blank">' + projectURL + '</a></span></div><p>' + projectDescription + '</p></div><div style="clear: both;"></div><img onclick="closeSite();" src="/wp-content/themes/oozle/images/portfolio-close.gif" style="margin-top: 1px; float: right; cursor: pointer;" /></div></div>');
	$('.overlay').css('opacity',0);
	$('.overlay').height(sVars.screenHeight);
	$('.overlay').width(sVars.screenWidth);
	$('.overlay').animate({opacity: 1},{duration: 300});
	sVars.oldScrollY = sVars.scrollY;
	$('#scroll-inside').height($('.overlay .inside').height() + 150);
	sVars.portfolio = 1;
	sVars.scrollYAPI.reinitialise();
	sVars.scrollYAPI.scrollToY(0, false);
	$('.overlay').click(function(event) {
		if ($(event.target).attr('id') == 'overlay') {
			closeSite();
		}
	});
}

function closeSite() {
		$('#overlay').animate({opacity: 0},{duration: 300, complete: function() {
			$(this).remove();
			$('#scroll-inside').height(sVars.currentPage.children('.page').height() + 40 + sVars.topOffset + 130);
			sVars.portfolio = 0;
			sVars.scrollYAPI.reinitialise();
			sVars.scrollYAPI.scrollToY(sVars.oldScrollY, false);
		}});	
}

