/*
$(document).ready(function() {
	$('.fondhome').maxImage({
		isBackground: true,
		slideShow: true,
		//slideShowTitle: false,
		slideDelay: 5.2,
		wait: false,
		loaderClass: 'scalingLoader',
		verticalAlign: 'top'
	});
});
$(document).ready(resize);
$(window).resize(resize);


*/
function view_child(obj) {
	var result = "";
	for (var i in obj) {
		result += 'obj' + "." + i + " = " + obj[i] + "<br>";
	}
	
	var w=window.open();
	w.document.write(result);
	return result;
}

$(document).ready(function() {
	// SOUND CODE SHOULD BE REFACTORED
	soundManager.url = 'js/swf/';
	soundManager.onload = function(){
		var $toolTip = $('#audiobtn'),
		siteMute  = false,
		mySound = soundManager.createSound({
				id: 'introSound',
				url: 'music/audio.mp3',
				volume: 60,
				stream: false,  
				onfinish: function(){mySound.setPosition(0);mySound.play();}
		}); 
		mySound.play();
		$toolTip.bind('click',function(){
		mySound.togglePause();
		siteMute = mySound.paused? true:false;
		$toolTip.html(mySound.paused ? audio_on_str : audio_off_str);
		});
		$(document).bind('touchstart',function(e){mySound.play();$(document).unbind('touchstart',arguments.callee);});
		// youtube api code (to Refactor) :
		window.wtf_yt_callback = function(newState){        	
		if(newState == 1)mySound.pause();
		if( (newState == 0 || newState == 2) && mySound.paused && !siteMute){mySound.play();}
		}
		window.onYouTubePlayerReady = function() {	
		var ytplayer = $("#ytplayer")[0];
		ytplayer.addEventListener("onStateChange","wtf_yt_callback");            
		} 
	} 
	//END SOUND
	
    $('#homeslide').maximage({
        cycleOptions: {
            fx:'fade',
            speed: 800,
            timeout: 5200
        },
        onFirstImageLoaded: function(){
            $('#cycle-loader').hide();
            $('#homeslide').fadeIn('fast');
        }
    });
    
    $("#logo").css({opacity:0});
    
	$(".home a").click (function() {
		//alert("Ecco!");
		
		$('#homeslide').fadeIn("fast");
		$('#content').animate({
			opacity: 0
		}, 400, function() {
			$('#content').empty();
			$("#content").css({ opacity: 1 });
		});
		
		$(".menu").animate({
			left: '-153'
		}, 1000, function()
			{
				$("#closeNav").css("display","none");
				$("#openNav").css("display","block");
			}
		);
		
		$("#logo").animate({
			opacity: '1'
		}, 1000);
				
	});
	$(".news a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/news.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});
	$(".foals a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/foals.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});
	
	$(".contacts a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/contacts.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});
	
	$(".shipping_center .quarantine_facility a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/quarantine_facility.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});

	$(".shipping_center .about_us a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/about_us.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});

	$(".shipping_center .what_makes_us_different a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/what_makes_us_different.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});

	$(".shipping_center .our_services a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/our_services.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});

	$(".forsale a").click(function(){
		$.ajax({
			type: "post",
			url: "ajax/forsale.php",
			success: function(html,stato){
				$('#homeslide').fadeIn("fast");
				$('.fondoScheda').animate({
				opacity: 0
				}, 500);
				$('#content').empty();
				$('#content').html(html);
				$("#statoChiamata").text(stato);
				
				//$(".boxWrap").draggable();
				$(".boxWrap").draggable({ handle: "#dragHandle" });
				$('.box').jScrollPane();
				
				$(".closeItem").click(function() {
					$('.boxWrap').animate({
						opacity: 0
					}, 200,
						function() {
							$('.boxWrap').remove();
						});
				});
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		})
	});
	
	$(".standard a").click (function() {
		$('#content').animate({
				opacity: 0
			}, 400, function() {
				$('#homeslide').fadeIn("fast");
				$('#content').empty();
				$("#content").css({ opacity: 1 });
				$("#statoChiamata").text("");
			});
	});
	
});


function horses(genere,ramo){
	$.ajax({
		type: "post",
		url: "ajax/horses.php",
		data: {
			genere: genere,
			ramo: ramo	
		},
		success: function(html,stato){
			$('#homeslide').fadeIn("fast");
			$('.fondoScheda').animate({
				opacity: 0
				}, 500, function() {});
			$("#content").html(html);
			$("#statoChiamata").text(stato);
			
			//$(".boxWrap").draggable();
			$(".boxWrap").draggable({ handle: "#dragHandle" });
			$('.box').jScrollPane();
			
			$(".closeItem").click(function() {
				$('.boxWrap').animate({
					opacity: 0
				}, 200,
					function() {
						$('.boxWrap').remove();
  					});
			});
		},
		error : function (richiesta,stato,errori) {
			alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
		}
	});
}


function horse(id) {
	$.ajax({
		type: "post",
		url: "ajax/horse.php",
		data: {
			id: id	
		}, //restituisce il valore di select,
		success : function (data,stato) {
			$('.boxWrap').animate({
				opacity: 0
			}, 600,
				function() {
				$('.boxWrap').remove();
			});
			$('.horse').remove();
		
			$("#statoChiamata").text(stato);
			
			
			
			$("#content").append(data);
			if($('.fondoScheda img').length>0){
				$('#cycle-loader').show();
				$('#homeslide').fadeOut();
			}
			
			$('#scheda').css({ opacity: 0 });
			$('#scheda').animate({
				opacity: 1
				}, 600,
				function() {}
			);
		
		
		
			if($('.fondoScheda img').length>0){
				$('.fondoScheda').maximage({
					cycleOptions: {
						fx:     'fade',
						speed:  400,
						timeout: 5200,
						pager:  '#gallery_nav',
						pagerAnchorBuilder: function(idx,slide){
							return '<a href="javascript:void(0)" style="background:url('+$(slide).attr("thumb")+') center center;"></a>';
						}
					},
						
					/*cycleOptions: {
						fx:'fade',
						speed: 800,
						timeout: 0
					},*/
					onFirstImageLoaded: function(){
						$('.fondoScheda').fadeIn('fast',function(){$('#cycle-loader').hide();});
					}
				});
			}
		
		},
		error : function (richiesta,stato,errori) {
			alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
		}
	});
}
	
function scheda(id,scope){
	$.ajax({
		cache:false,
		dataType : 'text',
		type: "post",
		url : "ajax/scheda.php",
		data : {
			id: id,
			scope: scope
		},
		success : function (data,stato) {
			//alert(data);
			//var cache = data;
			$('.scheda').remove();
			$('.horse').append(data);
			$("#bt-" + scope).addClass('active');
			
			if(scope == 'gallery') {
				/*$('.fondoScheda').maximage({
					
					cycleOptions: {
						fx:     'fade',
						speed:  400,
						timeout: 5200,
						pager:  '#gallery_nav',
						pagerAnchorBuilder: function(idx,slide){
							return '<a href="javascript:void(0)" style="background:url('+$(slide).attr("thumb")+') center center;"></a>';
						}
					},
					onFirstImageLoaded: function(){
						$('.fondoScheda').fadeIn('fast');
					}
				});*/
				
				$("#gallery_nav").animate({
					right: '0'
				}, 1000,
				'easeOutCubic',
				function() {});
				
			} else {
				
				$("#gallery_nav").animate({
					right: '-250'
				}, 1000,
				'easeOutCubic',
				function() {});
				
				$('.fondoScheda').maximage({
					
					cycleOptions: {
						timeout: 0
					},
					onFirstImageLoaded: function(){
						$('.fondoScheda').fadeIn('fast');
					}
				});
			}
			
			//$(".boxWrap").draggable();
			$(".boxWrap").draggable({ handle: "#dragHandle" });
			$('.box').jScrollPane();
			$(".closeItem").click(function() {
				$('.boxWrap').animate({
					opacity: 0
				}, 200,
					function() {
						$('.boxWrap').remove();
  					});
			});
			
			if(scope == 'pedigree'){
				//$(".boxPedigree").draggable();
				$(".menu").animate({
							left: '-153'
						}, 1000, function()
							{
								$("#closeNav").css("display","none");
								$("#openNav").css("display","block");
							}
						);	
				$(".closeItem").click(function() {
					$('.boxPedigree').animate({
						opacity: 0
					}, 200,
						function() {
							$(".menu").animate({
								left: '0'
							}, 1000, 
							'easeOutCubic',
							function()
								{	
									$("#openNav").css("display","none");
									$("#closeNav").css("display","block");
								}
							);
							$('.boxPedigree').remove();
						});
				});
			}
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		});
}

function news(id) {
	$.ajax({
		type: "post",
		url: "ajax/news_detail.php",
		data: {
			id: id	
		}, //restituisce il valore di select,
		success : function (data,stato) {
			$('.boxWrap').animate({
				opacity: 0
			}, 600,
				function() {
				$('.boxWrap').remove();
			});
			$('.horse').remove();
		
			$("#statoChiamata").text(stato);
			
			
			
			$("#content").append(data);
			/*if($('.fondoScheda img').length>0){
				$('#cycle-loader').show();
				$('#homeslide').fadeOut();
			}*/
			
			$('#scheda').css({ opacity: 0 });
			$('#scheda').animate({
				opacity: 1
				}, 600,
				function() {
					schedanews(id,"info");	
				}
			);
		
			$("#gallery_nav a").click(function(){
					$.ajax({
						type: "post",
						url: "ajax/newsgallery.php",
						data: {
							show: true,
							id: $(this).attr("rel")	
						},
						success:function(html){
							if($('.boxWrap').length>0){
								$('.boxWrap').animate({
									opacity: 0
								}, 600,
									function() {
									$('.boxWrap').remove();
									//$('#content').empty();
									$('#content').append(html);
									
									//$(".boxWrap").draggable();
									$(".boxWrap").draggable({ handle: "#dragHandle" });
									$('.box').jScrollPane();
									
									$(".closeItem").click(function() {
										$('.boxWrap').animate({
											opacity: 0
										}, 200,
											function() {
												$('.boxWrap').remove();
											});
									});
								});
							}else{
				
								//$('#content').empty();
								$('#content').append(html);
								
								$(".boxWrap").draggable({ handle: "#dragHandle" });
								$('.box').jScrollPane();
								
								$(".closeItem").click(function() {
									$('.boxWrap').animate({
										opacity: 0
									}, 200,
										function() {
											$('.boxWrap').remove();
										});
								});
							}
						}
						
					});
				});
		
		},
		error : function (richiesta,stato,errori) {
			alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
		}
	});
}

function schedanews(id,scope){
	$.ajax({
		cache:false,
		dataType : 'text',
		type: "post",
		url : "ajax/schedanews.php",
		data : {
			id: id,
			scope: scope
		},
		success : function (data,stato) {
			//alert(data);
			if(scope == 'gallery') {
				$("#gallery_nav").animate({
					right: '0'
				}, 1000,
				'easeOutCubic',
				function() {});
			
				
			} else {
				
				$("#gallery_nav").animate({
					right: '-250'
				}, 1000,
				'easeOutCubic',
				function() {});
				
			}

			$('.scheda').remove();
			$('.horse').append(data);
			$("#bt-" + scope).addClass('active');
			
			//$(".boxWrap").draggable();
			$(".boxWrap").draggable({ handle: "#dragHandle" });
			$('.box').jScrollPane();
			$(".closeItem").click(function() {
				$('.boxWrap').animate({
					opacity: 0
				}, 200,
					function() {
						$('.boxWrap').remove();
  					});
			});
			
			},
			error : function (richiesta,stato,errori) {
				alert("E' evvenuto un errore. Lo status della chiamata è: " + stato);
			}
		});
}

