var postXHR = '';

/* clear any user notifications */
function clearNotices() {
    $('.notice').each(function(i) {
        $(this).fadeOut('fast');
    });
}

$(document).ready(function() {
    /* make sure notices get cleared */
    if ($('.notice').length) {
        setTimeout("clearNotices()", 10000);
    }

    
    if ( $('#contactForm').length ) {
        $('#contactForm input[type=text]').each(function() {
            $(this).focus(function(){
                var re = new RegExp('(Your).*?(Here)',["i"]);
                if ( $(this).val().match(re) ) {
                    $(this).val('');
                }
            });
        });
    }
    var calcTimer;
    /* Calculate the Potential Funds Raised */
    function calcFunds() {
        var pId,tId,sId;
        if ( $('#prodPid').length ) {
            pId = $('#prodPid').val();
        }
        if ( $('#prodTid').length ) {
            tId = $('#prodTid').val();
        }
        if ( $('#prodSid').length ) {
            sId = $('#prodSid').val();
        }
        var qty = $('#prodQty').val();
        var rrp = $('#prodRrp').val();
        
        if ( parseInt(qty) != 0 && parseInt(rrp) != 0 ) {
            /* Call the backend to do the calculation taking into account qty breaks etc */
            postXHR = $.post(
                "/pricing",
                { 'pQty': qty, 'pPid': pId, 'pTid': tId, 'pSid': sId, 'pRrp': rrp },
                function(data) {
                    if ( data.status == 1 ) {
                        if ( data.qtyUsed != qty ) {
                            $('#prodQty').attr('disabled','disabled');
                            $('#prodQty').val('MIN:'+data.qtyUsed);
                            $('#prodQty').css({'color':'#f00','font-weight':'bold'});
                            var changeBack = function () { 
                                $('#prodQty').css({'color':'#000','font-weight':'normal'});
                                $('#prodQty').val(data.qtyUsed);
                                $('#prodQty').attr('disabled','');
    
                            };
    
                            setTimeout( function() { changeBack(); }, 1000);
                            
                        }
                        $('#prodFunds span').html(parseFloat(data.totalFunds).toFixed(2));
                    }
                },
                "json"
            );
        }
    }

    /* Give fund raising calculators dynamic pricing functionality */  
    if ( $('#hpCalc').length ) {
        function setTherm() {
            var code = $('#prodTid option:selected').attr('title').split('|');
            var profHeight = code[1];
            var rrp = code[2];
            code = code[0];
            $('#prodImg').attr('src', '/images/templates/small/'+code+'.jpg' );
            var newHeight = ( (( 218 / 5 ) * profHeight ));
            $('#heat').animate({ height: newHeight });
            $('#prodRrp').val(rrp);
        }
        
        $('#prodTid').bind('change', function() {
            /* clear the timer call (stops multiple request in a short space of time) */
            clearTimeout(calcTimer);
            
            /* kill the last request if its still running */
            if ( postXHR != '' ) {
                postXHR.abort();
                postXHR = '';
            }
            /* set the timer to do the calculation in 200th of a second */
            calcTimer = setTimeout( function() { calcFunds(); }, 200 );
            
            setTherm();
        });
        
        var curQty, curRrp;
        $('#prodRrp, #prodQty').keydown(function() {
            curQty = $('#prodQty').val();
            curRrp = $('#prodRrp').val();
        });
        
        /* When the qty or rrp are changed */
        $('#prodRrp, #prodQty').keyup(function() {
            
            if ( curQty != $('#prodQty').val() || curRrp != $('#prodRrp').val() ) { 
                /* clear the timer call (stops multiple request in a short space of time) */
                clearTimeout(calcTimer);
                /* set the timer to do the calculation in 200th of a second */
                calcTimer = setTimeout( function() { calcFunds(); }, 500 );
            }
        });
        
        calcFunds();
        setTimeout( function(){ setTherm();}, 500);
    }
    if ( $('#loginNow').length ) {
        $('#loginNow input[type=text]').focus(function() { 
            $('#loginNow input[type=text]').attr('value',''); 
            $('#loginNow input[type=text]').unbind(); 
        });
        $('#loginNow input[type=password]').focus(function() { 
            $('#loginNow input[type=password]').css('background','#fff'); 
            $('#loginNow input[type=password]').unbind(); 
        });
    }
    if ( $('#registerNow').length ) {
        $('#registerNow input[type=text]').focus(function() { 
            $('#registerNow input[type=text]').attr('value',''); 
            $('#registerNow input[type=text]').unbind(); 
        });
    }

    if ( $('#vidPH').length  ) {
        var vidStr = '<object width="392" height="294"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=9886384&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=9886384&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="392" height="294"></embed></object>';
        $('#vidPH').replaceWith(vidStr);
    }
    
    if ( $('#contactForm').length ) {
        $('#contact_contactMe').click(function () {
            if ( $('#contact_contactMe').is(':checked') ) {
                $('#contact_contactMethod').parent().show();
            } else {
                $('#contact_contactMethod').parent().hide();
            }
        });
        if ( !$('#contact_contactMe').is(':checked') ) { 
            $('#contact_contactMethod').parent().hide();
        }
        
        $('#contact_contactMethod').click(function() { 
            if ( $(this).val() == 1 ) { // 1==Phone
                $('#contact_phone').parent().show();
            } else {
                $('#contact_phone').parent().hide();
            }
        });
        if ( $('#contact_phone').val() == '' ) { 
            $('#contact_phone').parent().hide();
        }
    }
    if ( $('#fomWrap').length ) {
        $('#fomWrap').cycle({
            fx : 'scrollHorz',
            speed : 1000,
            timeout : 0,
            startingSlide: 0
        });
        $('#fomNav a').each(function(i) { 
            $(this).click(function() {
                $('#fomWrap').cycle(i); 
                return false; 
            }); 
        });
    }
});
