var ref = '';

function ajax_request(url,data,target,cb_func){
   clearTimeout(ref);
   new Ajax.Request(url, {
      method: 'get',
      parameters: data,
      onSuccess: function(transport) {
         if(transport.responseText.match(/Redirecting you, please wait.../)){
            res = transport.responseText;
            res = res.stripTags();
            res = res.replace(/Redirecting you, please wait.../,"");
            window.location = res;
         }
         if(target){
            $(target).innerHTML = transport.responseText;
         }
         if(cb_func){
            ref = setTimeout(cb_func,2000);
         }
      }
   }
);
}



function search_site(){
   if($('keyword_search').value == 'Search BRG Here!' || $('keyword_search').value == ''){
      alert('Please enter a search phrase.');
   }else{
			   $('content').innerHTML="<div class='loading'>loading search results...</div>";
			   ajax_request('/ajax/search.php', 'keyword='+$('keyword_search').value, 'content', '');
   }
}

function saveNewsletter(){
   ajax_request('/ajax/newsletter.php',Form.serialize('newsletterForm'),'newsletterBox','');
}

function saveComment(){
   ajax_request('/ajax/comment.php',Form.serialize('commentForm'),'commentBox','');
   //$('commentForm').innerHTML = '<p>Saving Comment, Please Wait...</p>';
}

function saveQuickInquire(){
   ajax_request('/ajax/quick_inquire.php', Form.serialize('quickInquireForm'),'quickInquire','');
}

function showQuickInquire(){
//   alert($('quickInquire').style.display);
   if($('quickInquire').style.display == 'none'){
      Effect.BlindDown('quickInquire', {duration: .2});
   }else{
      Effect.BlindUp('quickInquire', {duration: .2});   
   }
}

function rebuildQuickInquire(id){
   ajax_request('/ajax/quick_inquire.php', 'rebuild=true&art_id='+id,'quickInquire','');
}

function rebuildNewsletter(){
   ajax_request('/ajax/newsletter.php','rebuild=true','newsletterBox','');
}

function myBrLogin(){
   ajax_request('/ajax/login.php',Form.serialize('myBrLoginForm'),'myBrLoginBox','');
}

function rebuildLogin(){
   ajax_request('/ajax/login.php','rebuild=true','myBrLoginBox','');
}

function myBrLogout(){
   ajax_request('/ajax/login.php','logout=true&rebuild=true','myBrLoginBox','');
}

function browsePrev(){

   if(currentThumb - 1 >= 0){
      currentThumb--;
      scrollX += browseImg[currentThumb] + 5;
      moveScroller();
   }
   
}

function browseNext(){
   
   if(currentThumb + 1 < browseImg.length){
      scrollX -= browseImg[currentThumb] + 5;      
      currentThumb++;
      moveScroller();
   }

}

function moveScroller(){
   
   if(currentThumb <= 0){
      $('bPrev').disabled = true;
   }else{
      $('bPrev').disabled = false;
   }

   if(currentThumb >= browseImg.length - 1){
      newWidth = browseImg[currentThumb] + 5;
      $('bNext').disabled = true;
   }else{
      currentThumb2 = currentThumb + 1;
      newWidth = browseImg[currentThumb] + browseImg[currentThumb2] + 10;
      $('bNext').disabled = false;
   }

   new Effect.Morph('browseFilesBox', {
      style: 'width: '+newWidth+'px',
      duration: 0.8
   });
   
   new Effect.Morph('browseScroller', {
      style: 'left: '+scrollX+'px',
      duration: 0.8
   });
   
}
