﻿/*
 *  MATT STEINER - 2/20/09
 *  MAIN HTTP://WWW.IBIDATA.COM JAVASCRIPT FILE
 *  CONTAINS ALL MAJOR SITE FUNCTIONS
 */
 
 
////  SAVINGS CLOCK SETTINGS  ///////////////////////////////////
//                                                           ///
    var enddate = new Date();
    var endamount = 0;
    var speed = 6.3; //higher is slower
    var cnt = "";
    var imgurl = 'assets/images/numbers/odometer/';
    var loop = true;
//                                                   ///
////  OTHER VARIABLES  ////////////////////////////////
                                                   ///
//var url = 'http://dev.ibidata.net/ibidatacom/';
var url = 'http://www.ibidata.com/';
var ne = false;
var dteNow = new Date();
                                              ///
///////  BEGIN ON DOM READY  ///////////////////////////////////////////////////////////////////////
                                                                                                ///
$(document).ready(function(){
///* HIT COUNTER *///
    $.get(url+'assets/script/script.aspx', {x:'-1', y:'-1', mode:"coordinates", browser:$.browser.className, os:$.os.name, res:screen.width+"x"+screen.height, googlesearch:google(), referrer:document.referrer});
///* SERVER TIME *///
    if($(".gettime").length){
        getservertime();
    }
///* MONEY COUNTER *///
    if($("#count").length){
        $.getJSON('assets/script/script.aspx', {mode:"savingsclock"}, function(json) {
	       endamount = json.total;
	       enddate = new Date(json.year,(json.month) - 1,json.day); //Month is 0-11 (y,m,d)
           count();
        });
    }
///* NAV OPACITY FADE *///
		$("#nav > ul li").animate({opacity:0.8}, 500)
			.hover(function(){
				$(this).animate({opacity:1}, 250);
			}, function(){
				$(this).animate({opacity:0.8}, 250);
		});
///* COPYRIGHT DATE *///
	    $("#year").text(dteNow.getFullYear());

///* MOUSE CLICKS *///
	    $("body").click(function(event){
	    	$.get(url+'assets/script/script.aspx', {x:event.pageX, y:event.pageY, mode:"coordinates", browser:$.browser.className, os:$.os.name, res:screen.width+"x"+screen.height, googlesearch:"", referrer:""})
	    });

///* TALK LIVE NOW *///
	    $(".talkLiveNow").click(function(event){
	    	window.open(url+"clicktotalk.aspx?&referral="+location.href,"_blank","height=500,width=580,left=20,top=20,resizable=no,menubar=0,toolbar=0,status=0");
		    event.preventDefault();
		});

///* EXTENSION NOTIFICATION *///
	$(".notifyExtension").focus(function(){
		if(!ne){
			alert("Enter extension for auto attendant only! For live operator please leave blank.");
			ne=true;
		}
	});
});
                                                                                                      ///
//////////  BEGIN FUNCTION LIST  ///////////////////////////////////////////////////////////////////////
                                                                                                    ///
function getservertime(){//RETURN THE TIME ON THE SERVER TO MAKE SURE YOU GET THE BUSINESS TIME
    $.get(url+'assets/script/script.aspx', {mode:"servertime"}, function(sertime){
        $(".gettime").text(sertime);
    });
    setTimeout("getservertime()",1000);
}

function google(){//FOR SAVING GOOGLE SEARCHES
    r = document.referrer.toString();
    r = r.split("/");
    if(r[2] == "www.google.com"){
        var text = document.referrer.toString()
        text2 = text.split("q=");
        text3 = text2[1].split("&",1);
        return text3;
    }else{
        return "";
    }
}

function count() {// FOR MONEY CLOCK
	var today = new Date();
	var total = (enddate.getTime() - today.getTime())/(speed * 10000);
    if((endamount - total).toFixed(2) > endamount){
        text = '$' + addCommas(endamount);
        number = endamount;
	}else{
		text = '$' + addCommas((endamount - total).toFixed(2));
		number = (endamount - total).toFixed(2);
	}
	cnt = loop == true ? '<div style="width: 259px; height:250px; padding-left:8px"><img id="cntclock" alt="clock" src="assets/images/moneyClock/1.gif" style="padding:5px; width:234px; height:235px; border:0" /></div>':'';
	for(i = 0; i < text.length; i++){
		if(text.split("")[i] == "."){
			if(loop == true){
				cnt = cnt + "<img src=\""+ imgurl + "[.].gif\"alt =\"\" id=\"cnt" + i +"\" \/>";
			}
		}else{
			if(loop == true){
				cnt = cnt + "<img src=\""+ imgurl + text.split("")[i] +".gif\"alt =\"\" id=\"cnt"+i+"\" \/>";
			}else{
			    if(document.getElementById("cnt"+i).src != imgurl + text.split("")[i] +".gif"){
				    document.getElementById("cnt"+i).src = imgurl + text.split("")[i] +".gif";
			    }
                var inc = [0, 15625000.00, 16666666.67, 17708333.34, 18750000.00, 19791666.67, 20833333.33, 21875000.00, 22916666.67, 23958333.34, 25000000.00];
                for(var ii=0; ii<inc.length; ii++){
                   if(number > inc[ii]){
						if(document.getElementById("cntclock").src != "assets/images/moneyClock/"+ (ii + 1) +".gif"){
							document.getElementById("cntclock").src = "assets/images/moneyClock/"+ (ii + 1) +".gif";
						}
					}
				}
			}
		}
	}
	if(loop == true){
		document.getElementById("count").innerHTML = cnt;
		clockeffects();
		loop = false;
	}
	if((endamount - total).toFixed(2) < endamount){
		setTimeout("count()", 783);
	}
}

function addCommas(nStr){//ADDS COMMAS TO STRING {EX. ##,###,###.00)
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function clockeffects(){
    $('#cntclock').animate({opacity:0.75});
    $('#count, #clocklink').mouseover( function(){
        $('#cntclock').stop().animate({width:244, height:245, padding:0, opacity:1});
    })
    $('#count, #clocklink').mouseout( function(){
        $('#cntclock').stop().animate({width:234, height:235, padding:5, opacity:0.75});
    })
    $('#count').click(function (){
        tb_show('How do we save our clients so much money on payments?','assets/html/savings.html?keepThis=true&amp;TB_iframe=true&amp;height=400&amp;width=765');
    });
}
                                                                                 ///
///////////////////////////////////////////////////////////////////////////////////