$().ready(function() {

	$('.pagesettings_new').click(function() {

//        console.log(window.pagedata);

		$('.userMenu ul').hide();

		if ($('.dialog.current').length) return false;
		var popup=$('.dialog.psettings').clone().addClass('current').appendTo('body').css('left','50%').css('margin-left','-260px').css('top','45px');
		$('.dialog.current a.close, .dialog.current span.cancel a').click(function() {
			$('.dialog.current').remove();
			$('#mask').hide();
			return false;
		});
		$('#mask').css('width',$(document).width()+'px').css('height',$(document).height()+'px').show().click(function(){
			$('.dialog.current').remove();
			$('#mask').hide();
		});

		for (a in window.allthemes) {
			var theme=$('.dialog.current .themepreview.prototype').clone().appendTo('.dialog.current .allthemes').removeClass('prototype').show();
			theme.html('<img src="'+window.base+'/thumbs/phpThumb.php?src='+window.base+'/res/images/' + window.allthemes[a].pic + '&w=72&h=72&q=100&zc=C">');
			theme.attr('index',a);
//			if (!window.pagedata.custom_theme) {
			if (window.pagedata.theme_id==window.allthemes[a].id) {
				theme.addClass('active');
				window.pagedata.ctheme=a;
			}
//			}
		}

		$('.dialog.current .sbutton').click(function(){
			if ($(this).hasClass('active')) {
				$('.dialog.current .tabset').hide();
				$('.dialog.current .sbutton').removeClass('active');
			} else {
				$('.dialog.current .tabset').hide();
				$('.dialog.current .sbutton').removeClass('active');
				$('.dialog.current .tabset.'+$(this).attr('area')).show();
				$(this).addClass('active');
			}
		});

		$('.dialog.current .file-uploader').attr('id','file-uploader');

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.background_image && window.pagedata.custom_theme.background_image!='none') {
			$('.dialog.current .previewer.photopreview').html('<img src="'+window.base+'/thumbs/phpThumb.php?src='+window.base+'/uploads/' + window.pagedata.custom_theme.background_image + '&w=95&h=95&q=100&zc=C">');
			$('.dialog.current .background_image').val(window.pagedata.custom_theme.background_image);
		} else if (!window.pagedata.custom_theme.background_image) {
			$('.dialog.current .previewer.photopreview').html('<img src="'+window.base+'/thumbs/phpThumb.php?src='+window.allthemes[window.pagedata.ctheme].background_image + '&w=95&h=95&q=100&zc=C">');
		} else if (window.pagedata.custom_theme.background_image && window.pagedata.custom_theme.background_image=='none') {
			$('.dialog.current input[name=nobackground]').attr('checked',true);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.background_position) {
			$('.dialog.current .background_settings').val(window.pagedata.custom_theme.background_position);
		} else {
			$('.dialog.current .background_settings').val(window.allthemes[window.pagedata.ctheme].background_position);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.tile==1) {
			$('.dialog.current .tile').attr('checked','1');
		} else if(window.allthemes[window.pagedata.ctheme].tile==1) {
			$('.dialog.current .tile').attr('checked','1');
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.opacity) {
			$('.dialog.current .opacity').val(window.pagedata.custom_theme.opacity);
			$('.dialog.current .copacity').html(window.pagedata.custom_theme.opacity);
		} else {
			$('.dialog.current .opacity').val(window.allthemes[window.pagedata.ctheme].opacity);
			$('.dialog.current .copacity').html(window.allthemes[window.pagedata.ctheme].opacity);
		}

		//COLORS
		if (window.pagedata.custom_theme && window.pagedata.custom_theme.colors_background) {
			$('.dialog.current .pick.colors_background').attr('color',window.pagedata.custom_theme.colors_background);
			$('.dialog.current .pick.colors_background').css('background-color','#'+window.pagedata.custom_theme.colors_background);
			$('.dialog.current .pick.colors_background').parent().find('input').val(window.pagedata.custom_theme.colors_background);
		} else {
			$('.dialog.current .pick.colors_background').attr('color',window.allthemes[window.pagedata.ctheme].colors_background);
			$('.dialog.current .pick.colors_background').css('background-color','#'+window.allthemes[window.pagedata.ctheme].colors_background);
			$('.dialog.current .pick.colors_background').parent().find('input').val(window.allthemes[window.pagedata.ctheme].colors_background);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.colors_icons) {
			$('.dialog.current .pick.colors_icons').attr('color',window.pagedata.custom_theme.colors_icons);
			$('.dialog.current .pick.colors_icons').css('background-color','#'+window.pagedata.custom_theme.colors_icons);
			$('.dialog.current .pick.colors_icons').parent().find('input').val(window.pagedata.custom_theme.colors_icons);
		} else {
			$('.dialog.current .pick.colors_icons').attr('color',window.allthemes[window.pagedata.ctheme].colors_icons);
			$('.dialog.current .pick.colors_icons').css('background-color','#'+window.allthemes[window.pagedata.ctheme].colors_icons);
			$('.dialog.current .pick.colors_icons').parent().find('input').val(window.allthemes[window.pagedata.ctheme].colors_icons);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.colors_text) {
			$('.dialog.current .pick.colors_text').attr('color',window.pagedata.custom_theme.colors_text);
			$('.dialog.current .pick.colors_text').css('background-color','#'+window.pagedata.custom_theme.colors_text);
			$('.dialog.current .pick.colors_text').parent().find('input').val(window.pagedata.custom_theme.colors_text);
		} else {
			$('.dialog.current .pick.colors_text').attr('color',window.allthemes[window.pagedata.ctheme].colors_text);
			$('.dialog.current .pick.colors_text').css('background-color','#'+window.allthemes[window.pagedata.ctheme].colors_text);
			$('.dialog.current .pick.colors_text').parent().find('input').val(window.allthemes[window.pagedata.ctheme].colors_text);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.colors_modules) {
			$('.dialog.current .pick.colors_modules').attr('color',window.pagedata.custom_theme.colors_modules);
			$('.dialog.current .pick.colors_modules').css('background-color','#'+window.pagedata.custom_theme.colors_modules);
			$('.dialog.current .pick.colors_modules').parent().find('input').val(window.pagedata.custom_theme.colors_modules);
		} else {
			$('.dialog.current .pick.colors_modules').attr('color',window.allthemes[window.pagedata.ctheme].colors_modules);
			$('.dialog.current .pick.colors_modules').css('background-color','#'+window.allthemes[window.pagedata.ctheme].colors_modules);
			$('.dialog.current .pick.colors_modules').parent().find('input').val(window.allthemes[window.pagedata.ctheme].colors_modules);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.colors_modules2) {
			$('.dialog.current .pick.colors_modules2').attr('color',window.pagedata.custom_theme.colors_modules2);
			$('.dialog.current .pick.colors_modules2').css('background-color','#'+window.pagedata.custom_theme.colors_modules2);
			$('.dialog.current .pick.colors_modules2').parent().find('input').val(window.pagedata.custom_theme.colors_modules2);
		} else {
			$('.dialog.current .pick.colors_modules2').attr('color',window.allthemes[window.pagedata.ctheme].colors_modules2);
			$('.dialog.current .pick.colors_modules2').css('background-color','#'+window.allthemes[window.pagedata.ctheme].colors_modules2);
			$('.dialog.current .pick.colors_modules2').parent().find('input').val(window.allthemes[window.pagedata.ctheme].colors_modules2);
		}

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.colors_links) {
			$('.dialog.current .pick.colors_links').attr('color',window.pagedata.custom_theme.colors_links);
			$('.dialog.current .pick.colors_links').css('background-color','#'+window.pagedata.custom_theme.colors_links);
			$('.dialog.current .pick.colors_links').parent().find('input').val(window.pagedata.custom_theme.colors_links);
		} else {
			$('.dialog.current .pick.colors_links').attr('color',window.allthemes[window.pagedata.ctheme].colors_links);
			$('.dialog.current .pick.colors_links').css('background-color','#'+window.allthemes[window.pagedata.ctheme].colors_links);
			$('.dialog.current .pick.colors_links').parent().find('input').val(window.allthemes[window.pagedata.ctheme].colors_links);
		}
		//END COLORS

		var offset=(350-25)/100 * parseInt($('.dialog.current .opacity').val())+25;
		$('.dialog.current .sliderhead').css('left',offset+'px');

		if (window.pagedata.custom_theme && window.pagedata.custom_theme.font) {
			$('.dialog.current .font').val(window.pagedata.custom_theme.font);
		} else {
			$('.dialog.current .font').val(window.allthemes[window.pagedata.ctheme].font);
		}

		var uploader = new qq.FileUploader({
			element: document.getElementById('file-uploader'),
			allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
			action: window.base+'/res/client/php.php',
			onComplete: function(id, fileName, responseJSON){
				$('.dialog.current input[name=background_image]').val(responseJSON.file);
				$('.dialog.current input[name=nobackground]').attr('checked',false);
				$('.dialog.current .previewer').html('<img src="'+window.base+'/thumbs/phpThumb.php?src='+window.base+'/uploads/' + responseJSON.file + '&w=100&h=100&q=100&zc=C">');
			}
		});

		$('.qq-upload-button').addClass('setuploader');

		$('.dialog.current .pick').each(function(){
			$(this).parent().find('input').val($(this).attr('color'));
		});

		$('.dialog.current .pick').ColorPicker({
			onSubmit: function(hsb, hex, rgb, el) {
				window.pick.attr('color',hex);
				window.pick.css('background','#'+hex);
				window.pick.parent().find('input').val(hex);
				$(el).ColorPickerHide();
			},
			onChange: function(hex, hex2) {
				window.pick.attr('color',hex2);
				window.pick.css('background','#'+hex2);
				window.pick.parent().find('input').val(hex2);
			},
			onBeforeShow: function () {
				window.pick=$(this);
				$(this).ColorPickerSetColor($(this).attr('color'));
			}
		})
		.bind('keyup', function(){
			$(this).ColorPickerSetColor($(this).attr('color'));
		});

		$('.dialog.current .sliderhead').css('left',Math.floor((350-25)/100*$('.dialog.current input.opacity').val())+25+'px');

		function updateOpacity() {
			var offset=Math.floor((parseInt($('.dialog.current .sliderhead').css('left'))-25) / (350-25) * 100);
			$('.dialog.current input.opacity').val(offset);
			$('.dialog.current .copacity').html(offset);
//			console.log(parseInt($('.dialog.current .sliderhead').css('left')));
		}

		window.sliding=false;
		$('.dialog.current .sliderhead').mousedown(function() {
			window.sliding=true;
			$(document).unbind().mousemove(function(e){
				var offset=e.pageX-$('.dialog.current .sliderline').offset().left;
				updateOpacity();
				if (offset < 25) offset=25;
				if (offset > 350) offset=350;
				$('.dialog.current .sliderhead').css('left',offset+'px');
				return false;
			});
			$(document).mouseup(function() {
				window.sliding=false;
				$(document).unbind();
				updateOpacity();
			});
			return false;
		});

		$('.dialog.current .sliderline').click(function(e) {
			var offset=e.pageX-$('.dialog.current .sliderline').offset().left;
			if (offset < 25) offset=25;
			if (offset > 350) offset=350;
			$('.dialog.current .sliderhead').css('left',offset+'px');
			updateOpacity();
			return false;
		});

		$('.dialog.current .themepreview').click(function(){
			$('.dialog.current .themepreview').removeClass('active');
			$(this).addClass('active');

			//opacity
			$('.dialog.current input.opacity').val(window.allthemes[$(this).attr('index')].opacity);
			$('.dialog.current .sliderhead').css('left',Math.floor((350-25)/100*$('.dialog.current input.opacity').val())+25+'px');

			//bg image
			$('.dialog.current .previewer.photopreview').html('<img src="'+window.base+'/thumbs/phpThumb.php?src='+window.allthemes[$(this).attr('index')].background_image + '&w=95&h=95&q=100&zc=C">');
			$('.dialog.current input.background_image').val('');
			$('.dialog.current input[name=nobackground]').attr('checked',false);
			$('.dialog.current input.tile').attr('checked',false);
			$('.dialog.current select.background_settings').val(window.allthemes[$(this).attr('index')].background_position);

			//colors
			$('.dialog.current .pick.colors_links').attr('color',window.allthemes[$(this).attr('index')].colors_links);
			$('.dialog.current .pick.colors_links').css('background-color','#'+window.allthemes[$(this).attr('index')].colors_links);
			$('.dialog.current .pick.colors_links').parent().find('input').val(window.allthemes[$(this).attr('index')].colors_links);

			$('.dialog.current .pick.colors_background').attr('color',window.allthemes[$(this).attr('index')].colors_background);
			$('.dialog.current .pick.colors_background').css('background-color','#'+window.allthemes[$(this).attr('index')].colors_background);
			$('.dialog.current .pick.colors_background').parent().find('input').val(window.allthemes[$(this).attr('index')].colors_background);

			$('.dialog.current .pick.colors_text').attr('color',window.allthemes[$(this).attr('index')].colors_text);
			$('.dialog.current .pick.colors_text').css('background-color','#'+window.allthemes[$(this).attr('index')].colors_text);
			$('.dialog.current .pick.colors_text').parent().find('input').val(window.allthemes[$(this).attr('index')].colors_text);

			$('.dialog.current .pick.colors_modules').attr('color',window.allthemes[$(this).attr('index')].colors_modules);
			$('.dialog.current .pick.colors_modules').css('background-color','#'+window.allthemes[$(this).attr('index')].colors_modules);
			$('.dialog.current .pick.colors_modules').parent().find('input').val(window.allthemes[$(this).attr('index')].colors_modules);

			$('.dialog.current .pick.colors_modules2').attr('color',window.allthemes[$(this).attr('index')].colors_modules2);
			$('.dialog.current .pick.colors_modules2').css('background-color','#'+window.allthemes[$(this).attr('index')].colors_modules2);
			$('.dialog.current .pick.colors_modules2').parent().find('input').val(window.allthemes[$(this).attr('index')].colors_modules2);

			$('.dialog.current .pick.colors_icons').attr('color',window.allthemes[$(this).attr('index')].colors_icons);
			$('.dialog.current .pick.colors_icons').css('background-color','#'+window.allthemes[$(this).attr('index')].colors_icons);
			$('.dialog.current .pick.colors_icons').parent().find('input').val(window.allthemes[$(this).attr('index')].colors_icons);
			$('.dialog.current .theme_id').val(window.allthemes[$(this).attr('index')].id);
		});

		$('.dialog.current .theme_id').val(window.allthemes[window.pagedata.ctheme].id);

        if (window.pagedata.custom_theme.columns && window.pagedata.custom_theme.columns == 2) { $('select[name=columns]').val(2); }

		popup.show();
	});


//	$('.pagesettings').click();

	// USERMENY

	$('.userMenu .username').click(function(){
		$('.userMenu ul').toggle();
		if ($('.userMenu ul').css('display')=='block') {
			$('.userMenu ul').unbind();
			$('.userMenu ul').mouseleave(function(){
				$('.userMenu ul').hide();
			});
		}
	});

	var hash=document.location.hash;

	if (hash=='#preferences') {
		document.location.hash='';
		$('.pagesettings_new').click();
	}


	// MY INFO
	$('.myinfo_new').click(function(){
		$('.userMenu ul').hide();
		if ($('.dialog.current').length) return false;
		var dialog=$('.dialog.myinfo').clone();
		dialog.find('.close, .skip').click(function(){
			$('.dialog.current').remove();
		});
		dialog.addClass('current').appendTo('body').show();
		dialog.css('top','50%');
		dialog.css('margin-top','-'+Math.floor(dialog.height()/2)-20+'px');
		// ADJUST DAYS
		dialog.find('.s-month').change(function() {
			var days = $(this).find('option:selected').attr('days');
			$('.dialog.current .s-day option').show();
			var i=0;
			$('.dialog.current .s-day option').each(function(){
				i++;
				if (i>days) $(this).hide();
			});
			if ($('.dialog.current .s-day option:selected').val() > days) $('.dialog.current .s-day').val(days);
		});
		// ADJUST MONTHS
		dialog.find('.s-year').change(function() {
			if ($(this).find('option:selected').val() % 4 == 0) {
				$('.dialog.current .s-month option[value=February]').attr('days',29);
			} else {
				$('.dialog.current .s-month option[value=February]').attr('days',28);
			}
			dialog.find('.s-month').change();
		});
		dialog.find('.s-year').change();
	});

	if (hash=='#myinfo') {
		document.location.hash='';
		$('.myinfo_new').click();
	}


	$('.dialog.current .collegeselect').change(function(){
		$('.dialog.current input[name=college]').val($('.dialog.current .collegeselect option:selected').val());
		if ($('.dialog.current .collegeselect option:selected').val()=='-1') {
			$('.dialog.current .collegeselect').remove();
			$('.dialog.current input[name=college]').remove();
			$('.dialog.current .welcome-icon.college').before($('<input type="text" name="college" value="">'));
		}
	});

	$('.dialog.current .employerselect').change(function(){
		$('.dialog.current input[name=employer]').val($('.dialog.current .employerselect option:selected').val());
		if ($('.dialog.current .employerselect option:selected').val()=='-1') {
			$('.dialog.current .employerselect').remove();
			$('.dialog.current input[name=employer]').remove();
			$('.dialog.current .welcome-icon.employer').before($('<input type="text" name="employer" value="">'));
		}
	});

	if ($('.dialog.current .collegeselect').length) $('.dialog.current input[name=college]').val($('.dialog.current .collegeselect option:selected').val());
	if ($('.dialog.current .employerselect').length) $('.dialog.current input[name=employer]').val($('.dialog.current .employerselect option:selected').val());

	
});
