/* 
 ___    __    ____    __    _    _  ____  ____ 
/ __)  /__\  (  _ \  /__\  ( \/\/ )( ___)(  _ \
\__ \ /(__)\  )(_) )/(__)\  )    (  )__)  ) _ <
(___/(__)(__)(____/(__)(__)(__/\__)(____)(____/

CORE JS 
Developed by SADA Systems Inc.
*/

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
	//alert("did it");
}

// Preload Array 
$([
	'images/site/main_bg.jpg',
]).preload();



$(document).ready(function (){

	var animate = "1";
	
	if (isIE == "1") {
		animate = "1";
	}
	
	// Animate Inside Icons
	if (animate != "1") { // don't animate if your IE

		// Sectional Header Graphics
		var header_graphic_pricat = $(".header_graphic_pricat");  
		if ( header_graphic_pricat[0] ) {
			$(header_graphic_pricat).css("top","-250px");
			$(header_graphic_pricat).animate({top: "20px", right: "35px"}, 1200);
		}// end if

		// Inside Page Header Graphics
		var header_graphic = $(".header_graphic");  
		if ( header_graphic[0] ) {
			$(header_graphic).css("top","-250px");
			$(header_graphic).css("right","-250px");
			$(header_graphic).css("top","-250px");
			$(header_graphic).animate({top: 0 + "px", right: "10px"}, 1200);
		}  // end if
		
		// Inside Page Header Graphics
		var header_graphic = $(".header_graphic");  
		if ( header_graphic[0] ) {
			$(header_graphic).css("top","-250px");
			$(header_graphic).css("right","-250px");
			$(header_graphic).css("top","-250px");
			$(header_graphic).animate({
				top: "0",
				right: "10px",
				borderWidth: "5px"
			}, 1200, 'swing', function() {
			// oncomplete funtion
			});
		}  // end if
		

		
		// Inside Page Header Text
		var inside_intro = $("#inside_intro");  
		if ( inside_intro[0] ) {
			
			// set id's as vars 
			var highlight = $(".highlight");
			var inside_intro_h1 = $("#inside_intro>h1");
			var inside_intro_h2 = $("#inside_intro>h2");
			var inside_intro_p = $("#inside_intro p");

			// Get all heights and widths
			var txtWdth = $(inside_intro).outerWidth();
			var txtHght = $(inside_intro).outerHeight();
			
			var txtHghtH1 = $(inside_intro_h1).outerHeight();
			var txtHghtH2 = $(inside_intro_h2).outerHeight();
			var txtHghtP = $(inside_intro_p).outerHeight();
			
			var paraTop = txtHghtH1 + txtHghtH2 + 30; // top for paragraphs
			var divTop = txtHghtH1 + txtHghtH2 + txtHghtP +30; // top for divs
			
			var lftMgn = "25px"; // global left margin
				
			// Buttons
			$(highlight).css("position","absolute");
			$(highlight).css("right","-70px");
			$(highlight).css("top","370px");
			$(highlight).animate({top: divTop, left: lftMgn}, 800);

			// h1
			$(inside_intro_h1).css("position","absolute");
			$(inside_intro_h1).css("top","-70px");
			$(inside_intro_h1).animate({top: parseInt(txtHght + 20) / 4.5 + "px", left: lftMgn}, 800);
			
			// h2
			$(inside_intro_h2).css("position","absolute");
			$(inside_intro_h2).css("left","-70px");
			$(inside_intro_h2).animate({top: parseInt(txtHght) / 10 + "px", left: lftMgn}, 800);
			
			//p
			$(inside_intro_p).css("left","-200px");
			$(inside_intro_p).css("position","absolute");
			$(inside_intro_p).animate({top: paraTop, left: lftMgn}, 800);

		}  // end if

		// Video Player
		var player_wrap = $(".player_wrap");  
		if ( player_wrap[0] ) {
			
			// Video Players
			$(player_wrap).css("position","absolute");
			$(player_wrap).css("right","-70px");
			$(player_wrap).css("bottom","-70px");
			$(player_wrap).animate({top: 50, right: 50}, 800);
			
		}  // end if
		
		// Animate in All body elements

		//$("#inside_intro").css("display","none");
		//$("#inside_intro").fadeIn(800);	
	
		//$(".subnavBg").css("display","none");
		//$(".subnavBg").fadeIn(1000);
	
		//$(".contentBg").css("display","none");
		//$(".contentBg").fadeIn(1200);
		
		//$("#breadcrumbs").css("display","none");
		//$("#breadcrumbs").fadeIn(2700);	
		
	} else if (isIE == "1"){
		// do nothing yet
		//alert("I AM IE HEAR ME ROAR");
	};// end if ie
	
	// Homepage Buttons
	$("#rbgb_transform").click(function(){
		window.location = "transform-my-organization-main.php"
	});
	$("#rbgb_itsolutions").click(function(){
		window.location = "it-solutions.php"
	});
	$("#rbgb_cloudsolutions").click(function(){
		window.location = "cloud-solutions.php"
	});

	// Modals 
	$('a[name=modal]').click(function(e) {
		e.preventDefault();

		var id = $(this).attr('href');

		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		$('#mask').css({'width':maskWidth,'height':maskHeight});

		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	

		var winH = $(window).height();
		var winW = $(window).width();

		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
		$(id).fadeIn(1000);
	
	});
	
	$('.window .close').click(function (e) {
		e.preventDefault();
		$('.window').fadeOut(1000, function() {
   		   $('.window').hide();
   		});
		$('#mask').fadeOut(1000, function() {
   		   $('#mask').hide();
   		});
	});		

	$('#mask').click(function () {
		$('.window').fadeOut(1000, function() {
   		   $('.window').hide();
   		});
		$(this).fadeOut(1000, function() {
   		   $(this).hide();
   		});
	});
	
	// Tooltips
	$(".toolt").click(function(){
		var targetid = "#bble_cont_" + $(this).attr("id");
		$(targetid).slideToggle("fast");
	});	
	
	// Videos (chromebooks)
	$("video_cont_0").show(); // init the first video
	$(".vdo_item").click(function(){
		var targetid = "#video_cont_" + $(this).attr("id");
		for (e=0;e<=8;e++)
		{
			$("#video_cont_" + e).fadeOut("fast");
			//alert("video_cont_" + e);
		}
		$(targetid).fadeIn("slow");;
		$('body,html').animate({scrollTop: 0}, 800);
	});
	
	$("#totop").click(function(){
		$('body,html').animate({scrollTop: 0}, 800);
	});
	
	// Deal with HTML5 Placeholder elements for other browsers
	$("[placeholder]").focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr("placeholder")) {
		input.val("");
		input.removeClass("placeholder");
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == "" || input.val() == input.attr("placeholder")) {
		input.addClass("placeholder");
		input.val(input.attr("placeholder"));
	  }
	}).blur();

	// social buttons display toggle
	$("#sharethispage_btn").click(function(){
		$("#social").slideToggle('slow');
	});
	
	// Primary Catergory Hover Boxes

	// fall back link function for fast clickers 
	$(".pricat_boxes").click(function() {
		var eLink = $(this).closest("a").attr("href");
		window.location = eLink;
	});
	
	$(".pricat_in_wrap").css("top","180px"); // set init state

	$(".pricat_boxes").hover(function() {
		$(".pricat_in_wrap", this).stop().animate({"top": "10px"}, "fast");
		}, function() {
			$(".pricat_in_wrap", this).stop().animate({"top": "180px"}, "slow");
	});
	
	// Accordian Content
	$("div.acc_content").hide(); // set init state
	$('.acc_link').click(function() {
		$('.acc_link').removeClass('accon');
	 	$('.acc_content').slideUp(800);
		if($(this).next().is(':hidden') == true) {
			$(this).addClass('accon');
			$(this).next().slideDown(800);
		 }
	 });
	
	// Simple Content Expand
	$(".simple_expand").click(function(){
		var targetid = $(this).attr("name");
		$('#' + targetid).slideToggle("fast");
	});
	
	// SmoothScroll on Anchors
	$(".doitsmooth").click(function(e){
		e.preventDefault();
		var id = $(this).attr("href");
		id = id.substring(1);
		var position = $("#" + id).position();
		$("body,html").animate({scrollTop: position.top}, 1000);
	});
	
	
	// Highlight Elements
	/*
	$(".highlight").click(function(){
		var id = $(this).attr("title");
		var scrolltarget = $("#scrolltarget");
		var position = scrolltarget.position();
		$("body,html").animate({scrollTop: position.top}, 1000);
		$("#" + id).css("background-color","#FFFFFF");
		$("#" + id).css("border","1px solid #666");
		$("#" + id).animate({
			padding: "20px"
		}, 800);
	});
	*/
	

	// Check to see if a form is on the page, if so, hide it and assign handlers
	var form_pg = $("#form_pg");
		
	if ( form_pg[0] ) {	
		$(form_pg).css("display","none");
		$("body").append("<div id=\"fMask\"></div>");
		
		// Assign Click handler for adBox
		$('#formAdBox').click(function() {
			$().openCalltoaction();
		});
		
		// Assign Click handler for .highlight
		$('.highlight').click(function() {
			$().openCalltoaction();
		});
		
		// Assign Click handler for adBox
		$('#fMask').click(function() {
			$(this).closeCalltoaction();
		});
		
	}  // end if
	
	jQuery.fn.openCalltoaction = function(target) {
		
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		var boxWidth = 860;
		var frmHeight = form_pg.outerHeight();
		//var boxWidth = $(window).width() - 200px; // adjust for screen res
		
		$("body,html").animate({scrollTop: 0}, 600);

		$(form_pg).css({
			'position':'absolute', 
			'top':'-' + frmHeight + 'px',
			'left':((maskWidth / 2) - (boxWidth / 2)), 
			'opacity':'0', 
			'z-index':'9999999', 
			'background-color':'#fff',
			'width':boxWidth + 'px'
		});
		$(form_pg).slideDown(500);
		$(form_pg).animate({top: 0, opacity:1}, 800);
		
		
		$('#fMask').css({'width':maskWidth,'height':maskHeight,'background-color':'#000', 'position':'absolute', 'top':'0', 'z-index':'999999', 'display':'none'});
		$('#fMask').fadeIn(1000);	
		$('#fMask').fadeTo("slow",0.8);
		
	};// openCalltoaction

	jQuery.fn.closeCalltoaction = function(target) {
		$(form_pg).fadeOut(1000, function() {
		   $(form_pg).hide();
		});
		$(this).fadeOut(1000, function() {
		   $(this).hide();
		});
	};// closeCalltoaction

	// Form Follower
	jQuery.fn.followtheLights = function(target) {
		var obj = $(this);
		obj.css('position', 'absolute');
		function moveDiv() {
		  topPosition = $(window).scrollTop();
		  topInt = parseInt(topPosition);
			if (topInt >= initPos_ab_y) {
				obj.stop().animate({top:(topPosition + 20)}, 480, 'swing');
			} else {
				obj.stop().animate({top:initPos_ab_y}, 480, 'swing');
			}
		}
		$(window).scroll(function() {
		  moveDiv();
		});
		$(window).resize(function() {
		  moveDiv();
		});
	};// followtheLights
	
	// Set up follower functions	
	var formAdBox = $("#formAdBox");  
	if ( formAdBox[0] ) {
		var initPos_ab = $('#formAdBox').position();
		var initPos_ab_y = initPos_ab.top;
		$('#formAdBox').followtheLights();
	}  // end if
	
	// For the Forms
	$('#fieldOther').change(function() {	  
		if ($(this).val() == "other") {
			$('#frm_other').fadeIn('slow');
		} else {
			$('#frm_other').fadeOut('slow');
		}
	});

}); // onload



