window.onload = function(){

  //if (typeof(window.jQuery)=="undefined"){
    jQuery(document).ready(function(){

      // IE 7
      if($.browser.msie && $.browser.version < 9){
        xpr_navgation_overrides();
        xpr_form_overrides();
      }
      xpr_data_explorer();
      // SAFARI CHROME FIREFOX IE
      xpr_element_overrides();
      xpr_top_navigation();
      //xpr_dropdown();
    });
  //}
};

function xpr_dropdown(){
  $("select#dataTableSearchField").msDropDown().data("dd");
}


// ELMENT EDITOR

function xpr_top_navigation(){
  if($('h3.sectionTitle ~ .xprNavigation').length == 0 && $('.xprPaginationWrapper').length > 0){
    $('#xprBodyContent').addClass('xprBodyContentPadding');
  }
}

function xpr_element_overrides(){
  if($('.elementEditor').length > 0){
    $('.elementEditor input').each(function(index){
      var input_name = $('input').attr('name');
      if(input_name == 'ImageScaleWidth' || input_name == 'ImageScaleHeight')  $(this).addClass('image_input');
      if(input_name == 'ArticleLimit')  $(this).addClass('short_input');
    });
  }
}

function xpr_navgation_overrides(){
  $('#xprMainMenu ul, ul#xprSideMenu, ul#xprUser, #xprCPath ul, ul.xprNavigation, ul.xprListHeader, ul.xprOptions').each(function(){
    $(this).find('li:first').addClass('first');
    $(this).find('li:last').addClass('last');
  });
}

function xpr_form_overrides(){
  $('input').each(function(){
    var type = $(this).attr('type');
    $(this).addClass('input_'+type);
  });
}

function xpr_data_explorer(){
  if($.browser.msie && $.browser.version < 9) {
    $('table.xprDataExplorer').each(function(){
      $(this).find('tr:even td').addClass('.xprDataExplorerEvenTd');
    });
  }
  
  /* Bind mass actions master checkboxes to slave checkboxes */
  $('.DataExplorerMasterChkBox').change(function (e) {
	var dataExplorerId = $(e.target).attr('xprDataExplorerId');
  	$('.xprDataExplorerCheckboxColumn > input[type="checkbox"][xprDataExplorerId="'+dataExplorerId+'"]:enabled').attr('checked', $(e.target).attr('checked'));
  });
  $('.xprDataExplorerCheckboxColumn > input[type="checkbox"]').change(function (e) {
	  var target = $(e.target);
	  var dataExplorerId = target.attr('xprDataExplorerId');
	  if (target.attr('checked')) {
		  if ($('.xprDataExplorerCheckboxColumn input[type="checkbox"][xprDataExplorerId="'+dataExplorerId+'"]:enabled:not(:checked)').length == 0)
		  	$('.DataExplorerMasterChkBox[xprDataExplorerId="'+dataExplorerId+'"]').attr('checked', true);
	  }
	  else
		  $('.DataExplorerMasterChkBox[xprDataExplorerId="'+dataExplorerId+'"]').attr('checked', false);
  });
  
  /* If all the slave checkboxes are disabled, also disable the master */
  $('.DataExplorerMasterChkBox').each(function() {
	  var dataExplorerId = $(this).attr('xprDataExplorerId');
	  if ($('.xprDataExplorerCheckboxColumn > input[type="checkbox"][xprDataExplorerId="'+dataExplorerId+'"]:enabled').length == 0)
		  $(this).attr('disabled', true);
  });
  
  /* 'Uncheck' our checkboxes so that they do not maintain their values on page refresh */
  $('.DataExplorerMasterChkBox, .xprDataExplorerCheckboxColumn > input[type="checkbox"]').attr('checked', false);
}

$(".btn_pending > a").live('click', function(e) {
    $.get($(this).attr("href"), function(data) {
        location.reload();
    });
    return false;
});

$('.element-box-header').live('click',function(e){
    e.preventDefault();
    $(this).parents('.element-box').find('.invisible_area').slideToggle(function(){
        window.top.xprIframeInstance.xprIframe('resize');
    });
    var _target = $(this).find("a[rel='rotate']");

    if(_target.hasClass("arrow_right")){
        _target.removeClass("arrow_right").addClass("arrow_down");
    }else{
        _target.removeClass("arrow_down").addClass("arrow_right");
    }
});
$(document).ready(function() {
    if ($('input.datepicker, input[rel=datepicker]').length) {
        $.getScript('/shared/javascript/jquery-ui.min.js', initJqueryUi);
    }
});
function initJqueryUi() {
    $('input.datepicker, input[rel=datepicker]').datepicker();
}

var XPR_MESSAGE_LEVEL = {
    SUCCESS:'xprSuccess',
    INFO:'xprInfo',
    ERROR:'xprError'
};
var xprDefaultMessageSelector = '.xprMessage:first';
function xprMessage(message, level, messageDom) {
    if (messageDom == undefined) {
        messageDom = $(xprDefaultMessageSelector);
    }
    return $(messageDom).each(function() {
        $(this).html(message);
        $(this).removeClass('xprSuccess xprInfo xprError xprMessage');
        $(this).addClass('xprMessage').addClass(level);
        $(this).show();
    });
}
function xprError(message) {
    return xprMessage(message, XPR_MESSAGE_LEVEL.ERROR);
}
function xprInfo(message) {
    return xprMessage(message, XPR_MESSAGE_LEVEL.INFO);
}
function xprSuccess(message) {
    return xprMessage(message, XPR_MESSAGE_LEVEL.SUCCESS);
}
var openPopup = window.open;

function show_msg(msg) { alert(msg); }

function validateForm() {
	missing_required = 0;

	for (i = 0; i < arguments.length; i++) {
		if(arguments[i] == '') {
			missing_required = 1;
		}
	}

	if(missing_required) {
		alert("A required form field is missing.");
		return false;
	} else {
		return true;
	}
}

function expCustomLink(myURL) {
	location.href = myURL;
}

function setUrl(path) {
	document.location.href = path;
}

function expArticleLink(sectionId, articleId) {
	location.href = '/index.php?section_id=' + sectionId + '&section_copy_id=' + articleId;
}

function expPopupWindow(url, widthVal, heightVal, resizableVal, scrollbarsVal, toolbarVal, locationVal, directoriesVal, statusVal, menubarVal, copyHistoryVal) {

	var attributes = "width="  	 	 + widthVal       +
				 	 ",height=" 	 + heightVal      +
				 	 ",resizable="  + resizableVal  +
				 	 ",scrollbars="  + scrollbarsVal  +
				 	 ",toolbar=" 	 + toolbarVal 	  +
				 	 ",location=" 	 + locationVal 	  +
				 	 ",directories=" + directoriesVal +
				 	 ",status=" 	 + statusVal 	  +
				 	 ",menubar=" 	 + menubarVal 	  +
				 	 ",copyhistory=" + copyHistoryVal;

	window.open(url, 'WindowName', attributes);
}

function xprLog(msg) {
    if (window.console) console.log(msg);
}

if (document.addEventListener) {  
    document.addEventListener("DOMContentLoaded", function () { if (typeof onXprPageLoad == 'function') onXprPageLoad(); }, false);
} else if (document.attachEvent) {  
    document.attachEvent('DOMContentLoaded', function () { if (typeof onXprPageLoad == 'function') onXprPageLoad(); });
}  

