/**
 * CoffeeCup Software's Empire: Main Javascript Library
 *
 * This file contains main javascript library for the
 * empire application.
 *
 * @author     Jeff Welch <jw@coffeecup.com>
 * @category   Empire
 * @package    Public
 * @copyright  Copyright (c) 2006-2007 CoffeeCup Software, Inc. (http://www.coffeecup.com/)
 * @version    $Id$
 */

$(document).ready(function() {

    // Makes external anchor tags open links in new windows
    $('a[href^="http://"][class!="colorbox"][class!="colorbox_iframe"][class!="colorbox_flash"][class!="stay"]')
       .not('[href*="' + document.domain + '"]').not('[href*="recaptcha.net"]').addClass('external').
         click(function() { window.open(this.href); return false; });

    // Login links open in new windows
    $('a.login').click(function(){ window.open(this.href); return false; });

    // Logout link should submit logout form so that logout uses POST and isn't open to CSRF
    $('a.logout_link').click(function(){ $('#logout_form').submit(); return false; });

    // Allow for toggling billing address on and off
    if($('input#same_as_billing').attr('checked')){$('div#billing_address_toggler').hide()}
    $('input#same_as_billing').click(function(){$('div#billing_address_toggler').slideToggle('fast')});

    // Setup the jtageditors
    if($.fn.jTagEditor){$('textarea.jTagEditor').jTagEditor();}
    // Setup the list sorters
    if($.fn.ccListSorter){$('ul.sortable').ccListSorter();}
    // Setup the syncers
    if($.fn.ccSync){$('input#sync').ccSync();}
    // Setup the long uploads
    if($.fn.ccLongUpload){$('input.long_upload').ccLongUpload();}
    // Setup the waiting processor
     if($.fn.ccLongProcess){$('input.long_process').ccLongProcess();}
    // Setup slug builder
    if($.fn.ccSlugBuilder){$('form.slug_builder').ccSlugBuilder();}
    // Setup tag selector
    if($.fn.ccTagSelector){$('form.tag_selector').ccTagSelector();}
    // Setup star rater
    if($.fn.rating){$('input[type=radio].star').rating();}
    // Setup formsaver
    if($.fn.ccFormSaver){$('form.formsaver').ccFormSaver();}

    // for zebra striping table rows
    $('table.zebra tr:odd').addClass('ying');
    $('table.zebra tr:even').addClass('yang');

    //fancy trial download email box
    if($('input#trial_download_email'))
    {
       $('input#trial_download_email').focus(function(){if($('input#trial_download_email').val() == 'you@domain.com') $('input#trial_download_email').val('');});
       $('input#trial_download_email').blur(function(){if($('input#trial_download_email').val() == '') $('input#trial_download_email').val('you@domain.com');});
    }

     /*
    $('.dataTable tr').mouseover(function(){
        $(this).addClass('hover');
        return false;
    });

    $('.dataTable tr').mouseout(function(){
        $(this).removeClass('hover');
        return false;
    });
    */

    // hide disclaimer notes by default
    $('#disclaimer').hide();
    $('.form_note a[href=#disclaimer]').click(function(){
       $('#disclaimer').toggle();
       return false;
    });

    $('#contactUs select').change(function(){
       $(this).find('option:selected').each(function () {
          if($(this).val() == '3')
          {
             $('#contact_message').hide();
             $('#button_sendmymessage').hide();
             $('#user_info').hide();
             $('#support_details').show('fast');
          }
          else
          {
             $('#support_details').hide();
             $('#contact_message').show('normal');
             $('#user_info').show();
             $('#button_sendmymessage').show();
          }
      }).change()
    });

    // Open max-width'ed images in colorbox
    $('div.postcontents img').each(function(){

       if($.browser.msie && parseInt($.browser.version) <= 6 && $(this).width() > 520)
       {
          $(this).css('width','520px');
       }

       if($(this).width() == '520')
       {
          $(this).wrap('<a class="expandable_image colorbox" href="' + $(this).attr('src') + '"></a>');
       }
    });

    // hides the text 'add to cart' on software product home pages
    if($('.primary #addbutton')) {
       $('ul.primary #addbutton').attr("value","");
    }


    // for toggling FAQ's on /contact/
    $('#faqnotice').append("<br /><br /><b id=\"faqtoggle\" style=\"font-size:1.2em;color:#c00;text-decoration:underline;cursor:pointer\">Save some time &mdash; Check out the FAQ</b>");
    $('#contactfaqs').hide();
    $('#contactfaqs p').hide();
    $('#contactfaqs b').css({cursor:"pointer", color:"#008", textDecoration:"underline"});
    $('#faqtoggle').click(function() {
       $('#contactfaqs').slideToggle('slow');
    });
    $('#contactfaqs b').click(function() {
       $(this).parent().find('p').toggle();
       $(this).css({color:"#333", textDecoration:"none"})
    });

	 if($.fn.colorbox){
        if($('.colorbox_option').length > 0)
        {
           $('.colorbox_option').each(function(){
              $(this).before('<a title="' + $(this).attr('title') + '" class="colorbox_iframe_added" href="' + $(this).attr('href') + '?colorbox_preview=true"><img src="/images/icons/page_white_magnify.png" /></a>');
           });
        }

        $("a.colorbox").colorbox({width:"75%", height:"75%"});
        $("a.colorbox_iframe, a.colorbox_iframe_added").colorbox({width:"75%", height:"75%", iframe:true});
        $("a.colorbox_flash").colorbox({width:'700px', height:'470px'});
    }

});
