/**
 * jQuery.fullBg
 * Version 1.0
 * Copyright (c) 2010 c.bavota - http://bavotasan.com
 * Dual licensed under MIT and GPL.
 * Date: 02/23/2010
*/
(function($) {
  $.fn.fullBg = function(){
    var bgImg = $(this);		
 
    function resizeImg() {
      var imgwidth = bgImg.width();
      var imgheight = bgImg.height();
 
      var winwidth = $(window).width();
      var winheight = $(window).height();
 
      var widthratio = winwidth / imgwidth;
      var heightratio = winheight / imgheight;
 
      var widthdiff = heightratio * imgwidth;
      var heightdiff = widthratio * imgheight;
 
      if(heightdiff>winheight) {
        bgImg.css({
          width: winwidth+'px',
          height: heightdiff+'px'
        });
      } else {
        bgImg.css({
          width: widthdiff+'px',
          height: winheight+'px'
        });		
      }
    } 
    resizeImg();
    $(window).resize(function() {
      resizeImg();
    }); 
  };
})(jQuery)



/* WEB SITE JS STARTS HERE */

$(function(){

	//$("#fullbackground").fullBg();

	$("#inquiry-toggle").click( function(e){
	 	e.preventDefault();
	 	
		slidevalue = $('#inquiry-content').outerWidth();

	 	inquiryPosition = $('#inquiry').position();
 		screenwidth = $(window).width();
 		value = screenwidth - inquiryPosition.left;

	 	if ( value >= 25 ){
	 		slidevalue = '+=' + slidevalue;
			// alert('>' + value);
			$('#inquiry').removeClass('open');
	 	} else {
	 		slidevalue = '-=' + slidevalue;
			$('#inquiry').addClass('open');
	 	}

		$('#inquiry').animate({
		    left: slidevalue },
		    '500', function() {
		    // Animation complete.
		  });
	});
	$(window).resize(function(){
		setContact();	
    });
	setContact();
	
	$('.gallery-item a').fancybox({
		'titlePosition' : 'inside'
	});

	$('.manufacturesite').fancybox({
		'type': 'iframe',
		'titleShow' : false,
        'autoScale' : false,
		'width' : '100%',
		'height' : ' 99%',
		'padding' : '0',
		'overlayOpacity' : '.7',
		'overlayColor' : '#333'
		});
	
	$('.gallery-link a').click( function (e) {
		e.preventDefault();
		
		$(this).closest('.singleimage-gallery').find('.gallery-item').css('display', 'none');

		hash = $(this).attr('href');
		hashbits = hash.split('#');
		gotohash = '#' + hashbits[1];

		$( gotohash ).css('display', 'block');
	});
	
	randomBg();
})


function randomBg() {
	var bgs = new Array();

	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

	// Adds random CLS background from list below
	bgs.push("body-background-1.jpg");
	bgs.push("body-background-2.jpg");
	bgs.push("body-background-3.jpg");
	bgs.push("body-background-4.jpg");
	bgs.push("body-background-5.jpg");
	// End List
	
	$('#fullbackground').css('display', 'none');
	initial_select = bgs[randomize(0, bgs.length-1)];
	selectedbg = '/content/themes/blank/images/backgrounds/' + initial_select;

	var hidden = $('body').append('<div id="img-cache" style="display:none"/>').children('#img-cache');

	var dstart_date = new Date();
	dstart = dstart_date.getTime();

	$('<img id="preload-bg"/>').attr('src', selectedbg).appendTo(hidden).load( function() { 
		$('#fullbackground').css('background-image', 'url(\'' + selectedbg + '\')' );	

		var dend_date = new Date();
		dend = dend_date.getTime();

		if ( (dend - dstart) > 250 ) {
			$('#fullbackground').fadeIn('slow');
			ms_filters();
		} else {
			$('#fullbackground').css('display', 'block')
			ms_filters();
		}
	});
}

function ms_filters() {

	ie8_filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/content/themes/blank/images/backgrounds/" + initial_select + "', sizingMethod='scale')";
	ie8_filter = '"' + ie8_filter + '"';

	$("#fullbackground").css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/content/themes/blank/images/backgrounds/" + initial_select + "', sizingMethod='scale'");

	$("#fullbackground").css('-ms-filter', ie8_filter);
}

function randomize(min, max) {
	if (!min)
		min = 0;
	
	if (!max)
		max = 1;
	
	return Math.floor(Math.random()*(max+1)+min);
}


function setContact() {
	screenwidth = $('#maincontent').width() + 'px';
	$("#inquiry").css('left', screenwidth);
}
