var flashvars = {}, params = {}, attributes = {};
params.allowscriptaccess = "always";
params.scale = "noscale";
params.align = "t";
params.salign = "t";

swfobject.embedSWF("/swf/head.swf", "flash-header", "850", "118", "9.0.0", "/swf/head.swf", flashvars, params, attributes);

swfobject.embedSWF("/swf/head_members.swf", "flash-header-members", "850", "118", "9.0.0", "/swf/head_members.swf", flashvars, params, attributes);

$(document).ready(function(){
  $('.login-bar-icon').hover(function(){
    var temp = $(this).children('img').attr('src')
    $(this).children('img').attr('src',$(this).children('img').attr('alt'));
    $(this).children('img').attr('alt', temp);
  },function(){
    var temp = $(this).children('img').attr('src')
    $(this).children('img').attr('src',$(this).children('img').attr('alt'));
    $(this).children('img').attr('alt', temp);
  });

  $('.body-web .list ul li').hover(function(){
    $(this).css('background','url(/images/boton/boton2.png) top left no-repeat');
  },function(){
    $(this).css('background','url(/images/boton/boton.png) top left no-repeat');
  });
  
  $('.text-body:empty').remove();

  $('.type-text').each(function(){
    $(this).parents('li').children('.text-item-list:empty').remove();
  })
  $('.type-file').each(function(){
    var temp = $(this).parents('li').children('.text-item-list').html();
    $(this).parents('li').children('.text-item-list').html('<a href="/sf/web/uploads/files/'+temp+'" target="_blank" >Download File</a>');
  })
  $('.type-link').each(function(){
    var urlExterna = /^http:/;
    var temp = $(this).parents('li').children('.text-item-list').html();
    
    $(this).html('<a href="'+temp+'" >'+$(this).html()+'</a>');
    
    if (urlExterna.test($(this).parents('li').children('.text-item-list').html())){
      $(this).children('a').attr('target','_blank');
    }else{
      $(this).children('a').attr('target','_self');
    }
    
    $(this).parents('li').children('.text-item-list').remove();
  })
  
  
  $('.content-item-list').not('.type-link').toggle(function(){
    $(this).parents('li').children('.text-item-list').show();
  },function(){
    $(this).parents('li').children('.text-item-list').hide();
  })
  
  //$("a[rel^='prettyPhoto']").prettyPhoto({ theme:'facebook', hideflash: true });
 
	//////////////////   QUOTATIONS ////////////////////
	///////////////////// MANEJA LOS PACKAGES  ///////////////////////////////////////////////////////////////////////////
		
		$('#packages').bind('keyup paste',function(){
			//alert($("input[@name=shipment]:checked").attr('id'));
			if (($("input[name=shipment]:checked").attr('id') == 'shipment-3') && ($('#packages').val().length)){
				//alert('aigachu!');
				agregarQuitarPackages();
			}
		});
		///////////////////// MANEJA LOS PACKAGES end  ///////////////////////////////////////////////////////////////////////////
		
		/*
		$('#country').bind('change',function(){
			if (($(this).val() == 'Argentina') || ($(this).val() == 'Brazil')){
				$('#import-clear option').css('display','none');
			}else{
				$('#import-clear option').css('display','block');
			}
		})*/
		
		$('#wrap-no-of-containers').css('display','none');
		$('#wrapout-packages-data').css('display','none');
		$('input[name=shipment]').bind('click',function(){
			if ($("input[name=shipment]:checked").attr('id') == 'shipment-4'){
				$('#wrap-no-of-containers').fadeIn();
			}else{
				$('#wrap-no-of-containers').fadeOut();
			}
			if ($("input[name=shipment]:checked").attr('id') == 'shipment-3'){
				$('#wrapout-packages-data').fadeIn();
			}else{
				$('#wrapout-packages-data').fadeOut();
			}
		});
		
		/*
		$('#wrap-other-instructions').css('display','none');
		$('#instructions-radios input').bind('click',function(){
			//alert($("#instructions-radios input:checked").attr('id'));
			if ($("#instructions-radios input:checked").attr('id') == 'instruction-5'){
				$('#wrap-other-instructions').fadeIn();
			}else{
				$('#wrap-other-instructions').fadeOut();
			}
		});
		*/
		$('#envia-form-quotation').bind('click',function(){
			var dimensions = '';
			$('.dimensions').each(function(){
				dimensions += $(this).val()+'&';
			});
			var grossWeight = '';
			$('.gross-weight').each(function(){
				grossWeight += $(this).val()+'&';
			});
			var volumeWeight = '';
			$('.volume-weight').each(function(){
				volumeWeight += $(this).val()+'&';
			});
		
			$('#ajax-invisible').load('./envia-quotation.php',{
				country: $('#country').val(),
				type: $('#type-of-quote').val(),
				duties: $('#duties-radios input:checked').val(),
				hscode: $('#hs-code').val(),
				currency: $('#currency').val(),
				shipment: $('#shipment-radios input:checked').val(),
				comodity: $('#comodity').val(),
				rating: $('#rating-radios input:checked').val(),
				packages: $('#packages').val(),
				dimensions: dimensions,
				grossWeight: grossWeight,
				volumeWeight: volumeWeight,
				typeOfSeaShipment: $('#type-of-sea-shipment').val(),
				containers: $('#containers').val(),
				totalKgs: $('#total-in-kgs').val(),
				totalM3: $('#total-in-m3').val(),
				//instructions: $('#instructions-radios input:checked').val(),
				from: $('#from').val(),
				to: $('#to').val(),
				otherInstructions: $('#special-instructions').val(),
				company: $('#your-compamy').val(),
				contact: $('#contact-person').val(),
				mail: $('#mail').val(),
				tel: $('#tel').val(),
				fax: $('#fax').val(),
				//collection: $('#collection').val()
			});
		})
		
		$('#back').bind('click',function(){
			window.history.go(-1);
		})
});
var cssTPackage = "display:static;margin-right:38px;font-weight:bold;float:left;width:150px;";
var cssInputPackage = "position:static;	width:151px;";

function agregarQuitarPackages(){
	//alert('aigachu!');
	//cuenta cuantos datos de packages hay
	var cantPackagesData = 0;
	$('.package').each(function(){
		cantPackagesData++;
	});

	while ($('#packages').val() != cantPackagesData){
	
		if ($('#packages').val() > cantPackagesData){
			noPkg = cantPackagesData+1;
			
			ispar = noPkg%3==0?true:false;
			
			if (ispar){margen='margin-right:0;';}else{margen='margin-right:60px;';}
			
			$('#wrap-packages-data').append('<div class="package" style="padding:5px;float:left;width:165px;margin-top:20px;'+margen+'"><div class="titulo-package" style="font-weight:bold;margin-bottom:10px;width:100%;">Package No '+noPkg+'</div><div class="linea-form"><div class="titulo-campo-form" style="'+cssTPackage+'">Dimensions:</div><input id="dimensions-'+noPkg+'" type="text" class="text-input dimensions" style="'+cssInputPackage+'"></div><div class="linea-form"><div class="titulo-campo-form" style="'+cssTPackage+'">Gross weight:</div><input id="gross-weight-'+noPkg+'" type="text" class="text-input gross-weight" style="'+cssInputPackage+'"></div><div class="linea-form"><div class="titulo-campo-form" style="'+cssTPackage+'">Volume weight in m&sup3;:</div><input id="volume-weight-'+noPkg+'" type="text" class="text-input volume-weight" style="'+cssInputPackage+';width:151px">&sup3;</div></div>');
			
			cantPackagesData++;
			
		}else{
			$("#dimensions-"+cantPackagesData).parent().parent().remove();
			cantPackagesData--;
		}
	
	}
}
function trim (str){
  str = str.replace(/^\s*|\s*$/g,"");
  return str;
}
function roundNumber(num, dec) {
  var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  return result;
}
