// JavaScript Document


/*
// Detect whether device supports orientationchange event, otherwise fall back to 
// the resize event. 
var supportsOrientationChange = "onorientationchange" in window, 
    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; 

window.addEventListener(orientationEvent, function() { 
    
	$('#footer').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
 
	$('#page').animate({height: ($(window).height()-10)+'px'}, 1, function(){});
	$('#page').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
	
	horizon();
	
}, false);

*/

/*
var supportsOrientationChange2 = "ongesturechange" in window, 
    orientationEvent2 = supportsOrientationChange2 ? "gesturechange" : "resize"; 
 
window.addEventListener(orientationEvent2, function() { 

	alert("ZOOM CHANGE");
    
	$('#footer').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
 
	$('#page').animate({height: ($(window).height()-10)+'px'}, 1, function(){});
	$('#page').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
	
	horizon();
	
}, false);

*/

window.onresize = function(event){

	$('#footer').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
 
	$('#page').animate({height: ($(window).height()-10)+'px'}, 1, function(){});
	$('#page').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
	
	horizon();
	
}

function resizer(){
	
	$('#site').css('display','block');
	
	$("object").append(
		$("<param/>").attr({
			'name': 'wmode',
			'value': 'transparent'
		})
	).find("embed").attr('wmode', 'transparent');
	
	$('#footer').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
 
	$('#page').animate({height: ($(window).height()-10)+'px'}, 1, function(){});
	$('#page').animate({width: ($(window).width()-10)+'px'}, 1, function(){});
	
	horizonfast();
	
}

function return_page_dimensions(){
	
	if(window.addEventListener){
		
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
		
	} else {
		
		windowWidth = document.documentElement.clientWidth ||
		document.body.clientWidth;
		windowHeight = document.documentElement.clientHeight ||
		document.body.clientHeight;
		
	}
	
}

open_page = "nothing";

function animate_background(coordinates, page_url, history_push){

	if(open_page != page_url){
		
		if(document.getElementById('__legacy_feature_panel')){ clearInterval(viewer_interval); } 
	
		fade_out('pagecontentholder');
		
		$('#permalink_url').html('<span style="text-decoration:blink;">loading page data one moment..</span>');
		
		if($(window).width() < 1025){
		
			$('#page').animate({opacity: '.2'}, 300, function(){ $('#page').animate({opacity: '1'}, 600, function(){ document.getElementById('loader').style.display = 'block'; request_ajax("ajax_page_called:::::requests",page_url+":::::"+website_requests,website_root_url + 'includes/legacy.ajax.page.loader.php','pageloader',0,"horizon(); document.getElementById('loader').style.display = 'none'; if(document.getElementById('__legacy_feature_panel')){ view_load(0); } set_links_to_ajax_paths();"); }) });
		
		} else {
			
			$('#page').animate({opacity: '.2'}, 300, function(){ $('#page').animate({backgroundPosition: coordinates}, 600, function(){ $('#permalink_url').html('Permalink: <a rel="permalink" href="'+website_root_url + page_url +'">'+website_root_url + page_url +'</a>'); var wh = $(window).height(); $('#pageloader').css('top',wh+'px'); $('#page').animate({opacity: '1'}, 300, function(){ document.getElementById('loader').style.display = 'block'; request_ajax("ajax_page_called:::::requests",page_url+":::::"+website_requests,website_root_url + 'includes/legacy.ajax.page.loader.php','pageloader',0,"horizon(); document.getElementById('loader').style.display = 'none'; if(document.getElementById('__legacy_feature_panel')){ view_load(0); } set_links_to_ajax_paths();"); }) }) });
		
		}
		
		open_page = page_url;
		
		if(history_push){
		
		
		
		} else {
			
			history_stack_clean();
			
			ajax_history.push(page_url);
			
			ajax_history_current = ajax_history.length-1;
			
		}
		
		history_toolbar_update();
		
		pageTracker._trackPageview("/"+page_url);
		
	}
	
}

function horizon(){
	
	$('#pageloader').css('height','auto');

	var wh = $(window).height();
	var ww = $(window).width();
	
	var h = $('#pageloader').height();
	var w = $('#pageloader').width();
	
	var l = (ww/2) - (w/2);
	var t = (wh/2) - (h/2)-30;
	
	//var t = ((h-wh)/2)-40;
	
	if(h < (wh-150)){
		
		$('#pageloader').css('height',(wh-150)+'px');

		$('#pageloader').animate({top: t+'px',left: l+'px'}, 500, function(){fade_in('pageloader');});
		
	} else {
		
		$('#pageloader').css('height',(wh)+'px');
	
		$('#pageloader').animate({top: '0px',left: l+'px'}, 500, function(){fade_in('pageloader');});
		
	}
	
	$('#pagemenutrigger').css('height',(wh-50)+'px');
	$('#pagemenutrigger').css('width',ww+'px');


}

function horizonmobile(){
	
	$('#pageloader').css('height','5000px');
	$('#pageloader').css('overflow','auto');
	$('html').css('overflow','auto');
	$('body').css('overflow','auto');
	$('html').css('height','5000px');
	$('body').css('height','5000px');
	$('#page').css('height','5000px');
	$('#page').css('overflow','auto');
	
	var wh = $(window).height();
	var ww = $(window).width();
	
	var h = $('#pageloader').height();
	var w = $('#pageloader').width();
	
	var l = (ww/2) - (w/2);
	var t = (wh/2) - (h/2)-30;
	
	//var t = ((h-wh)/2)-40;
	
	if(h < (wh-150)){
		
		//$('#pageloader').css('height',(wh-150)+'px');

		$('#pageloader').animate({top: t+'px',left: l+'px'}, 500, function(){fade_in('pageloader');});
		
	} else {
		
		//$('#pageloader').css('height',(wh)+'px');
	
		$('#pageloader').animate({top: '0px',left: l+'px'}, 500, function(){fade_in('pageloader');});
		
	}
	
	$('#pagemenutrigger').css('height',(wh-50)+'px');
	$('#pagemenutrigger').css('width',ww+'px');


}

function horizonfast(){
	
	$('#pageloader').css('height','auto');

	var wh = $(window).height();
	var ww = $(window).width();
	
	var h = $('#pageloader').height();
	var w = $('#pageloader').width();
	
	var l = (ww/2) - (w/2);
	var t = (wh/2) - (h/2)-30;
	
	//var t = ((h-wh)/2)-40;
	
	if(h < (wh-150)){
		
		$('#pageloader').css('height',(wh-150)+'px');

		$('#pageloader').animate({top: t+'px',left: l+'px'}, 1, function(){fade_in('pageloader');});
		
	} else {
		
		$('#pageloader').css('height',(wh)+'px');
	
		$('#pageloader').animate({top: '0px',left: l+'px'}, 1, function(){fade_in('pageloader');});
		
	}
	
	$('#pagemenutrigger').css('height',(wh-50)+'px');
	$('#pagemenutrigger').css('width',ww+'px');


}

ajax_history = Array();

ajax_history_current = 0;

function history_back(){

	if(ajax_history_current > 0){
		
		ajax_history_current--;
		
		animate_background(get_random_background_coordinates(), ajax_history[ajax_history_current], 1);
		
		history_toolbar_update();
	
	}

}

function history_forward(){

	if(ajax_history_current < (ajax_history.length-1)){
		
		ajax_history_current++;
		
		animate_background(get_random_background_coordinates(), ajax_history[ajax_history_current], 1);
		
		history_toolbar_update();
	
	}

}

function history_stack_clean(){

	while(ajax_history.length > (ajax_history_current+1)){
	
		ajax_history.pop();
	
	}

}

function history_toolbar_update(){

	if(ajax_history_current == (ajax_history.length-1)){
		
		$('#historyforward').css({opacity: .5});
		
	} else {
		
		$('#historyforward').css({opacity: 1});
		
	}
	
	if(ajax_history_current == 0){
		
		$('#historyback').css({opacity: .5});
		
	} else {
		
		$('#historyback').css({opacity: 1});
		
	}
	
}

function animate_open(){

	document.getElementById('site').style.display = 'block';
	document.getElementById('initloader').style.display = 'none';
	
	$('#footer').animate({width: ($(window).width()-10)+'px'});
	
	$('#page').animate({height: ($(window).height()-10)+'px'}, 100, function(){});
	$('#page').animate({width: ($(window).width()-10)+'px'}, 1000, function(){});
	
}

function fade_in(div){

	$('#'+div).fadeIn(800,function(){});

}

function fade_out(div){

	$('#'+div).fadeOut(1000,function(){});

}

dropmenus = Array();

dropmenus.push("dropmenuservices");
dropmenus.push("dropmenutechnologies");

dropmenu_current = "";

function animate_dropmenu(div,w){

	if(dropmenu_current != div){

		$('#'+div).animate({width: w+'px'}, 500, function(){});
		
		dropmenu_current = div;
		
		animate_hide_dropmenu();
		
	}

}

function animate_hide_dropmenu(){

	for(i=0;i<dropmenus.length;i++){
	
		if(dropmenu_current != dropmenus[i]){
		
			$('#'+dropmenus[i]).animate({width: '0px'}, 300, function(){});
			
		}
		
	}

}

function animate_hide_all_dropmenus(){

	for(i=0;i<dropmenus.length;i++){
		
		$('#'+dropmenus[i]).animate({width: '0px'}, 100, function(){});
		
	}
	
	dropmenu_current = "";

}

function fadeout(div){
	
	$('#'+div).animate({opacity: 0}, 500, function(){});
	
}

function fadein(div){
	
	$('#'+div).animate({opacity: 1}, 500, function(){});
	
}

function fadeout_50_percent(div){
	
	$('#'+div).animate({opacity: .5}, 500, function(){});
	
}

function scrollup(div){
	
	$('#'+div).animate({scrollTop: '-=240'}, 500, function(){});
	
}

function scrolldown(div){
	
	$('#'+div).animate({scrollTop: '+=240'}, 500, function(){});
	
}

function request_ajax(vars,values,request_url,target,append,callback){
	
	function updatecontent(data,tar){
		
		document.getElementById(tar).innerHTML = data;
		
	}
	
	var xmlhttp =  new XMLHttpRequest();
	
	vars_array = vars.split(":::::");
	values_array = values.split(":::::");
	
	query_string = '';
	
	for(i=0;i<vars_array.length;i++){
	
		//values_array[i] = values_array[i].replace(/"/g,'\\"');
		//values_array[i] = values_array[i].replace(/\n/g,'::::::');
		//values_array[i] = values_array[i].replace(/\r/g,'::::::');
		//values_array[i] = values_array[i].replace(/\t/g,'::::::');
	
		query_string = query_string + vars_array[i] + "=" + encodeURIComponent(values_array[i]) + "&";
	
	}
	
	xmlhttp.open('POST', request_url);
	
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;'); 
	//xmlhttp.setRequestHeader('Content-length',query_string.length);
	//xmlhttp.setRequestHeader('Connection','close');
	
	xmlhttp.send(query_string);
	
	xmlhttp.onreadystatechange = function() {
	
		if (xmlhttp.readyState == 4){
			
			if(xmlhttp.status == 200){
			
				if(!append){
		
					response = xmlhttp.responseText;
					
				} else {
				
					response = xmlhttp.responseText + document.getElementById(target).innerHTML;
				
				}
				
				if(target){
					
					updatecontent(response, target);
					
				}
				
				if(callback){ eval(callback); }
				
				return false;
			
			} else {
				
				return true;
				
			}
	
			
		}
		
	}
	
}

function get_random_background_coordinates(){

	var bg_width = 3950;
	var bg_height = 3950;
	
	var maxVal = bg_width - $(window).width();
	var minVal = 0;
	
	var randVal_x = minVal+(Math.random()*(maxVal-minVal));
	
	var maxVal = bg_height - $(window).height();
	var minVal = 0;
	
	var randVal_y = minVal+(Math.random()*(maxVal-minVal)); 
	
	var output = (Math.round(randVal_x)*-1)+"px "+(Math.round(randVal_y)*-1)+"px";
	 
	return output;

}

// preload images before page onload is triggered -->

wallpaper = new Image(3958,3937); 
wallpaper.src="http://www.sleekinteractive.com/legacy.dev/media/jpg/swirlpaper.jpg"; 

header = new Image(1874,82); 
header.src="http://www.sleekinteractive.com/legacy.dev/media/png/headerbg.png"; 

footer = new Image(200,154); 
footer.src="http://www.sleekinteractive.com/legacy.dev/media/png/footerbg.png"; 

badge = new Image(75,66); 
badge.src="http://www.sleekinteractive.com/legacy.dev/media/png/badge.png"; 

logo = new Image(493,90); 
logo.src="http://www.sleekinteractive.com/legacy.dev/media/png/logo.png"; 


function scroll_content_up(){
	
	$('#pageloader').animate({scrollTop: '-=50px'},1);	
	
}

function scroll_content_down(){
	
	$('#pageloader').animate({scrollTop: '+=50px'},1);	
	
}

function auto_scroll_content_up(){
	
	scroll_content_interval = setInterval('scroll_content_up()',400);	
	
}

function auto_scroll_content_down(){
	
	scroll_content_interval = setInterval('scroll_content_down()',400);	
	
}

/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta) {
        if (delta < 0)
		scroll_content_down();
        else
		scroll_content_up();
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;




function throw_anchor(){
	
	if(ajax_anchor){
		
		var targetOffset = $('#'+ajax_anchor).offset().top - $(window).height()-80;
	
		$('#pageloader').animate({scrollTop: targetOffset}, 800);
		
		ajax_anchor = '';
		
	}	
	
}



// set links to AJAX paths //

function set_links_to_ajax_paths(){
	
	output = '';
	
	var allLinks = document.links;
	
	for (var i=0; i<allLinks.length; i++) {
		
		l = allLinks[i].href;
	  	// convert to AJAX link //
		
		if(allLinks[i].rel != 'permalink'){
		
			if(l.indexOf(website_root_url) == -1){
				
				if((l.indexOf('http://') == -1) && (l.indexOf('https://') == -1)){
				
					
					
				} else {
					
					allLinks[i].href = "javascript: target_blank('"+l+"')";
					allLinks[i].title = 'Click to open preview panel!';
					allLinks[i].target = '_self';
					
				}
				
			} else {
				
				nl = l.replace(website_root_url, '');
				
				ajax_link = "javascript: load_page('"+nl+"');";
				//$(this).attr('href',ajax_link);
				allLinks[i].href = ajax_link;
				
			}
			
		}
		
	}
	
	/*
	$('a').each(function(){
						 
		l = $(this).attr('href');
		
		if(l.indexOf(website_root_url) === -1){
			
			alert('FOUND EXTERNAL LINK TO: ' + l);
			
		} else {
			
			// convert to AJAX link //
			
			nl = l.replace(website_root_url, '');
			
			ajax_link = "javascript: load_page('"+nl+"');";
			$(this).attr('href',ajax_link);
			
		}
		
	})
	*/
	
	$('li').each(function(){
	
		if($(this).attr('onclick') != undefined){
			
			l = $(this).attr('onclick');
			
			l = decodeURI(l);
			
			ol = l;
	
			if(l.indexOf(website_root_url) == -1){
	
				
			} else {
				
				// convert to AJAX link //
				
				l1 = l.replace(website_root_url, '');
				l2 = l1.replace("document.location='", '');
				l3 = l2.replace("document.location = '", '');
				
				
				//nl = l.substr(19+website_root_url.length);
				
				nnl = l3.substr(0,(l3.length-2));
				
				var js = 'load_page("'+nnl+'");';
				// create a function from the "js" string
				var newclick = new Function(js);
				// clears onclick then sets click using jQuery
				$(this).attr('onclick', '').click(newclick);
	
	
			}
			
		}
		
	})
	
	$('blockquote').each(function(){
	
		if($(this).attr('onclick') != undefined){
			
			l = $(this).attr('onclick');
			
			l = decodeURI(l);
			
			ol = l;
	
			if(l.indexOf(website_root_url) == -1){
	
				
			} else {
				
				// convert to AJAX link //
				
				l1 = l.replace(website_root_url, '');
				l2 = l1.replace("document.location='", '');
				l3 = l2.replace("document.location = '", '');
				
				
				//nl = l.substr(19+website_root_url.length);
				
				nnl = l3.substr(0,(l3.length-2));
				
				var js = 'load_page("'+nnl+'");';
				// create a function from the "js" string
				var newclick = new Function(js);
				// clears onclick then sets click using jQuery
				$(this).attr('onclick', '').click(newclick);
	
	
			}
			
		}
		
	})
	
	$('form').each(function(){
	
			l = $(this).attr('action');
			form_id = $(this).attr('id');
			
			l = decodeURI(l);
			
			ol = l;
	
			if(l.indexOf(website_root_url) == -1){
	
				
			} else {
				
				// convert to AJAX link //
				
				l1 = l.replace(website_root_url, '');
				l2 = l1.replace("document.location='", '');
				l3 = l2.replace("document.location = '", '');
				
				
				//nl = l.substr(19+website_root_url.length);
				
				nnl = l3.substr(0,(l3.length-2));
				
				var js = 'ajax_gather_form_data("'+form_id+'"); load_page("'+l1+'"); return false;';
				// create a function from the "js" string
				var newclick = new Function(js);
				// clears onclick then sets click using jQuery
				$(this).attr('onsubmit', '').submit(newclick);
				
	
			}
		
	})
	
	$('select').each(function(){
	
			l = $(this).attr('onchange');
			
			l = decodeURI(l);
			
			ol = l;
	
			if(l.indexOf('if(this.options[this.options.selectedIndex].value){ document.location = this.options[this.options.selectedIndex].value; }') == -1){
				
				
				
			} else {
				
				
				
				// convert to AJAX link //
				
				el = $(this);
				
				nl = l.replace('if(this.options[this.options.selectedIndex].value){ document.location = this.options[this.options.selectedIndex].value; }', 'if(this.options[this.options.selectedIndex].value){ load_page(strip_domain(this.options[this.options.selectedIndex].value)); }');

				var js = 'alert("FOUND A SELECT MENU");';
				// create a function from the "js" string
				var newclick = new Function(js);
				// clears onclick then sets click using jQuery
				
				$(this).attr("onchange" , nl).change(nl);
	
			}
		
	})
	
	$("object").append(
		$("<param/>").attr({
			'name': 'wmode',
			'value': 'transparent'
		})
	).find("embed").attr('wmode', 'transparent');
	
	throw_anchor();
	
}

ajax_anchor = '';

function strip_domain(url){
	
	url = url.replace(website_root_url,'');
	
	return url;
	
}

function ajax_gather_form_data(form_id){
	
	var $inputs = $('#'+form_id+' :input');
    
	var values = {};
	
	website_requests = '';
    
	$inputs.each(function() {
        
		values[this.name] = $(this).val();
		
		website_requests += this.name+'='+$(this).val()+'&';
    
	});
	
}

function set_interface_cookie(){
	
	deleteCookie('toggle');
	
	if(document.getElementById('interfacetoggle').checked){ setCookie('toggle','ajax',1); } else { setCookie('toggle','bot',1); } 
	
}

function load_page(page){
	
	$('#pageloader').animate({scrollTop: '0'}, 500,function(){ animate_background(get_random_background_coordinates(), page); });
	
}



function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

function target_blank(url){
	
	$('#external_browser').css('display', 'none');
	
	$('#external_browser_window').attr("src" , url);
	
	$('#external_browser_window').load(function(){
												
		$('#external_browser_window').fadeIn(600);
			
	});
	
	var wh = $(window).height();
	var ww = $(window).width();
	
	$('#external_browser').css('width', ww + 'px');
	$('#external_browser').css('height', wh + 'px');
	$('#external_preloader').css('width', ww + 'px');
	$('#external_preloader').css('height', wh + 'px');
	$('#external_browser').css('display', 'block');
	$('#external_browser_close_button').css('display', 'block');

	$('#external_browser_window').css('height', (wh-80) + 'px');
	
	var w = $('#external_browser_window').width();
	
	var l = (ww/2) - (w/2);
	
	$('#external_browser_window').css('left', l + 'px');
	$('#external_browser_close_button').css('left', (l+w-26) + 'px');
	
}

function external_browser_window(){
	
	$('#external_browser_window').attr("src" , '');
	
	$('#external_browser_window').fadeOut(600,function(){ $('#external_browser_close_button').css('display', 'none'); $('#external_browser').css('display', 'none'); });
	
}

$(document).ready(function(){
						   
   $(document).mousemove(function(e){
								  
      cursor_x = e.pageX;
	  cursor_y = e.pageY;
	  
   }); 
   
})

function scroll_to_selector(id){
	
	var targetOffset = $('#'+id).offset().top;
	
	$('html,body').animate({scrollTop: targetOffset}, 500);
	
}
