// execute your scripts when the DOM is ready. this is a good habit 
$(document).ready(function(){

	$("#buymetrogadget").click(function() {
		$("#buymetrogadgetForm").slideToggle();
	});

	$(".lang-selected").click(function() {
		$(".lang-dropdown").slideToggle();
	});
	
	// Show Invite Friend popup
	
	$("#inviteFriendLink").click(function(){
		$(".overlay").show();
		$(".iFriendPopup").show();
	});
	
	// Close Invite Friend popup by fake submit button
	
	$("#ifSendButton").click(function(){
		$(".overlay").hide();
		$(".iFriendPopup").hide();
	});
	
	// Close Invite Friend popup by cancel button
	
	$("#ifCancelButton").click(function(){
		$(".overlay").hide();
		$(".iFriendPopup").hide();
	});

    // initialize scrollable
    var api = $("div.scrollable").scrollable({
		size: 4,
		next: ".arrowRight",
		prev: ".arrowLeft"
    }).mousewheel({api: true});
	
	// onload move to position 2
	if (api) {
		api.seekTo(2);
		$(".selected").animate( {left: "232px", width: "466px"} );
		
		// set scroll position onclick links
		$("#z1").click(function() {api.seekTo(0);});
		$("#z2").click(function() {api.seekTo(1);});
		$("#z3").click(function() {api.seekTo(2);});
		$("#z4").click(function() {api.seekTo(3);});
		$("#z5").click(function() {api.seekTo(4);});
		$("#z6").click(function() {api.seekTo(5);});
		$("#z7").click(function() {api.seekTo(6);});
		$("#z8").click(function() {api.seekTo(7);});
	
		// set background position
		api.onStart(function() {
			scrollPosition = this.getIndex();
			if (scrollPosition == 0) {
				$(".selected").animate( {left: "0px", width: "460px"} );
			}
			if (scrollPosition == 1) {
				$(".selected").animate( {left: "115px", width: "475px"} );
			}
			if (scrollPosition == 2) {
				$(".selected").animate( {left: "232px", width: "466px"} );
			}
			if (scrollPosition == 3) {
				$(".selected").animate( {left: "350px", width: "455px"} );
			}
			if (scrollPosition > 3) {
				$(".selected").animate( {left: "460px", width: "470px"} );
			}
		});
	}
	
	/* Show importVCF popup */
	$('#importVCF').click(
		function () {
			$('.overlay').show();
			$('#importVCF-window').show();
		}
	);
	
	/* Show importPST popup */
	$('#importPST').click(
		function () {
			$('.overlay').show();
			$('#importVCF-window').show();
		}
	);
	
	$('.cancelImport').click(function() {
			$('.overlay').hide();
			$('.popup-window').hide();
			$('.popup-content').hide();
			$('.sel-user-import').hide();
			$('.file-selector').show();
			$('.popup-content .after-form').hide();
	});
	
	$('.loadFile').click(function(){
						
		$(this).parents('div[id]:first').children('.file-selector').hide();
		$(this).parents('div[id]:first').children('.file-preloader').show();

		var self = this;
		
		setTimeout(function() {
			setTimeout(function() {
				$(self).parents('div[id]:first').children('.file-preloader').hide();
				$(self).parents('div[id]:first').children('.popup-content').show();
			}, 3000);
		});
		
	});
	
	$('#selSkypeUsers').change(function(){
		$(this).parents('div[id]:first').children().children('.sel-user-import').slideUp();
		$(this).parents('div[id]:first').children().children('.f-inputs').slideUp();
		$(this).parents('div[id]:first').children().children('.sel-user-import').slideDown();
		$(this).parents('div[id]:first').children().children('.f-inputs').slideDown();
	});
	
	$('.ordered .f-row').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	$('.ordered .i-small').focus(
		function () {
			if ($(this).val() == "Quick Dial") {
				$(this).val('');
			}
		}
	);
	
	$('.ordered .i-small').blur(
		function() {
			if ($(this).val() == "") {
				$(this).val('Quick Dial');
			}
		}
	);
	
	
	$('.actionedTable TR').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	
	
	
	$(document).pngFix();
	
	resizeOverlay();
	resizeContactlist();	
	
	if($.browser.msie && $.browser.version=="6.0") {
		$('.overlay').html('<iframe frameborder="0"></iframe>');
	}
	
	$('.tabsComplex A').click(function(){
		window.location.href = this.href;
	});
	
	$('.datepicker').datepicker({
		showOn: 'button',
		buttonImage: '/images-nd/ico-calendar.gif',
		buttonImageOnly: true
	});
	
		
});

function resizeOverlay() {
	$(".overlay").height(($(".body").height() > $(document).height())?$(".body").height():$(document).height());
}

function resizeContactlist() {
	
	var cdHeight = $('.contact-details').height();
	var clHeight = $('.list-names ul').height();

	if (cdHeight > clHeight) {
		$('.list-names ul').height(cdHeight);
	}
	
}