// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// used to make external URL's open in a new window and also be XHTML valid
function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors .length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
      anchor.target = "_blank";
      anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
      anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
    }
  }
}
window.onload = externalLinks;

function publishContent(frm, actn) {
        prev_actn = frm.action;
        frm.target = "_self";
        frm.action = actn;
        frm.submit();
        frm.target = "";

        // reset action once window has been opened
        frm.action = prev_actn;
}

function toggle_and_save(element) {
  element = $(element);
  element.toggle();
  document.cookie = element.id + '-open=' + element.visible() + '; path=/';
}

function widgets_checkall(frm) {
//    var form = $('edit_widget_form');
//    all_checkboxes = form.getInputs('checkbox');

    var all_checkboxes = [];
    all_checkboxes = $$('input').each(function(e){ if(e.type == 'checkbox') all_checkboxes.push(e) });

    all_checkbox = $('all_pages');
    elements = all_checkboxes.reject(function(n) { return n.getAttribute('id') == 'all_pages' })

    if (all_checkbox.checked) {
        elements.invoke('setAttribute', 'checked', true);
        $$('.placement').invoke('removeAttribute', 'disabled');
    }
    else {
        elements.invoke('removeAttribute', 'checked');
        $$('.placement').invoke('setAttribute', 'disabled', 'disabled');
    }
}

function mark_for_destroy(element, hide) {
    $(element).next('.should_destroy').value = 1;
    $(element).up('.' + hide).hide();
}

function toggle_disable(elem) {
  elem = $(elem);
  if (elem.getAttribute('disabled')) {
    elem.removeAttribute('disabled');
  }
  else {
    elem.setAttribute('disabled', true);
  }
}

function toggleDetailedDescriptionLink(ddId, ddIdToggle) {
   if (ddId.style.display == "none") {
       ddIdToggle.innerHTML = "Click here to view detailed information about this product";
   } else {
      ddIdToggle.innerHTML = "hide";
   }
}

function toggleNewsLink(newsId, newsIdToggle) {
        if (newsId.style.display == "none") {
                 newsIdToggle.innerHTML = "Click here to read the whole story";
        } else {
                newsIdToggle.innerHTML = "Hide";
        }
}

function toggle_page_image_uploads() {
        input = document.getElementById('page_layout_id');

        // we will be evaluating layouts on their ID - not ideal
        // 1 => homepage
        // 2 => corp landing page
        // 3 => corp internal page

        homepage_div = document.getElementById('homepage_image_upload');
        landing_div = document.getElementById('landing_image_upload');
        internal_div = document.getElementById('internal_image_upload');

        switch (input.value) {
                case "1":
                        // homepage layout, display homepage upload fields
                        if (homepage_div.style.display == "none") {
                                Effect.BlindDown(homepage_div, { duration: 0.5 });
                        }
                        if (landing_div.style.display != "none") {
                                Effect.BlindUp(landing_div, { duration: 0.5 });
                        }
                        if (internal_div.style.display != "none") {
                                Effect.BlindUp(internal_div, { duration: 0.5 });
                        }
                        return false;
                        break;

                case "2":
                case "4":
                        // landing page layout, display landing page upload fields
                        if (homepage_div.style.display != "none") {
                                Effect.BlindUp(homepage_div, { duration: 0.5 });
                        }
                        if (landing_div.style.display == "none") {
                                Effect.BlindDown(landing_div, { duration: 0.5 });
                        }
                        if (internal_div.style.display != "none") {
                                Effect.BlindUp(internal_div, { duration: 0.5 });
                        }
                        return false;
                        break;

                case "3":
                case "5":
                case "6":
                        // internal page layout, display internal page upload fields
                        if (homepage_div.style.display != "none") {
                                Effect.BlindUp(homepage_div, { duration: 0.5 });
                        }
                        if (landing_div.style.display != "none") {
                                Effect.BlindUp(landing_div, { duration: 0.5 });
                        }
                        if (internal_div.style.display == "none") {
                                Effect.BlindDown(internal_div, { duration: 0.5 });
                        }
                        return false;
                        break;

                default:
                        // hide all upload fields
                        if (homepage_div.style.display != "none") {
                                Effect.BlindUp(homepage_div, { duration: 0.5 });
                        }
                        if (landing_div.style.display != "none") {
                                Effect.BlindUp(landing_div, { duration: 0.5 });
                        }
                        if (internal_div.style.display == "none") {
                                Effect.BlindUp(internal_div, { duration: 0.5 });
                        }
                        return false;
                        break;
        }
}

// Globals
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;

function toggleRateType(v) {
    annual = $('annual');
    hourly = $('hourly');
    switch(v.value) {

    case 'any':
        hourly.hide();
        annual.hide();
        break;

    case 'annual':
        hourly.hide();
        annual.show();
        break;

    case 'hourly':
        annual.hide();
        hourly.show();
        break;
    }
}

function fontSize(v) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        var body_style = document.styleSheets[0].rules[0];

        switch(v) {
            case 'increase':
                document.styleSheets[0].rules[0].style.fontSize = '130%';
//                document.styleSheets[0].rules[0].style.setProperty('font-size', '130%', 'important');
                break;

            case 'decrease':
                document.styleSheets[0].rules[0].style.fontSize = '100%';
//                document.styleSheets[0].rules[0].style.setProperty('font-size', '100%', 'important');
                break;
        }
    } else {
        var body_style = document.styleSheets[0].cssRules[0];

        switch(v) {
            case 'increase':
                document.styleSheets[0].cssRules[0].style.setProperty('font-size', '130%', 'important');
                break;

            case 'decrease':
                document.styleSheets[0].cssRules[0].style.setProperty('font-size', '100%', 'important');
                break;
        }
    }
}

function textOnly() {
    if( document.styleSheets ) {
        var stylesheets = document.styleSheets;
        //DOM stylesheets are available
        for (var i in stylesheets) {
            stylesheets[i].disabled = true;
        }
    }
}

function clearSearchText() {
    search_field = document.getElementById('search_term');

    if (search_field.value == "Search") {
        search_field.value = "";
    }
}