/* Javascript */

var timer = 0;
var timer2 = 0;
window.onload=ready; 

jQuery(document).ready(function() {
    jQuery('.jcarousel-skin-tango').jcarousel({
        });
});

function ready() {
    document.getElementById('placeholder').style.display = 'none';
    var feat = document.getElementById('featured');
    feat.style.visibility = 'visible';
}

function startAjaxSpinner(){
    //hide the form and display a loading message
    $('form#collection').hide();
    var loadMsg = '<p class="loadMsg"> </p><p class="loadMsg" style="font-size:30px;padding-left:30px;">Please Wait, Loading... &nbsp; <img src="/concepts/content/template/default/images/blue-ajax-spinner.gif"></p><p class="loadMsg"> </p>';
    $('div#floater p').filter(":first").append(loadMsg);
}
function stopAjaxSpinner(){
    //show the form/ hide the load message
    $('form#collection').show();
    $('p.loadMsg').hide();
}
function collect() {
    startAjaxSpinner();
    //add user data in our system with ajax
    var form = document.collection;
    var name = encodeURIComponent(form.name.value);
    var email = encodeURIComponent(form.email.value);
    var business = encodeURIComponent(form.business.value);
    var phone1 = encodeURIComponent(form.phone1.value);
    var phone2 = encodeURIComponent(form.phone2.value);
    var phone3 = encodeURIComponent(form.phone3.value);
    //var act = encodeURIComponent(form.account.value);
    var cmd = encodeURIComponent(form.cmd.value);
    var affiliate = encodeURIComponent(form.affiliate.value);
    var src = encodeURIComponent(form.src.value);
    var vars = 'name='+name+'&email='+email+'&business='+business+'&phone1='+phone1+'&phone2='+phone2+'&phone3='+phone3+'&cmd='+cmd+'&affiliate='+affiliate+'&src='+src;
    //var ajax = new ajaxObject('http://www.owpools.com/concepts/'+act+'/collect.php', alertResponse);
    /* var ajax = new ajaxObject('http://www.owpools.com/concepts/collect.php', alertResponse);
	ajax.update(vars,'POST'); */
	
    jQuery.ajax({
        type: "POST",
        url: "/concepts/collect.php",
        data: jQuery('#collection').serialize(),
        success: function(msg){
            if (parseInt(msg) > 0) {
                jQuery('#floater').addClass('hidden');
                jQuery('#cover').addClass('hidden');
                var links = document.getElementById('showing').getElementsByTagName('a');
                for (i=0;i<links.length;i++) {
                    links[i].href += '&uni='+msg;
                }
                timer=setTimeout('prompt2()',45000);
            } else {
                stopAjaxSpinner();
                alert(msg);
            }
        }
    });
}


function alertResponse(responseText, responseStatus, responseXML) {
    if (responseStatus==200) {
        if (parseInt(responseText) > 0) {
            document.getElementById('floater').className = 'hidden';
            document.getElementById('cover').className = 'hidden';
            var links = document.getElementById('showing').getElementsByTagName('a');
            for (i=0;i<links.length;i++) {
                links[i].href += '&uni='+responseText;
            }
            timer=setTimeout('prompt()',45000);
        } else {
            alert(responseText);
        }
    } else {
        alert(responseStatus + ' -- Error Processing Request');
    }
}


function showcat(id,name) {
    var showing = document.getElementById('showing');
    var cats = showing.getElementsByClassName('prototypes','div');
    var cat = document.getElementById('cat'+id);
    for (i=0;i<cats.length;i++) {
        cats[i].style.visibility = 'hidden';
    }
    cat.style.visibility = 'visible';
    document.getElementById('title').innerHTML = name;
}

function startTimer() {
    addEvent(document.getElementById('lbClose'),'click',clearTimer);
}
function prompt() {
    document.getElementById('floater2').className = '';
}

function prompt2() {
    jQuery('floater2').removeClass('hidden');
}

function clearTimer() {
}


/*
 * Ajax Engine
 * http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object
 */

function ajaxObject(url, callbackFunction) {
    var that=this;      
    this.updating = false;
    this.abort = function() {
        if (that.updating) {
            that.updating=false;
            that.AJAX.abort();
            that.AJAX=null;
        }
    }
    this.update = function(passData,postMethod) { 
        if (that.updating) {
            return false;
        }
        that.AJAX = null;                          
        if (window.XMLHttpRequest) {              
            that.AJAX=new XMLHttpRequest();              
        } else {                                  
            that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
        }                                             
        if (that.AJAX==null) {                             
            return false;                               
        } else {
            that.AJAX.onreadystatechange = function() {  
                if (that.AJAX.readyState==4) {             
                    that.updating=false;                
                    that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
                    that.AJAX=null;                                         
                }                                                      
            }                                                        
            that.updating = new Date();                              
            if (/post/i.test(postMethod)) {
                var uri=urlCall+'?'+that.updating.getTime();
                that.AJAX.open("POST", uri, true);
                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                that.AJAX.setRequestHeader("Content-Length", passData.length);
                that.AJAX.send(passData);
            } else {
                var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
                that.AJAX.open("GET", uri, true);                             
                that.AJAX.send(null);                                         
            }              
            return true;                                             
        }                                                                           
    }
    var urlCall = url;        
    this.callback = callbackFunction || function () { };
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
	http://www.robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
*/
var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
        getElementsByClassName = function (className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
            nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
            returnElements = [],
            current;
            for(var i=0, il=elements.length; i<il; i+=1){
                current = elements[i];
                if(!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
            classesToCheck = "",
            xhtmlNamespace = "http://www.w3.org/1999/xhtml",
            namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
            returnElements = [],
            elements,
            node;
            for(var j=0, jl=classes.length; j<jl; j+=1){
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try	{
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
            classesToCheck = [],
            elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
            current,
            returnElements = [],
            match;
            for(var k=0, kl=classes.length; k<kl; k+=1){
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for(var l=0, ll=elements.length; l<ll; l+=1){
                current = elements[l];
                match = false;
                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};

/*http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html*/
function addEvent(obj, evType, fn, useCapture){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        alert("Handler could not be attached");
    }
}

