jQuery.noConflict();

var dreamingPanelGroup = 1;
var dreamingPanelGroupMatch = location.search.match(/dreaming-panel-group=(\d)/);
if(dreamingPanelGroupMatch){
	dreamingPanelGroup = dreamingPanelGroupMatch[1];
}

var Background = {
  
  init:function(){
    
    var timeoutToPreload = 100;
    
    var body = jQuery("body");
    
    var bgsource = body.css("backgroundImage");
    
    if(bgsource){
      
      bgsource = bgsource.replace(/ |url|\(|\)|"|'/g, "");
      
      var bg = jQuery('<div class="bg"></div>');
      Background.bg = bg;
      
      var hider = jQuery('<div class="hider"></div>');
      bg.append(hider);
      
      var img = jQuery('<img />');
      img.css({opacity:0});
      bg.append(img);
      Background.img = img;
      
      var startTime = new Date().getTime();
      
      //
      img.load(function(){
        var loadTime = new Date().getTime();
        if(loadTime-startTime > timeoutToPreload){
          setTimeout(function(){
            Background.img.css({left:"50%", marginLeft:-(Background.img.outerWidth(true)*0.5)+"px"});
            Background.resizeBackground();
            img.animate({opacity:1}, 2000, function(){
              body.css({ backgroundImage:"url("+bgsource+")" });
              Background.bg.remove();
            });
          }, 10);
        } else {
          body.css({ backgroundImage:"url("+bgsource+")" });
          Background.bg.remove();
        }
      });
      
      //jQuery(window).resize(Background.resizeBackground);
      //jQuery(window).fontResize(Background.resizeBackground);
      
      img.attr("src", bgsource);
      
      body.css({ backgroundImage:"none" });
      body.append(bg);
      
      
    } 
  },
  
  resizeBackground: function(){
    Background.bg.css({ 
      width: Math.max(jQuery("#wrap").outerWidth(true), jQuery("html").innerWidth())+"px", 
      height:Math.max(jQuery("#wrap").outerHeight(true), jQuery("html").innerHeight())+"px"
    });
  }
  
};

var fontSizer = {
  
  init:function(){
    
    jQuery("#font-bigger").click(fontSizer.bigger);
    jQuery("#font-smaller").click(fontSizer.smaller);
    
    //is there a cookie with the size set?
    var cookiedSize = jQuery.cookie("fontSizerSize");
    
    if(cookiedSize){
      fontSizer.size = parseFloat(cookiedSize);
      jQuery("body").css({fontSize:fontSizer.size+"%"});
    } else {
      fontSizer.size = (parseFloat(jQuery("body").css("fontSize"))/parseFloat(jQuery("html").css("fontSize")))*100;
    }
    
  },
  
  bigger:function(){
    
    fontSizer.size += 10;
    
    //store a cookie
    jQuery.cookie("fontSizerSize", fontSizer.size);
     
    jQuery("body").css({fontSize:fontSizer.size+"%"});
    
  },
  
  smaller:function(){
    
    fontSizer.size -= 10;
    fontSizer.size = Math.max(70, fontSizer.size);
    
    //store a cookie
    jQuery.cookie("fontSizerSize", fontSizer.size);
    
    jQuery("body").css({fontSize:fontSizer.size+"%"});
    
  }
  
};


var windowResizeOverflower = {
  
  init:function(){
    
    if(windowResizeOverflower.initted) return;
    windowResizeOverflower.initted = true;
    
    windowResizeOverflower.scrollBarWidth();
    
    windowResizeOverflower.targetElement = "html";
    //if(jQuery.browser.msie) 
    
    windowResizeOverflower.onResize();
    
    jQuery(window).resize(windowResizeOverflower.onResize);
    
  },
  
  scrollBarWidth: function() {
    
    var inner = document.createElement('p');
    inner.style.width = '100%';
    inner.style.height = '200px';
    
    var outer = document.createElement('div');
    outer.style.position = 'absolute';
    outer.style.top = '0px';
    outer.style.left = '0px';
    outer.style.visibility = 'hidden';
    outer.style.width = '200px';
    outer.style.height = '150px';
    outer.style.overflow = 'hidden';
    outer.appendChild (inner);
    
    document.body.appendChild (outer);
    var w1 = inner.offsetWidth;
    outer.style.overflow = 'scroll';
    var w2 = inner.offsetWidth;
    if (w1 == w2) w2 = outer.clientWidth;
    
    document.body.removeChild (outer);
    
    windowResizeOverflower.scrollBarWidth = (w1 - w2);
    
  },
  
  onResize:function(){
    
    if(jQuery(window).width() == windowResizeOverflower.windowWidth) return;
    
    windowResizeOverflower.windowWidth = jQuery(window).width();
    
    clearTimeout(windowResizeOverflower.timeout);
    
    windowResizeOverflower.timeout = setTimeout(function(){
      var documentWidth = jQuery(document).width();
      var windowWidth = jQuery(window).width();
      var shrink = false;
      
      if(documentWidth > windowWidth){
        var buffer = 18;
        var extraBuffer = 0;
        if(!windowResizeOverflower.shrunk) extraBuffer = 18;
        if(documentWidth - windowWidth <= buffer+extraBuffer){
          var shrinkOffset = Math.max(0, documentWidth - windowWidth - extraBuffer);
          shrink = true;
        }
      }
      
      windowResizeOverflower.shrunk = shrink;
      
      if(shrink){
        jQuery(windowResizeOverflower.targetElement).css({overflowX:"hidden"});
        jQuery("#wrap").css({left:"-"+shrinkOffset+"px"});
      } else {
        jQuery(windowResizeOverflower.targetElement).css({overflowX:"auto"});
        jQuery("#wrap").css({left:"0px"});
      }
    }, 100);
    
  }
  
};

  
 var dreamingPanelExpander = {
  
  init: function(){
	//jQuery(".small-dreaming-panel #dreaming-panel .panel a").click(dreamingPanelExpander.expand);
    	jQuery("#dreaming-panel-swf").css({display:"none"});
	jQuery("#dreaming-panel-swf.full").css({display:"block"});
	
	jQuery("#expandPartialPanel").click(dreamingPanelExpander.expand);
	jQuery("#dreaming-panel-swf").html("<a href='http://get.adobe.com/flashplayer/' title='Get Flash'><img id='alternate_flash' src='/custom/files/media/alternative_flash.jpg' alt='Get Flash' border='0'/></a>");
	jQuery("#alternate_flash").css({display:"none"});
	jQuery("#dreaming-panel-swf.full #alternate_flash").css({display:"block"});
  },
  
  expand: function(){
    	jQuery(this).css({display:"none"});
	jQuery("#dreaming-panel-swf").css({display:"block"});

  	
	var panel = jQuery("#dreaming-panel .panel");
	panel.find("h1, img").fadeOut('500');  
    	panel.animate({height:"392px"}, 500, function(){
		var flashvars = {};
		flashvars.xmlsource = panel.find("input[name=xmlsource]").val();
		flashvars.color = panel.find("input[name=color]").val();
		flashvars.group = panel.find("input[name=group]").val();
		var params = {
			menu: "false",
			allowFullscreen: "true",
			allowScriptAccess: "always",
			wmode: "transparent"
		};
		var attributes = {};
			
		swfobject.embedSWF('/custom/files/media/dreaming_panel.swf', 'dreaming-panel-swf', '612', '379', '10.0.0', '/custom/files/media/expressInstall.swf', flashvars, params, attributes);
		jQuery("#alternate_flash").css({display:"block"});

    	});
  }
};



var isMSIE6 = jQuery.browser.msie && jQuery.browser.version < 7;

jQuery(document).ready(function(){
  
  //Background.init();
  
  //fontSizer.init();
	
  
  //fix pngs for IE6
  
  
  jQuery(".corner").decorateBlocks({ cornerSize:21, cap:{top:true, bottom:true}, shadowSize:8 });
  jQuery(".capBottom").decorateBlocks({ cornerSize:21, cap:{bottom:true}, shadowSize:8 });
  jQuery(".capTop").decorateBlocks({ cornerSize:21, cap:{top:true}, shadowSize:8 });
  jQuery(".shadow").decorateBlocks({ cap:{}, shadowSize:8 });
  
  if(isMSIE6) DD_belatedPNG.fix( "div,img,a,span,input" );
  
  jQuery("#right-panel .nav, #right-panel .nav > li > ul").jqueryNav();
  
  jQuery("#hotel-booking #right-panel .tabs").jqueryTabs("#right-panel .destinations", {wrap:true} );
  // modified 2009-12-29, Luke Skibinski, turned off horizontal scrolling for the booking search panel
  jQuery("#booking-panel .tabs").jqueryTabs("#booking-panel .tab-content", {hscroll:false}); 
  


	//William Rogers - 21st Feb 2010
		for(i=0; i<jQuery('.galpage .centricListingGeneral').size(); i++){
		var hreef = jQuery('.galpage .centricListingGeneral:eq('+i+') a').attr('href');		
		jQuery('.galpage .centricListingGeneral:eq('+i+') img').wrapAll('<a href="'+hreef+'" style="display:block" rel="galleryimg" />');
	}
	//jQuery('.galpage .centricListingThumbnail a').colorbox({slideshow:true,slideshowSpeed:5000,slideshowAuto:false});
	//jQuery("a[rel='galleryimg']").colorbox({slideshow:true,slideshowSpeed:5000,slideshowAuto:false});
	jQuery("a[rel='galleryimg']").lightBox();
        jQuery('.galpagelanding .centricListingResultSmall').after('<div class="opacity">&nbsp;</div>');

	//Josh - 09 03 2010       
	var div_count = jQuery("#cart_control div").size() - 2;
	//jQuery("#cart_control div:last").css({height:"30px"});
	jQuery("#cart_control div:last").addClass("cart_checkout");
	//jQuery("#cart_control div:eq("+div_count+")").css({height:"30px"});
	jQuery("#cart_control div:eq("+div_count+")").addClass("cart_checkout_total");
	//jQuery("#cart_control div:eq("+div_count+") p").css({height:"30px"})
	jQuery("#address_div p:last").addClass("confirm_address_button");
	jQuery("#checkout_control p:last").addClass("confirm_checkout_button");
	//var checkout_page = location.search.match(/checkout_process\.jsp/);
	var checkout_page = location.pathname;
	if(checkout_page.indexOf('checkout_process') != -1){
		jQuery("#checkout_control div").addClass("checkout_divs");
	}
	var found_payment_heading = jQuery("#checkout_control p").find(":contains('Payment Details:')");
	if(found_payment_heading.length == 0){
		jQuery("#checkout_control div").removeClass("checkout_divs");
		jQuery("#checkout_control div").addClass("payment_divs");
	}
	var found_payment_heading2 = jQuery("#checkout_control h2").find(":contains('Confirm Order and Purchase')");
	if(found_payment_heading2.length == 0){
		jQuery("#checkout_control div").removeClass("payment_divs");
		jQuery("#checkout_control div").addClass("checkout_divs");
	}
	jQuery("#cart_control_minimal").prepend("<h1>Shopping Cart</h1>");
	var found_minimal_cart = jQuery("#cart_control_minimal").find("table");
	if(found_minimal_cart.length == 0){
		jQuery("#cart_control_minimal").css({display:"none"});
	}else{
		jQuery("#cart_control_minimal").css({display:"block"});
	}

	jQuery("#merch_detail form").submit(function(){
		alert("The selected product has been added to the cart.");	
	});
	//jQuery("#cart_control_minimal td").css({"border-top":"1px solid #999999"});
	jQuery("#cart_control_minimal td[colspan='4']").css({"border-top":"1px solid #999999"});
	jQuery(".element input[type='checkbox']").css({"margin-right":"5px"});
	jQuery(".element input[type='radio']").css({"margin-right":"5px"});

	//---Josh Font Resize Code
	var originalFontSize = jQuery("html").css("font-size");
	// Increase Font Size
	jQuery("#font-bigger").click(function(){
		var currentFontSize = jQuery("html").css("font-size");
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.1;
		jQuery("html").css("font-size", newFontSize);
		return false;
	});
	// Decrease Font Size
	jQuery("#font-smaller").click(function(){
		var currentFontSize = jQuery("html").css("font-size");
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.9;
		jQuery("html").css("font-size", newFontSize);
		return false;
	});

  windowResizeOverflower.init();
  
  dreamingPanelExpander.init();
  
  jQuery("input[type=text]").each(function(){
    var i = jQuery(this);
    var originalFormValue = i.attr("value");
    i.click(function(){
      if(i.attr("value") == originalFormValue) i.attr("value", "");
    });
    i.blur(function(){
      if(i.attr("value") == "") i.attr("value", originalFormValue);
    });
  });
    
}); 



function RGBToHex(rgbString) {
  
  if(rgbString.indexOf("#") == 0) return rgbString.replace("#", "0x");
  
  var parts = rgbString.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  delete (parts[0]);
  for (var i = 1; i <= 3; ++i) {
    parts[i] = parseInt(parts[i]).toString(16);
    if (parts[i].length == 1) parts[i] = '0' + parts[i];
  }
  return '0x'+parts.join(''); // "0070ff"
  
} 



