/*!
 * jquery.cornerhack.js - Rounded corners for IE via javascript
 */

 //extend with an array to hold all items
 jQuery.extend({
 	elementsWithCornerHack: []
 });

 //helper function for IE to calculate
 jQuery.fn.cornerImageCoordinates = function(offsetWidth,offsetHeight){
  element = $(this);
  if(offsetWidth==undefined) offsetWidth = 5;
  if(offsetHeight==undefined) offsetHeight = 5;

  return {
    left: '-1px',
    right: element.width() + parseInt(element.css('paddingLeft')) + parseInt(element.css('paddingRight')) - offsetWidth + 'px',
    top:  '-1px',
    bottom: element.height() + parseInt(element.css('paddingTop')) + parseInt(element.css('paddingBottom')) - offsetHeight + 'px'
  };
 };

 jQuery.fn.round = function(topLeft, topRight, bottomRight, bottomLeft, offsetWidth, offsetHeight, cornerImage){        
   return this.each(function(){
     var region = $(this);
     
     //check if corner hack already applied
     if(region.hasClass("corner-hacked")) return;
     else region.addClass("corner-hacked");
     
     //don't add corners to regions with no border
     if(region.css('border-style')=='none') return;
     
     var cornerCoordinates = region.cornerImageCoordinates(offsetWidth,offsetHeight);
     
     jQuery.elementsWithCornerHack.push(this);

     region.css({
       position: region.css('position')=='absolute' ? 'absolute' : 'relative',
       zoom: 1
     });


     //default values of variables
     if(cornerImage==undefined) cornerImage = "corner";
     if(topLeft==undefined) topLeft = true;
     if(topRight==undefined) topRight = true;
     if(bottomRight==undefined) bottomRight = true;
     if(bottomLeft==undefined) bottomLeft = true;
     
     //build base path
      var baseImagePath = '/new/images/cornerhack/'+ cornerImage +'_';

     //top left corner
     if(topLeft){
       region.append(
         $("<img/>").attr({src: baseImagePath+'top_left.gif'}).addClass('ieCorner').css({
            position: 'absolute',
            zIndex: 3000,
            //border: '1px solid red',
            top: cornerCoordinates.top,
            left: cornerCoordinates.left
          })
       );
     }

     //top right corner
     if(topRight){
       region.append(
         $("<img/>").attr({src: baseImagePath+'top_right.gif'}).addClass('ieCorner').css({
           position: 'absolute',
           zIndex: 3000,
           //border: '1px solid red',
           top: cornerCoordinates.top,
           left: cornerCoordinates.right
         })
       );
     }

     //bottom right
     if(bottomRight){
       region.append(
         $("<img/>").attr({src: baseImagePath+'bottom_left.gif'}).addClass('ieCorner').css({
            position: 'absolute',
            zIndex: 3000,
            //border: '1px solid red',
            top: cornerCoordinates.bottom,
            left: cornerCoordinates.left
          })
       );
     }

     //bottom left
     if(bottomLeft){
       region.append(
         $("<img/>").attr({src: baseImagePath+'bottom_right.gif'}).addClass('ieCorner').css({
            position: 'absolute',
            zIndex: 3000,
            //border: '1px solid red',
            top: cornerCoordinates.bottom,
            left: cornerCoordinates.right
          })
       );
     }

   });
 };

 jQuery.fn.fixRound = function(offsetWidth,offsetHeight){
   return this.each(function(){
     var region = $(this);
     var cornerCoordinates = region.cornerImageCoordinates(offsetWidth,offsetHeight);
     $('img.ieCorner',region).each(function(){
       img = $(this);
       if( img.css('top') != '-1px'){
         img.css('top',cornerCoordinates.bottom);
       }
     });
   });
 };

function roundCorners(context){
  $("#menu").css('zoom',1).round(true,true,true,true,undefined,undefined,"navmenu_corner");
  $("#sidebar-region div.region").round(true,true,true,true,undefined,undefined,"sidebar_corner");
  $(".home-page #hero-region").round(true,true,false,false,5, undefined,"white_corner");
  $("#hero-region").round(true,true,true,true,5, 5,"white_corner");
  $("div.region").round();
 }

 $(document).ready(function(){
   roundCorners(this);
 });