$(document).ready(function() {
   
   	// Tabs

	$('#hw-tabs').tabs();
	$('#home-tabs').tabs({
        show: function(event, ui) {
			tabImgPosition = $(document).width() / 2 - 480;
			tabContentPadding = $('#home-tabs div:not(".ui-tabs-hide, .welcome-tab") div img').width() + 50;
            $(ui.panel).find('h2')
                .removeAttr('style')
                .css({
                    'opacity': 0,
					'right': '-250px'
                })
                .animate({
                    'opacity': 1,
					'right': 0
                }, 400,'easeOutBack');
            $(ui.panel).find('div p')
                .removeAttr('style')
                .css({
                    'opacity': 0,
					'bottom': '-80px'
                })
                .animate({
                    opacity: 1,
					'bottom': 0
                }, 400,'easeOutBack');
            $(ui.panel).find('img:not(".welcome-tab img, .height-controller")').removeAttr('style')
                .css({
                    'opacity': 0,
                    'margin-top': '-55px',
					'left': tabImgPosition
                })
                .animate({
                    'opacity': 1,
                    'margin-top': 0
                }, 300,'easeOutBack');
			$(ui.panel).find('div').css({'paddingLeft': tabContentPadding, 'width': 920 - tabContentPadding});
			welcomeImgHeight = $('.welcome-tab ul li.disc img').outerHeight(true);
			$('.welcome-tab ul').css('height',welcomeImgHeight);
        }
    });

   	// Tabs | On resize scaling
		
	$(window).resize(function(){
		welcomeImgHeight = $('.welcome-tab ul li.disc img').outerHeight(true);
		$('.welcome-tab ul').css('height',welcomeImgHeight);
		tabContentPadding = $('#home-tabs div:not(".ui-tabs-hide, .welcome-tab") div img').width() + 50;
		$('.tab-content div').css({'paddingLeft': tabContentPadding, 'width': 920 - tabContentPadding});
		tabImgPosition = $(document).width() / 2 - 480;
		$('.tab-content div img').css('left',tabImgPosition);
		$('#home-tabs > div:not(".ui-tabs-hide")').css('height','auto');
		activeTabHeight = $('#home-tabs > div:not(".ui-tabs-hide")').height();
		$('#home-tabs .ui-tabs-hide').css('height',activeTabHeight);
	});
	
   	// Tabs | Welcome tab on load animation

	if ($.browser.msie && $.browser.version < 9) {
		
		$("#loader").css('display','none');			
		$('.welcome-tab ul').css('height','800px');
		$('.main-parole').animate({'marginTop':'0px'},700,'easeOutBack');
		$('.welcome-tab ul').animate({'minHeight':'0'},700);
		$('.welcome-tab p').animate({'marginTop':'0px'},700,'easeOutBack', function() {
			$('.welcome-tab').animate({'minHeight':'0'},750, function(){
			if ($('.welcome-tab').hasClass('ui-tabs-hide')){
				$('#home-tabs > div:not(".ui-tabs-hide")').css('height','auto');
				otherTabHeight1 = $('#home-tabs > div:not(".ui-tabs-hide")').height();
				$('#home-tabs .ui-tabs-hide').css('height',otherTabHeight1);				
			} else {
				welcomeTabHeight = $('.welcome-tab').height();
				$('#home-tabs > div').css('height',welcomeTabHeight);						
			};
			});
		});
		
		if ($('.welcome-tab').hasClass('ui-tabs-hide')){
			$('#home-tabs > div:not(".ui-tabs-hide")').css('height','auto');
			otherTabHeight = $('#home-tabs > div:not(".ui-tabs-hide")').height();
			$('#home-tabs .ui-tabs-hide').css('height',otherTabHeight);				
		};
			
		$('.welcome-tab ul li').css('display','none');
						
		$('.welcome-tab ul li.disc').css({
			'margin-top': '-50px',
			'display':'block'
		})
		.animate({
			'margin-top': '0px'
		}, 1000, function() {
			$('.welcome-tab ul li.music').css({
				'z-index':'3',
				'display':'block'
			}).animate({
				'marginLeft':'-29%'
			},400, 'easeOutBack');
			$('.welcome-tab ul li.film').css({
				'z-index':'3',
				'display':'block'
			}).animate({
				'marginLeft':'10.5%'
			},400, 'easeOutBack', function(){
				$('.welcome-tab ul li.photo').css({
					'margin-left':'-29%',
					'display':'block'
				}).animate({
					'marginLeft':'-48%'
				},400, 'easeOutBack');
				$('.welcome-tab ul li.text').css({
					'margin-left':'10.5%',
					'display':'block'
				}).animate({
					'marginLeft':'28%'
				},400, 'easeOutBack');
			});
		});
	} else {
		if( $('#home-tabs').length ) {
			
			var homeTabsImgs = $("#home-tabs img").map(function() {
				return $(this).attr("src");
			}).get();
	
			$(function() {
				$.preload(homeTabsImgs, {
					init: function(loaded, total) {
					},
					loaded: function(img, loaded, total) {
					},
					loaded_all: function(loaded, total) {
						$("#loader").fadeOut(100);			
						$('.welcome-tab ul').css('height','800px');
						$('.main-parole').animate({'marginTop':'0px'},700,'easeOutBack');
						$('.welcome-tab ul').animate({'minHeight':'0'},700);
						$('.welcome-tab p').animate({'marginTop':'0px'},700,'easeOutBack', function() {
							$('.welcome-tab').animate({'minHeight':'0'},750, function(){
							if ($('.welcome-tab').hasClass('ui-tabs-hide')){
								$('#home-tabs > div:not(".ui-tabs-hide")').css('height','auto');
								otherTabHeight1 = $('#home-tabs > div:not(".ui-tabs-hide")').height();
								$('#home-tabs .ui-tabs-hide').css('height',otherTabHeight1);				
							} else {
								welcomeTabHeight = $('.welcome-tab').height();
								$('#home-tabs > div').css('height',welcomeTabHeight);						
							};
							});
						});
						
						if ($('.welcome-tab').hasClass('ui-tabs-hide')){
							$('#home-tabs > div:not(".ui-tabs-hide")').css('height','auto');
							otherTabHeight = $('#home-tabs > div:not(".ui-tabs-hide")').height();
							$('#home-tabs .ui-tabs-hide').css('height',otherTabHeight);				
						};
						
						$('.welcome-tab ul li').css('opacity','0');
						
						$('.welcome-tab ul li.disc').css({
							'margin-top': '-50px'
						})
						.animate({
							'margin-top': '0px',
							'opacity':'1'
						}, 1000, function() {
							$('.welcome-tab ul li.music').css('z-index','3').animate({
								'marginLeft':'-29%',
								'opacity':'1'
							},400, 'easeOutBack');
							$('.welcome-tab ul li.film').css('z-index','3').animate({
								'marginLeft':'10.5%',
								'opacity':'1'
							},400, 'easeOutBack', function(){
								$('.welcome-tab ul li.photo').css('margin-left','-29%').animate({
									'marginLeft':'-48%',
									'opacity':'1'
								},400, 'easeOutBack');
								$('.welcome-tab ul li.text').css('margin-left','10.5%').animate({
									'marginLeft':'28%',
									'opacity':'1'
								},400, 'easeOutBack');
							});
						});
					}
				});
			});
		};
	};
	
	// Tab navigation
	
	$('.tab-nav li a').hover(function(){
		$(this).css('backgroundPosition','50% 27px').addClass('animated');
		$('.animated').stop().animate({'backgroundPosition':'50% 20px'},150, function(){
			$(this).stop().animate({'backgroundPosition':'50% 27px'},150);	
		});
		animationInterval = setInterval(function(){
			$('.animated').stop().animate({
				'backgroundPosition':'50% 20px'
			},150, function(){
				$(this).stop().animate({
					'backgroundPosition':'50% 27px'
				},150);
			});
		},1300);
	}, function() {
		$(this).removeClass('animated');
		clearInterval(animationInterval);
	});
	
	$('#footer .footer-tab-nav li a').click(function() {
		var index = $(this).parent().index();
		var li = $('#home-tabs ul.tab-nav li');
		$('a', li[index+1]).trigger('click');

		return false;
	});

   	// Login | On load animation
   
	if ($.browser.msie && $.browser.version < 9) {
		$('#login-holder form ul').css({	
			'marginTop':'-100px',
			'marginBottom':'200px'
		}).animate({
			'marginTop':'0px',
			'marginBottom':'15px'
		},1500, 'easeOutCubic');
	} else {
		$('#login-holder form ul, .login-button, .login-holder form a, .login-holder form p').css('opacity','0');
		$('#login-holder form ul').css({	
			'marginTop':'-100px',
			'marginBottom':'200px'
		}).animate({
			'marginTop':'0px',
			'marginBottom':'15px',
			'opacity':'1'
		},1500, 'easeOutCubic');
		$('#login-holder form p, #login-holder form a, .login-button').animate({'opacity':'1'},1500);
	};
	
   	// Signup | On load animation

	if ($.browser.msie && $.browser.version < 9) {
		$('.signup-holder h2, .signup-holder form, .signup-holder > div').css('display','block');
	} else {
		$('.signup-holder h2, .signup-holder form').fadeIn(800, function() {
			$('.signup-holder > div').fadeIn(800);
		});
	};

   	// Signup | Drag and drop tip tabs

	if ($.browser.msie && $.browser.version < 9) {
		$('#drag-and-drop-tip').tabs();
	} else {
		$('#drag-and-drop-tip').tabs({ fx: { opacity: 'toggle' } });
	};

	// Settings | On load animation
   	
	if ($.browser.msie && $.browser.version < 9) {
		$('.settings-column').css('display','block');
	} else {
		$('.settings-column:first').fadeIn(800, function(){
			$(this).next().fadeIn(800, function(){
				$(this).next().fadeIn(800);
			});
		});
	};
	
	// Settings | Accept all incoming files switcher

	$(".cb-enable").click(function(){
		var parent = $(this).parents('.settings-switch');
		$('.cb-disable',parent).removeClass('selected');
		$(this).addClass('selected');
		$('.checkbox',parent).attr('checked', true);
	});
	$(".cb-disable").click(function(){
		var parent = $(this).parents('.settings-switch');
		$('.cb-enable',parent).removeClass('selected');
		$(this).addClass('selected');
		$('.checkbox',parent).attr('checked', false);
	});

	// Download | On load animation

	if ($.browser.msie && $.browser.version < 9) {
	} else {
		$('.download h3, .download p, .download a, .download img').css('opacity','0');
		$('.download h3').animate({'opacity':'1'},400, function(){
			$('.download p').animate({'opacity':'1'},400, function(){
				$('.download a').animate({'opacity':'1'},400, function(){
					$('.download img').animate({'opacity':'1'},400);
				});			
			});
		});
	};
	
	// Features | On scroll animation

	if ($.browser.msie && $.browser.version < 9) {
		featuresListItemEven = $(".features-list li:even");
		featuresListItemEven.find('img').css({'marginLeft':'-150px'});
		featuresListItemOdd = $(".features-list li:odd");
		featuresListItemOdd.find('img').css({'marginRight':'-150px'});
	
		featuresListItemEven.each(function(i) {
			a = $(this).offset().top + $(this).height();
			b = $(window).scrollTop() + $(window).height();
			if (a < b) {
				$(this).find('img').animate({
					'marginLeft':'-20px'
				},500);
			};
		});
			
		$(window).scroll(function(d,h) {
			featuresListItemEven.each(function(i) {
				a = $(this).offset().top + $(this).height();
				b = $(window).scrollTop() + $(window).height();
				if (a < b) {
					$(this).find('img').animate({
						'marginLeft':'-20px'
					},500);
				};
			});
		});
	
		featuresListItemOdd.each(function(i) {
			a = $(this).offset().top + $(this).height();
			b = $(window).scrollTop() + $(window).height();
			if (a < b) {
				$(this).find('img').animate({
					'marginRight':'-20px'
				},500);
			};
		});
			
		$(window).scroll(function(d,h) {
			featuresListItemOdd.each(function(i) {
				a = $(this).offset().top + $(this).height();
				b = $(window).scrollTop() + $(window).height();
				if (a < b) {
					$(this).find('img').animate({
						'marginRight':'-20px'
					},500);
				};
			});
		});
	} else {
		featuresListItemEven = $(".features-list li:even").css({'opacity':'0'});
		featuresListItemEven.find('img').css({'marginLeft':'-150px'});
		featuresListItemEven.find('h3').css({'opacity':'0'});
		featuresListItemEven.find('p').css({'opacity':'0'});
		featuresListItemOdd = $(".features-list li:odd").css({'opacity':'0'});
		featuresListItemOdd.find('img').css({'marginRight':'-150px'});
		featuresListItemOdd.find('h3').css({'opacity':'0'});
		featuresListItemOdd.find('p').css({'opacity':'0'});
	
		featuresListItemEven.each(function(i) {
			a = $(this).offset().top + $(this).height();
			b = $(window).scrollTop() + $(window).height();
			if (a < b) {
				$(this).find('img').animate({
					'marginLeft':'-20px'
				},500);
				$(this).animate({
					'opacity':'1'
				},500, function() {
					$(this).find('h3').animate({'opacity':'1'},500);
					$(this).find('p').animate({'opacity':'1'},500);
				});
			};
		});
			
		$(window).scroll(function(d,h) {
			featuresListItemEven.each(function(i) {
				a = $(this).offset().top + $(this).height();
				b = $(window).scrollTop() + $(window).height();
				if (a < b) {
					$(this).find('img').animate({
						'marginLeft':'-20px'
					},500);
					$(this).animate({
						'opacity':'1'
					},500, function(){
						$(this).find('h3').animate({'opacity':'1'},500);
						$(this).find('p').animate({'opacity':'1'},500);
					});
				};
			});
		});
	
		featuresListItemOdd.each(function(i) {
			a = $(this).offset().top + $(this).height();
			b = $(window).scrollTop() + $(window).height();
			if (a < b) {
				$(this).find('img').animate({
					'marginRight':'-20px'
				},500);
				$(this).animate({
					'opacity':'1'
				},500, function(){
					$(this).find('h3').animate({'opacity':'1'},500);
					$(this).find('p').animate({'opacity':'1'},500);
				});
			};
		});
			
		$(window).scroll(function(d,h) {
			featuresListItemOdd.each(function(i) {
				a = $(this).offset().top + $(this).height();
				b = $(window).scrollTop() + $(window).height();
				if (a < b) {
					$(this).find('img').animate({
						'marginRight':'-20px'
					},500);
					$(this).animate({
						'opacity':'1'
					},500, function(){
						$(this).find('h3').animate({'opacity':'1'},500);
						$(this).find('p').animate({'opacity':'1'},500);
					});
				};
			});
		});
	};

	// Affiliate | On scroll animation
		
	if ($.browser.msie && $.browser.version < 9) {
		affiliateItem = $(".affiliate-entry > div").css({'paddingTop':'30px'});
	
		affiliateItem.each(function(i) {
			a = $(this).offset().top;
			b = $(window).scrollTop() + $(window).height();
			if (a < b) {$(this).animate({'paddingTop':'0'},500);};
		});
			
		$(window).scroll(function(d,h) {
			affiliateItem.each(function(i) {
				a = $(this).offset().top + $(this).height();
				b = $(window).scrollTop() + $(window).height();
				if (a < b) {$(this).animate({'paddingTop':'0'},500);};
			});
		});
	} else {
		affiliateItem = $(".affiliate-entry > div").css({'opacity':'0', 'paddingTop':'30px'});
	
		affiliateItem.each(function(i) {
			a = $(this).offset().top;
			b = $(window).scrollTop() + $(window).height();
			if (a < b) {$(this).animate({'opacity':'1', 'paddingTop':'0'},500);};
		});
			
		$(window).scroll(function(d,h) {
			affiliateItem.each(function(i) {
				a = $(this).offset().top + $(this).height();
				b = $(window).scrollTop() + $(window).height();
				if (a < b) {$(this).animate({'opacity':'1', 'paddingTop':'0'},500);};
			});
		});
	};

	// Affiliate | Position agree terms paragraph
	
	var agreeTermsMargin = (980 - $('.agree-terms').width())/2;
	$('.agree-terms').css('margin-left',agreeTermsMargin);

	// Contact | On load animation

	if ($.browser.msie && $.browser.version < 9) {
		$('.contact-list > li:first').find('img').animate({'left':'-20px'},400, function(){
			$(this).parent().next().find('img').animate({'left':'-20px'},400, function(){
				$(this).parent().next().find('img').animate({'left':'-20px'},400, function() {
					$(this).parent().next().find('img').animate({'left':'-20px'},400);
				});
			});
		});
	} else {
		$('.contact-list > li').css('opacity','0');
		$('.contact-list > li:first').find('img').animate({'left':'-20px'},400);
		$('.contact-list > li:first').animate({'opacity':'1'},400, function(){
			$(this).next().find('img').animate({'left':'-20px'},400);
			$(this).next().animate({'opacity':'1'},400, function(){
				$(this).next().find('img').animate({'left':'-20px'},400);
				$(this).next().animate({'opacity':'1'},400, function() {
					$(this).next().find('img').animate({'left':'-20px'},400);
					$(this).next().animate({'opacity':'1'},400);
				});
			});
		});
	};

   	// Payment | Tabs

	if ($.browser.msie && $.browser.version < 9) {
		$('.payment-tabs').tabs();
	} else {
		$('.payment-tabs').tabs({ fx: { opacity: 'toggle' } });
	};
	$('#expiration_month, #expiration_year, #country').dropdownized();

   	// Plans & pricing | Most popular column
	
	$('#pricing-table').find('.most-popular').each(function(){
		$(this).prev().addClass('most-popular-shadow-left');
		$(this).next().addClass('most-popular-shadow-right');
	});

   	// Plans & pricing | On load animation

	totalDots = $('#pricing-table').find('img').size();
	currentDot = 0; dotDelay = 0;
	
	if ($.browser.msie && $.browser.version < 9) {
	} else {
		$('#pricing-table').find('img').css('opacity','0');
		while(currentDot<totalDots) {
			$('#pricing-table').find('img').eq(currentDot).delay(dotDelay).animate({'opacity':'1'},800);
			dotDelay += 50; currentDot += 1;
		};
	};
	
   	// Terms & conditions, FAQ, Privacy policy | On load animation

	if ($.browser.msie && $.browser.version < 9) {
		$('.withPadding img').animate({'left':'-70px'},800);
	} else {
		$('.withPadding img')
		.css('opacity','0')
		.animate({
			'opacity':'1',
			'left':'-70px'
		},800);
	};
	
   	// FAQ & API | Scrolling
	
	$("#questions-list, .apiList").find('a').click(function(){
		var questionMark = $(this).attr('href');
		$('html,body').animate({scrollTop: $(questionMark).offset().top},'slow');
		return false;
	});

	// Custon checkbox
	
	$("input[type=checkbox],input[type=radio]").checkBox();

});

$(window).load(function(){
	welcomeImgHeight = $('.welcome-tab ul li.disc img').outerHeight(true);
	$('.welcome-tab ul').css('height',welcomeImgHeight);
	tabContentPadding = $('#home-tabs div:not(".ui-tabs-hide, .welcome-tab") div img').width() + 50;
	$('.tab-content div').css({'paddingLeft': tabContentPadding, 'width': 920 - tabContentPadding});
	tabImgPosition = $(document).width() / 2 - 480;
	$('.tab-content div img').css('left',tabImgPosition);
});

(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
		}
	});
})(jQuery);
