/*********************
Local Javascript File
All site specific and custom javascript functions go in this file if they don't already belong to another lib
This should be the last javascript file loaded into the page as it has a lot of dependencies and is used as an override for a few 3rd party js libs
**********************/

/*********************
If scripting is disabled the following stylesheet will not be read.
*********************/
//document.write('<link href="/includes/styles/jsActiveStyles.css" rel="stylesheet" type="text/css">');

/***********************
JQUERY Genernal/Global Functions:
functions placed within a ready function which means they will not be applied until the dom is ready
************************/
$().ready(function() {
/***********************
YUI Calendar Script
In order to add a dhtml date picker to an input(type=text):
All you have to do is give it a class of 'date' and be sure to also give it an id.
Th script below will dyeamically pull in the necessary YUI files from http://yui.yahooapis.com/2.5.2/
TODO: this has not been tested in conjunction with an SSL security certificate 
************************/
  $.getPlugin(plugin.calendar,function() {
    $("input.date").each(function(i) {
      $(this).after('<div id="'+i+'_calendar_container" class="calendar_box"></div>' );
      $('.calendar_box').wrap('<div id="calendar_wrap"></div>');
      $('.calendar_box').css('display','none');
      $('.calendar_box').css('position','absolute');
      $('.calendar_box').css('z-index','10000');
      el = $(this);
      myCalendar = new YAHOO.widget.Calendar($(this).attr('id'),i+"_calendar_container", { title:"Date", close:true, iframe:true,navigator:true } );
      myCalendar.render();
      YAHOO.util.Event.addListener(el, "click", myCalendar.show, myCalendar, true);
      function handleSelect(type,args,obj) {
        var dates = args[0];
        var date = dates[0];
        var year = date[0], month = date[1], day = date[2];
        var cal_id = obj.input_id;
        $('#'+cal_id).attr('value',year + "-" + month + "-" + day);
//        $('#'+cal_id).attr('value',month + "/" + day + "/" + year);
        $('.calendar_box').css('display','none');
      }
      myCalendar.input_id = $(this).attr('id');
      myCalendar.selectEvent.subscribe(handleSelect, myCalendar, false);
    });
  });

/**********************
RICH TEXT EDITOR
In order to add an rte to a textarea give it a class of 'rte-zone'
**********************/
	$("textarea.rte-zone").each(function(j){
    if(j==0) {
      $.getScript("/includes/lib/common/fckeditor/fckeditor.js",function() {
        $("textarea.rte-zone").each(function(i) {
          var rte_name = $(this).attr('name');
          var oFCKeditor = new FCKeditor(rte_name);
          oFCKeditor.BasePath='/includes/lib/common/fckeditor/';
          oFCKeditor.ToolbarSet='GUD';
          oFCKeditor.Config['ToolbarCanCollapse']=false;
          oFCKeditor.Config['CustomConfigurationsPath']='/includes/js/fckconfig_my.js';
          oFCKeditor.ReplaceTextarea();
          rte_script=true;
        });
      });
    }//end if j==0
  });

/****************
IF IE6 rules
****************/
  if (window.attachEvent) {
    //:hover only works on anchor tags in IE 6 and earlier
    //do your best to make the ui work with a:hover before resorting to this
		//the following is a simplified version of the suckerfish method
    $('.iehover').each(function() {
      $(this).hover(function(){
        $(this).addClass("hover");
      },function() {
        $(this).removeClass("hover");
      });
    });

  }//end if IE6 rules


/************************
Close errors and messages
*************************/
  $("#close_errors").click(function() {
    $('#errors').fadeOut('slow');
  });
  $("#close_messages").click(function() {
    $('#messages').fadeOut('slow');
  });
  $("#messages").fadeIn('slow');
  $("#errors").fadeIn('slow');

/*****************
apply target='_blank' to all links with class 'external_link'
*****************/
  $('a.external_link').each(function(){
    $(this).attr('target','_blank');
    $(this).attr('title',$(this).attr('title')+' - Link opens in new window');
  });

/*****************************
Apply Simple popup to a link
*****************************/
	$('a.popup').each(function(){
    $(this).attr('title',$(this).attr('title')+" - Link Loads in Popup Window");
		$(this).click(function() {
			var relSplit = $(this).attr('rel').split("|");
    	popupWidth = relSplit[0];
    	popupHeight = relSplit[1];
    	jspopup(this.href,'console',popupWidth,popupHeight);
			return false;
		});
  });

/*************************
give all inputs a class based on their type
*only use if necessary, css will not work if js is not enabled*
*************************/
/*
  $('input').each(function(){
    $(this).addClass($(this).attr('type'));
  });
*/

/****************
rounded corners
TODO: this is really only needed for ie7 but currently is used for all IE versions
TODO: get rounded corner method to work in IE6, currently only works in IE7
*****************/
	if(!jQuery.support.cssFloat) {
		$('.rounded').each(function() {
			$(this).prepend('<div class="tl"></div><div class="tr"></div>');
			$(this).append('<div class="bl"></div><div class="br"></div>');
		});
	}

/* box model test */
	$('#boxmodel_test').html('This div uses the W3C box model: '+jQuery.support.boxModel);

});//end ready function for general functions

/***********************
 Simple Popup Function
************************/
function jspopup(strURL,strType,strWidth,strHeight) {
var strOptions="";
if (strType=="console") strOptions="resizable,scrollbars=yes,height="+strHeight+",width="+strWidth;
//create different popup window options here
window.open(strURL, '', strOptions);
}

/*******************
Site Specific Code
*******************/
$().ready(function() {

	$('.learnmore').click(function() {
		var isinline = $(this).parents('.inline').length;
		if (isinline) {
			var div = $(this).prev('div');
			if ($(this).hasClass("open")) {
				$(this).removeClass("open");
				div.hide("slow");
			} else {
				$(this).addClass("open");
				div.show("slow");
				div.fadeIn("slow");
			}
		} else {
			if ($(this).hasClass("open")) {
				$(this).removeClass("open");
				var div = $(this).prev('div');
				div.hide("slow",function() { div.empty(); } );
			} else {
				$(this).addClass("open");
				$(this).before('<div style="display: none;"></div>');
				var div = $(this).prev('div');
				$.post($(this).attr('href'),{handbrewed:true},function(html) {
					div.html(html);
					div.show("slow");
					div.fadeIn("slow");
				},'html');
			}
		}
		$(this).blur();
		return false;
	});

	$('.more').css("display","none");
	$('.inline .learnmore').css("display","block");
	$('a[rel*=facebox]').facebox();
	$('.admin a.delete').click(function() {
		return confirm("are you sure?");
	});

	$('#reorder').sortable({
		axis: 'y',
		cursor: 'crosshair',
		cursorAt: 'top',
		update: function(ev,ui) {
			var i=1;
			$('#reorder tr').each(function() {
				//recolor
				$(this).addClass((i%2)?'even':'odd');
				$(this).removeClass((i%2)?'odd':'even');
				//$(this).find(':first').html(i);
				++i;

			});
			$.ajax({
				data: $('#reorder').sortable('serialize'),
				url:"/admin/pages/order",
				type:"post",
				error:function() { alert("ERROR UPDATING SORT ORDER"); },
				success:function(data) {
					//ignoring server response :)
				}
			});

		}
	});

	companypage = $('body.company').length;
	if (companypage == 1) {
		$('body.company #content').css('background','transparent');
    var flashvars = {
    };
    var params = {
      menu: "false",
      scale: "noScale",
      allowFullscreen: "true",
      allowScriptAccess: "always",
      wmode: "transparent"
    };
    var attributes = {
      id:"homepage"
    };
    swfobject.embedSWF("/swf/team.swf?v=1.2.5", "flashpiece", "1376", "350", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);

	}

});//end site specific code ready function
