var START_WIZARD = {
	initModule : function(moduleName) {
		if (this[moduleName]) this[moduleName].Init();
	}
}

START_WIZARD.DESIGN_CHOOSER = function() {
	var init = function() {
		$('.image_holder, .design_choose_radio').click(function() {
			$(this)
				.parents('.design')
					.siblings()
						.find('.image_holder').removeClass('selected_design')
					.end()
				.end()
					.find('.image_holder').addClass('selected_design')
				.end()
					.find(':radio').attr('checked', true);
		});
	}
	
	return {
		Init : init
	}
}();


START_WIZARD.THEMES_CHOOSER = function(){
	var _chooserType = '';
	
	var colorsList = [];
	var themesList = [];
	
	var init = function() {
		_chooserType = $('.themes_chooser_type').val() || 'Standard';
		
		colorsList = eval($('.colors_list').text());
		themesList = eval($('.themes_list').text());
		
		initThemeChooser();
		initColorChooser();
	}
	
	var getColorIndexById = function(colorId){
		for (var i = 0; i < colorsList.length; i++) {
			var color = colorsList[i];
			if(color.id == colorId)
				return i;
		}
	}
	
	var getThemeIndexById = function(themeId){
		for (var i = 0; i < themesList.length; i++) {
			var theme = themesList[i];
			if(theme.id == themeId)
				return i;
		}
	}

	var initColorChooser = function() {
		var $colorChooser = $('.color_chooser');
		for (var i = 0; i < colorsList.length; i++) {
			var color = colorsList[i];
			var $color = $('<li>').attr('id', color.id).appendTo($colorChooser);
			
			var $colorPalette = $('<div>').append(
				$('<span>').addClass('color_body').css('background-color', '#'+color.colors[0])
			).append(
				$('<span>').addClass('color_content').css('background-color', '#'+color.colors[1])
			).append(
				$('<span>').addClass('color_header').css('background-color', '#'+color.colors[2])
			).append(
				$('<span>').addClass('color_menu').css('background-color', '#'+color.colors[3])
			);
			
			
			if (_chooserType == 'Wide') {
				$colorPalette.addClass('narrow_palette').children('.color_header').hide();
			}
			$colorPalette.appendTo($color);
			
			$('<p>').addClass('color_title').text(color.title).appendTo($color);
		}
		
		$colorChooser.cycle({
			fx : 'scrollHorz',
			timeout : 0,
			speed : 400,
			startingSlide : getColorIndexById($('.selected_color_id').val()),
			prev : '.color_chooser_container .prev_arrow',
			next : '.color_chooser_container .next_arrow',
			after : function(curr, next, opt, fwd) {
				var $current = $(next);
				$('.selected_color_id').val($current.attr('id'));
				
				var bodyColor = $current.find('.color_body').css('background-color');
				$('.theme_chooser').find('.theme_body').css('background-color', bodyColor);
				var menuColor = $current.find('.color_menu').css('background-color');
				$('.theme_chooser').find('.theme_menu').css('background-color', menuColor);
				var contentColor = $current.find('.color_content').css('background-color');
				$('.theme_chooser').find('.theme_content').css({
					'background-color' : contentColor,
					'border-right' : '1px solid '+ menuColor
				});
				var headerColor = $current.find('.color_header').css('background-color');
				$('.theme_chooser').find('.theme_header').css({
					'background-color' : headerColor
				});
				
				if (_chooserType == 'Right') {
					$('.theme_chooser').find('.header_gradient').css('background-color', headerColor);
				}
			}
		});
	}
	
	var initThemeChooser = function() {
		var $themeChooser = $('.theme_chooser');
		
		var themeImagesPath = $('.image_url').val();
		var themeHeadlineText = $('.headline_txt').val();
		var themeImageUrl = $('.theme_image').val();
		
		if (_chooserType == 'Right') {
			var photoUrl = $('.header_image').text();
			var $photo = $('<img />').attr({
					src : photoUrl,
					width : 57,
					height : 38,
					sizingType : 'scale'
				}).addClass('photo_right');
		}
		if (_chooserType == 'Wide') {
			var photoUrl = $('.header_image').text();
			$photo = $('<img />').attr({
				src : photoUrl,
				width : 188,
				height : 38,
				sizingType : 'scale'
			}).addClass('photo_wide');
		}
		
		for (var i = 0; i < themesList.length; i++) {
			var theme = themesList[i];
			var $theme = $('<li>').attr('id', theme.id).addClass(theme.stylename).appendTo($themeChooser);
			
			var $themeBody = $('<div>').addClass('theme_body').appendTo($theme);
			var $themeContent = $('<div>').addClass('theme_content').appendTo($theme);
			var	$themeHeader = $('<div>').addClass('theme_header').appendTo($theme);
			var $themeHeadline = $('<div>').addClass('theme_headline').html(themeHeadlineText).appendTo($theme);
			
			$('<div>').addClass('theme_menu').appendTo($theme);
			$('<p>').addClass('theme_title').text(theme.title).appendTo($theme);
			$('<div>').addClass('theme_design').css('background', 'url('+themeImagesPath+theme.image+') no-repeat 0 0').appendTo($theme);
			
			if (_chooserType == 'Standard') {
				$themeHeadline.addClass('theme_headline_standard');
			}
			if (_chooserType == 'Right') {
				$themeHeader.addClass('theme_header_photoright');
				$themeHeadline.addClass('theme_headline_photoright');
				$('<div>').addClass('header_gradient').appendTo($theme);
				$photo.clone().appendTo($theme);
			}
			if (_chooserType == 'Wide') {
				$themeHeader.addClass('theme_header_wide');
				$themeHeadline.addClass('theme_headline_wide');
				$photo.clone().appendTo($theme);
				$('<div>').addClass('photo_line').css('opacity', 0.5).appendTo($theme);
			}
			
			if ($.trim(themeImageUrl).length)
				$('<img>').attr('src', themeImageUrl).addClass('theme_image').appendTo($theme);
		}
		
		$themeChooser.cycle({
			fx : 'scrollHorz',
			timeout : 0,
			speed : 400,
			startingSlide : getThemeIndexById($('.selected_theme_id').val()),
			prev : '.theme_chooser_container .prev_arrow',
			next : '.theme_chooser_container .next_arrow',
			after : function(curr, next, opt, fwd) {
				$('.selected_theme_id').val($(next).attr('id'));
			}
		});
	}
	
	return {
		Init : init
	}
}();


START_WIZARD.JCROP = function() {
	var imgWidth, imgHeight, imgWidthScaled, imgHeightScaled, rectWidth, rectHeight = 0;
	var jcrop_api = null;
	
	var init = function() {
		$('.header_line').css('opacity', .4);
		
		imgWidth = parseInt($('.img_width').val());
		imgHeight = parseInt($('.img_height').val());
		imgWidthScaled = parseInt($('.img_width_scaled').val());
		imgHeightScaled = parseInt($('.img_height_scaled').val());
		
		rectWidth = parseInt($('.jcrop_image_holder').width());
		rectHeight = parseInt($('.jcrop_image_holder').height());
		
		$('.jcrop_outer').css('top', Math.round((rectHeight - imgHeightScaled) / 2));
		
		var selWidth = imgWidth;
		var selHeight = Math.round(selWidth * 122 / 595);
		
		var initCoords = {
			x : 0,
			y : Math.round((imgHeight - selHeight) / 2),
			x2 : selWidth,
			y2 : Math.round((imgHeight - selHeight) / 2) + selHeight,
			w : selWidth,
			h : selHeight
		}
		
		jcrop_api = $.Jcrop('.crop_image', {
			onChange : showPreview,
			onSelect : setCoords,
			aspectRatio: 595/122,
			bgOpacity: .4,
			trueSize: [imgWidth, imgHeight],
			minSize: [110, 22]
		});
		jcrop_api.setSelect([initCoords.x, initCoords.y, initCoords.x2, initCoords.y2]);
		jcrop_api.animateTo([initCoords.x, initCoords.y, initCoords.x2, initCoords.y2]);
		setCoords(initCoords);
		
		setTipBoxPositions();
	}
	
	var setCoords = function(coords) {
		if (coords.w < 110){
			var x1 = $('.cropX').val();
			var y1 = $('.cropY').val();
			var x2 = x1 + $('.cropW').val();
			var y2 = y1 + $('.cropH').val();
			jcrop_api.setSelect([x1, y1, x2, y2]);
		}
		else{
			if(coords.w > 0 && coords.h > 0){
				$('.cropX').val(coords.x);
				$('.cropY').val(coords.y);
				$('.cropW').val(coords.w);
				$('.cropH').val(coords.h);
			}	
		}
	}
	
	var showPreview = function(coords) {
		if (parseInt(coords.w) > 0 && parseInt(coords.h) > 0) {
			var rx = 595 / parseInt(coords.w);
			var ry = 122 / parseInt(coords.h);
			
			if(imgWidth >= 0 && imgHeight >= 0)
			{
				$('.preview_image').css({
					width : Math.round(rx * imgWidth),
					height : Math.round(ry * imgHeight),
					marginLeft : -(Math.round(rx * coords.x)),
					marginTop : -(Math.round(ry * coords.y))
				});
			}
		}
	}
	
	var setTipBoxPositions = function() {
		$('.jcrop_tip_box')
			.css({
				top : $('.jcrop_inner').offset().top + 10,
				left : $('.jcrop_inner').offset().left + $('.jcrop_inner').width() + 5
			})
			.appendTo('body')
			.show();
	}
	
	return {
		Init : init
	}
}();


START_WIZARD.PAGE_CONTENT = function() {
    var init = function() {
        if (!$(".default_images_list").length) return false;

        var defaultImagesList = $(".default_images_list li");
        var ownImage = defaultImagesList.filter(":last");

        var chosenText = $('.radio_chosen_text').val();
        var chooseText = $('.radio_choose_text').val();

        defaultImagesList.click(function() {
            var $this = $(this);
            if ($this.find("img").length) {
                $this.siblings().removeClass("selected")
                    .find("span").text(chooseText)
                    .end().end().addClass("selected")
                    .find(":radio").attr("checked", true);
            }
            
            if ($(":radio", $this).is(':checked')) {
                $("span", $this).text(chosenText);
            }
            else {
                $("span", $this).text(chooseText);
            }
        }).filter(":last").hover(function() {
            var $this = $(this);
            if ($this.find("img").length) {
                $this.find(".own_image").show();
            }
        }, function() {
            var $this = $(this);
            if ($this.find("img").length) {
                $this.find(".own_image").hide();
            }
        }).end().filter(":first").click();

        if (ownImage.find("img").length) {
            ownImage.find(".own_image").hide();
        }
        else {
            ownImage.find(":radio").hide();
        }

        $(".default_images_list :radio:checked").parent().addClass("selected");




    }

    return {
        Init: init
    }
} ();


START_WIZARD.DONE_POPUP = function() {
	var popupScreenSmall = true;
	var viewPortWidth = 0;
	
	var init = function()
	{
		viewPortWidth = $().width();
		if (viewPortWidth > 1355)
			popupScreenSmall = false;
		
		showOverlayLayer();
		showDonePopup();
	}
	
	var showOverlayLayer = function()
	{
		$('body').scrollTop(0).css('overflow', 'hidden');
		
		$('<div>')
			.attr('id', 'overlayLayer')
			.css({
				position : 'absolute',
				top : 0,
				left : 0,
				width : 3000,
				height : 3000,
				zIndex : 1000,
				backgroundColor : '#000',
				opacity : 0.4
			})
			.appendTo('body');
	}
	
	var showDonePopup = function()
	{
		var popupClassName = popupScreenSmall ? 'startWizardPopupSmallScreen' : 'startWizardPopupBigScreen';
		var $popupContent = popupScreenSmall ? $('.pnlSmallScreen') : $('.pnlBigScreen');
		
		$('.startwizard_preview_box')
			.addClass(popupClassName)
			.appendTo('body')
			.show();
		
		$popupContent.show();
	}
	
	return { 
		Init : init
	}
}();


START_WIZARD.EXPERIMENT_WIDE_SIMPLE = function() {
	var init = function()
	{
		$('body').addClass('landingpage_widesimple_body');
	}
	
	return {
		Init : init
	}
}();


START_WIZARD.EXPERIMENT_NEW_CAROUSEL = function() {
	var init = function()
	{
		$('body').addClass('landingpage_newcarousel_body');
	}
	
	return {
		Init : init
	}
}();

$(function(){
	var modulesToLoad = $('.start_wizard_step');
	$.each(modulesToLoad, function(){
		START_WIZARD.initModule($(this).val());
	})
});