var CK_CONFIG={
    toolbar :
    [
    [ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','Superscript' ],
    [ 'UIColor','Styles','-','Source' ]
    ]
};
$(function(){

    if (typeof(CKEDITOR)!="undefined") {
        CKEDITOR.addStylesSet('default', []);
    
        CKEDITOR.stylesSet.registered['default'].push({
            name: 'Marker: Draft Copy',
            element: 'span',
            styles: {
                'background-color': '#FF5F5F',
                'color': '#000000'
            }
        });
    }

    $('#edit_mode .block').live('mouseover',function(){
        var block_edit=$(this).children('.block_edit');

        var new_width=$(this).width();
        var new_height=$(this).height();
        var content_child=$(this).children(':not(.block_edit):not(.spacer)');
        if (content_child.length==1) {
            new_width=content_child.outerWidth();
            new_height=content_child.outerHeight();
        }

        $(this).closest('.column').children('.add_widget').width(new_width-8);
        block_edit.children('.block_edit_wrap,.block_edit_title_bar').width(new_width);
        block_edit.children('.block_edit_wrap').height(new_height)
        block_edit.show();
    });
    $('.block').live('mouseout',function(){
        $(this).children('.block_edit').hide();
    });
	$('.delete_button').live('click',function(e){
		e.preventDefault();
		if (confirm('Are you sure you wish to remove this widget?')) {
			var clicked_button=$(this);
			$.get($(this).attr('data-delete-url'),function(){
				clicked_button.closest('.block').slideUp(function(){
					$(this).remove();
				});
			})
		}
		return false;
	});

    $('[data-handle-url]').live('click',function(e){
        e.preventDefault();
        $.get($(this).attr('data-handle-url'),function(data) {
            handleJson(data);
        },'json');
    });

    $('.widget_form').live('submit',function(){
        var widget_form=$(this);
        $(this).ajaxSubmit({
            success:function(data){
                handleJson(data,widget_form);
            },
            dataType:"json"
        });//,data:{json:false}
        return false;
    });


    $('#edit_mode .column .add_widget').live('click',function(e){
		e.preventDefault();
        $.get('/widgets/manage-block',{

            column_type:$(this).closest('.column_section').attr('data-type'),
            piece_id:$(this).attr('data-piece-id'),
			page_id:$(this).attr('data-page-id'),
			column_id:$(this).closest('.column').attr('data-column-id'),
			column_num:$(this).closest('.column').attr('data-column-num')
        },function(data){
            show_popup({
                content:data,
                popup_title:"Pick a widget"
            });
        })
    })
    $('.block_layouts a').live('click',function(e){
        var clicked_button=$(this);
        e.preventDefault();
        $.get('/widgets/manage-block',{
            block_type_id:clicked_button.attr('data-block-type-id'),
            column_id:clicked_button.closest('.block_layouts').attr('data-column-id'),
            block_id:clicked_button.closest('.block_layouts').attr('data-block-id')
        },function(data){
            handleJson(data,clicked_button);

			if (data.replace_with_id) {
				$('#'+data.replace_with_id+' .block_edit_wrap').click();
			}
			if (data.append_id) {
				$('#'+data.append_id+" .block:last .block_edit_wrap").click();
			}


            //move all add widgets to bottom
            $('.add_widget').each(function(){
                $(this).parent().append($(this));
            })
        },'json');
    })

    $('.dialog_popup').live('closing',function(){
        $(this).find('.editable_ck').each(function(){
            var existing_instance=CKEDITOR.instances[$(this).attr('id')];
            if (existing_instance) {
                existing_instance.destroy();
            }
        });
    });


    $('.dialog_popup .button_label_save_changes').live('click',function(){
        $(this).closest('form').submit();
    });
    $('.dialog_popup .button_label_cancel,button_label_back').live('click',function(){
        $(this).closest('.ui-dialog').children().dialog("close");
    });
    $('.dialog_popup .button_label_delete').live('click',function(){
		if (confirm('Are you sure you wish to remove this widget?')) {
			$(this).closest('.ui-dialog').children().dialog("close");
			var blockname = "#block_" + $(this).attr('data-block-id');
			$.get($(this).attr('data-delete-url'),function(){
				$(blockname).hide();
			})
		}
		return false;
	});
	$('.dialog_popup .button_label_change_widget').live('click',function(){
		var block=$('#block_'+$(this).attr('data-block-id'));
		var add_widget=block.parent().children('.add_widget');

		$.get('/widgets/manage-block',{
			column_type:add_widget.closest('.column_section').attr('data-type'),
            block_id:$(this).attr('data-block-id'),
            piece_id:add_widget.attr('data-piece-id'),
			page_id:add_widget.attr('data-page-id'),
			column_id:add_widget.closest('.column').attr('data-column-id'),
			column_num:add_widget.closest('.column').attr('data-column-num')
        },function(data){
            var popup=show_popup({
                content:data,
                popup_title:"Pick a new widget"
            });
			popup.bind('closing',function(){
				popup.unbind('closing');
				$('.ui-dialog').children().dialog("close");
			});
        })
	});
    $('.column_section .column').live('mouseover',function(){
        $(this).addClass('over');
    }).live('mouseout',function(){
        $(this).removeClass('over');
    });



    //****** section slider and timer Fade inout Slider *************** //
    $('.section_slider .column_section').each(function(){
        $(this).css({
            overflow:'hidden',
            position:'relative'
        })
        var layout_column_type = $(this).attr("data-type");
        var layout_columns = 0;
        var max_height=0;
        var max_width = $(this).width();
        var columns=$(this).find('.column');
        var use_width=columns.eq(0).width();
        var is_timer_fade = $(this).parent().hasClass("section_slider_timer_fade_inout");
        var current_column_index = 0;
        switch(layout_column_type){
            case "one_column":
                layout_columns = 1;
                break;
            case "two_columns":
                layout_columns = 2;
                break;
            case "four_columns":
                layout_columns = 4;
                break;
            default:
                break;
        }


        //set width and height
        columns.each(function(){
            if ($(this).height()>max_height) {
                max_height=$(this).height();   
            }
        }).height(max_height).width(use_width);
		  
        //add absolute wrapper
        var wrap=$("<div />").addClass('slider_wrapper').css({
            position:'absolute',
            top:0,
            left:0
        });

        //add controller wrapper
        var slider_nav_wrap = $('<div />').addClass('slider_nav_wrapper');

        //****** For timer fadeinout
        if(is_timer_fade){
            var timer_delay = 10000;
            var slide_index = 0;
            var timer_id;

            wrap.width(use_width).height(max_height);
            $(this).height(max_height).wrapInner(wrap);

            //hide all slides and reset z-index for each of them
            for(i=columns.length-1,j=0; i>=0; i--,j++){
                if(columns.eq(i)){
                    columns.eq(i).hide();
                    columns.eq(i).css({
                        zIndex:j
                    });
                }
            }

            //fade in first slide
            columns.eq(0).fadeIn("slow");

            
            function startSlider(){

                if(slide_index == (columns.length-1)){
                    slide_index = -1;
                }
                slide_index++;
                
                slider_nav_wrap.find(".slider_nav_pointer").each(function(){
                    if($(this).index() === slide_index){
                        $(this).addClass("selected");
                    }else{
                        $(this).removeClass("selected");
                    }
                });
                columns.each(function(){
                    if($(this).index() === slide_index){
                        $(this).fadeIn("slow");
                    }else{
                        $(this).fadeOut("fast");
                    }
                })
                
            }
            //start timer
            timer_id = setInterval( startSlider, timer_delay);

            //add controller and its style will be in your own css
            var point_button = $('<div/>').addClass('slider_nav_pointer');

            for(p=0; p<columns.length; p++){
                var point = point_button.clone();
                if(p == 0) point.addClass("selected");
                slider_nav_wrap.append(point);
                point.bind("click", function(e){
                    e.preventDefault();
                    slide_index = $(this).index();

                    //reset all selected class and add it to the one only
                    $(this).parent().find(".slider_nav_pointer").removeClass("selected");
                    $(this).addClass("selected");

                    //
                    columns.each(function(){
                        if($(this).index() === slide_index){
                            $(this).fadeIn("slow");
                        }else{
                            $(this).fadeOut("fast");
                        }
                    })

                });
            }
            $(this).append(slider_nav_wrap);
            return;
        }

        //**** For simple slide left/right

        wrap.width(use_width*columns.length).height(max_height);
        $(this).height(max_height).wrapInner(wrap);

        if(layout_columns >= columns.length){
            return;
        }

        //add nav buttons and its style the wrapper and buttons in your own css.
        var slider_nav_left = $('<div />').addClass('slider_nav_left').css({
            height:max_height+"px"
        });
        var slider_nav_right = $('<div />').addClass('slider_nav_right').css({
            height:max_height+"px"
        });
        slider_nav_left.hide();
        slider_nav_wrap.append(slider_nav_right).append(slider_nav_left);
        slider_nav_wrap.insertBefore($(this));

        var slider_nav_right_timer_id = setInterval(function(){
            slider_nav_right.animate({marginRight:"-815px"}, 300).animate({marginRight:"-800px"}, 100);
        }, 3000);

        var slider_nav_left_timer_id = setInterval(function(){
            slider_nav_left.animate({marginLeft:"-815px"}, 300).animate({marginLeft:"-800px"}, 100);
        }, 3000);


        slider_nav_left.bind('click',function(e){
            e.preventDefault();
            
            var slider = $(this).parent().next().find(".slider_wrapper");
            var current_left = slider.position().left;
            if(current_column_index == 1){
                $(this).hide();
                //clearInterval(slider_nav_right_timer_id);
            }
            if(current_left < 0){
                slider_nav_right.show();
                current_column_index--;
                if(current_column_index <0 ) current_column_index = 0;
                if(!slider.is(':animated')){
                    slider.animate({
                        left:'+='+(use_width)
                    }, 400,'swing');
                }
            }
            //console.log("LEFT" +current_column_index);
        });
        slider_nav_left.bind("mouseover", function(){
            $(this).addClass("over");
        });
        slider_nav_left.bind("mouseout", function(){
            $(this).removeClass("over");
        });



        slider_nav_right.bind('click',function(e){
            e.preventDefault();
            
            var slider = $(this).parent().next().find(".slider_wrapper");
            var current_left = slider.position().left;
            
            if(current_column_index == (columns.length-layout_columns-1)){
                $(this).hide();
                //clearInterval(slider_nav_right_timer_id);
            }
            //if(Math.abs(current_left - (-use_width*columns.length+max_width)) >use_width ){
            if(current_column_index <= (columns.length-layout_columns-1)){
                slider_nav_left.show();
                current_column_index++;
                if(current_column_index > (columns.length-1) ) current_column_index = columns.length-1;

                if(!slider.is(':animated')){
                    slider.animate({
                        left:'-='+(use_width)
                    }, 400,'swing');
                }
            }
           // console.log("RIGHT" +current_column_index + ", "+columns.length + " , "+layout_columns);
        });
        slider_nav_right.bind("mouseover", function(){
            $(this).addClass("over");
        });
        slider_nav_right.bind("mouseout", function(){
            $(this).removeClass("over");
        });




    });


    /* Slider script end */

    $('.button_label_toggle_preview').click(function(){
        $('body').attr('id',$('body').attr('id')=='edit_mode'?'not_editing':'edit_mode');
    })
});

function getBlockPath(html_tag)
{
    var block_id=$(html_tag).attr('data-block-id');

    return '/blocks_handle/'+block_id;
}

//GENERIC, swfobject, facebox, main nav hover

$(function(){

	$('#facebox_overlay').live('click',function(){

	});

    swfobject.registerObject("knfads","10");



    $('a').click(function(e){



	if($(this).attr('rel') == 'facebox'){

		e.preventDefault();



		var html = $($(this).attr('href')).html();

		$.facebox(html);

		//var width = $('#facebox .content').width()+20;

	}

    });



    $("#nav ul.main_nav li.main").mouseenter(function(e){

	$(this).find('ul').show();

    });

    $("#nav ul.main_nav li.main").mouseleave(function(e){

	$(this).find('ul').hide();

    });

});



//CKEDITOR styles

$(function(){

    if (typeof(CKEDITOR)!="undefined") {

	CKEDITOR.stylesSet.registered['default'].push(

	{

	    name: 'Red Text',

	    element: 'span',

	    styles: {

		'color': 'red'

	    }

	});

    }

});



//large sub nav selected links

$(function(){

		var h = $('.content_subnav ul.main li.selected a.lead');

		if(h.length > 0){

			h = h.text().length;

			if(h > 15){

				//$('.content_subnav .main li. div.selected_sub a.lead').css('background','url(/blocks/knf/html/img/content_subnav_arrow2.png) top right no-repeat');

				$('.content_subnav .main li. div.selected_sub').css('background','url(/blocks/knf/html/img/content_subnav_arrow2.png) bottom left');

				$('.content_subnav .main li. div.selected_sub a.lead').css('height','30px');

			}

		}



    var a = $('.content_subnav ul.main li a.lead');

    a.each(function(i){

	if(!$(this).parent().hasClass('selected_sub')){

	    if($(this).text().length > 14){

		$(this).parent().height(35);

	    }

	}

    });

});



//sub nav heading links

$(function(){

    var sitem = $('.content_subnav ul.main li.selected');

    var titles = $('div.title h3');

	 

	 /*titles.each(function(i){

		 console.log($(this).html());

	 });*/

    var existing = $.trim(sitem.find('ul.sub').html());

    if(existing.length == 0){

	if(titles.length > 0 && sitem.length > 0){

	    titles.each(function(i){

			 var title = '';

			 if($(this).find('span').length > 0){

				 title = $.trim($(this).find('span').text());

			 }else{

				 title = $.trim($(this).html());

			 }



		if(title != "Click here to add a title"){

			 /*var new_title = title.split(' ');

			 var display = '';

			 for(x in new_title){

				 if(x )

				 display += new_title[x]+" ";

			 }*/

		    $(this).attr('id',i);

		    sitem.find('ul.sub').append(

			"<li><a href='#"+i+"'>"+title+"</a></li>"

			);

		}

	    });

	}

    }

});



function validateEmail(elementValue){

    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    return emailPattern.test(elementValue);

}



//user login form(s)

$(function(){

	$('a.btn.login').click(function(e){

		e.preventDefault();

		if($('#facebox').length >0){

			$('#facebox').find('form').find('input[name=Customer_Code]').trigger('focus');

		}

	});



		$('.signup_moreinfo a.more_info').hide();

    var form = $('form.login');

    $('form.login input.radio').live('click',function(e){

		 form = $(this).closest('form');

	var val = $(this).val();

	var type_change = form.find('div.type_change');

	var lang = form.attr('lang');



	if(val == "M"){

		$('.signupbtn').attr('rel','#signup_manu');

		type_change.find('p').text($(this).parent().find('span.rtext').text()).siblings('input[name=Customer_Code]').trigger('focus');

		type_change.find('input[type=hidden]').attr('name','Manufacturer').val('Yes');

		form.attr('action',"https://dev.kohlandfrisch.com:8080/cgi/zyw607?lang="+lang);

		$('a.change_pwd').attr('rel','#change_passwd_manu');

		if(form.attr('lang') != "F"){

		 form.closest('div').parent().find('span.existing_text').text('manufacturer');

		}else{

		 form.closest('div').parent().find('span.existing_text').text('fabricant');

		}

	}



	if(val == "R"){

	    $('.signupbtn').attr('rel','#signup_retail');

	    type_change.find('p').text($(this).parent().find('span.rtext').text()).siblings('input[name=Customer_Code]').trigger('focus');

	    type_change.find('input[type=hidden]').attr('name','none').val('0');

	    form.attr('action',"https://dev.kohlandfrisch.com:8080/cgi/zyw507?lang="+lang);

		 $('a.change_pwd').attr('rel','#change_passwd_retail');

		if(form.attr('lang') != "F"){

		 form.closest('div').parent().find('span.existing_text').text('customer');

		}else{

		 form.closest('div').parent().find('span.existing_text').text('client');

		}

	}



		 var code = $(this).attr('data-code');

		 form.find('p.code').html(code);



		 var number = $(this).attr('data-number');

		 form.find('p.number').html(number);

		// alert(form.find('p.number').html());

    });



		function submitToKnF(e){

			e.preventDefault();

			var form = $(this).closest('form');

			var cc = $.trim(form.find('input[name=Customer_Code]').val());

			var p = $.trim(form.find('input[name=Password]').val());

			var ec = $.trim(form.find('input[name=Employee_Code]').val());

			var ec_num = parseInt(ec);

			if(cc.length == 0 || p.length == 0 || ec.length == 0 || isNaN(ec_num) || ec.length != 4){

				if(form.attr('lang') != "F"){

				 alert("The following error(s) occurred:\n-Customer/Manufacturer Code is required.\n-Password is required.\n-Employee Code is required.");

				}else{

				 alert("Une ou plusieurs erreurs se sont produites :\n-Code du client exigé.\n-Mot de passe exigé.\n-Code employé exigé.");

				}

			}else{

				 var form_data = form.serializeArray();

				 var data = {};



				 for(x in form_data){

				data[form_data[x].name] =  form_data[x].value;

				 }



				 var url = form.attr('action');

				 //var secure = "https://"+location.hostname;

				 $.post("/widgets/create/slug/registration_form/render/login/json/false",{

				url:url,

				form:form.serialize()

				},function(data){

				var check = '';

						if(form.attr('lang') != "F"){

						 check = data.search('Please try again.');

						}else{

						 check = data.search('Le code client ou mot passe');

						}

				if(check > 0){

						if(form.attr('lang') != "F"){

						 alert("The Customer/Manufacturer Code or Password or Employee Code you entered is not correct.");

						}else{

						 alert("Le client code ou mot de passe ou code employé que vous avez entré sont incorrect.");

						}

					 

				}else{

					 form.submit();

				}

				 });

			}

		}



	 form.live('keypress',function(e){

		 if(e.which == 13){

			$(this).find('a.loginbtn').trigger('click');

		 }

	 });

    form.find('a.loginbtn').live('click',submitToKnF);



    form.find('a.forgotpop').live('click',function(e){

	e.preventDefault();

	var html = $($(this).attr('rel')).html();

	$.facebox(html);

		/*var width = $('#facebox').width();

		$('#facebox table.close_bar').width(width+"px");*/

    });



    $('a.more_info').live('click',function(e){

	e.preventDefault();

	var html = $($(this).attr('rel')).html();

	$.facebox(html);

		/*var width = $('#facebox').width();

		$('#facebox table.close_bar').width(width+"px");*/

    });



    $('a.signupbtn').live('click',function(e){

	e.preventDefault();

	var html = $($(this).attr('rel'));

	var newuser = $(this).attr('newuser');

	var rel = $(this).attr('rel');

	if(newuser == '1' &&  rel == "#signup_retail"){

		html.find('.customer_code').hide();

		html.find('input[name=actionflag]').val('newacc');

	}else{

		html.find('.customer_code').show();

		html.find('input[name=actionflag]').val('exstacc');

	}

	if(newuser == '1' &&  rel == "#signup_manu"){

		html = $('#more_info');

	}

	$.facebox(html.html());

		/*var width = $('#facebox').width();

		$('#facebox table.close_bar').width(width+"px");*/

    });



    $('a.forgotbtn').live('click',function(e){

	e.preventDefault();

	var form = $(this).parent().parent();

	var em = $.trim(form.find("input[name=Emailbox]").val());

	if(em.length == 0 || validateEmail(em) == false){

		if(form.attr('lang') != "F"){

		 alert("The following error(s) occurred:\n-A valid email is required.");

		}else{

		 alert("Une ou plusieurs erreurs se sont produites :\n-Courrier électronique.");

		}	    

	}else{

	    var url = form.attr('action');

	    $.post(

		"/widgets/create/slug/registration_form/render/forgotpw/json/false",

		{

		    url:url,

		    form:form.serialize()

		},

		function(data){

		    var thanks = $('#thank_you_fp');

		    thanks.find('span.user_email').text(em);

		    $.facebox(thanks.html());

				/*var width = $('#facebox').width();

				$('#facebox table.close_bar').width(width+"px");*/

		}

		);

	}

    });



    $('.login_forgot_pw .signupbtnsubmit').live('click',function(e){

	e.preventDefault();

	var form = $(this).parent().parent();



	if(form.hasClass('signup_manu')){

	    var an = $.trim(form.find('input[name=Exst_Acc_No]').val());

	}else{

	    var an = true;

	}

	var cn = $.trim(form.find('input[name=Customer_Name]').val());

	var en = $.trim(form.find('input[name=Employee_Name]').val());

	var ad = $.trim(form.find('input[name=Address]').val());

	var ci = $.trim(form.find('input[name=City]').val());

	var pr = $.trim(form.find('select[name=Province]').val());

	var ph = $.trim(form.find('input[name=Phone]').val());

	var em = $.trim(form.find('input[name=Email]').val());

	var pw = $.trim(form.find('input[name=Password]').val());

	var pw2 = $.trim(form.find('input[name=Password1]').val());



	if(an.length == 0 || cn.length == 0 || en.length == 0 || ad.length == 0 || ci.length == 0

	    || pr.length == 0 || ph.length == 0 || em.length == 0 || pw.length == 0

	    || pw2.length == 0){

		if(form.attr('lang') != "F"){

		 alert('Please make sure all fields marked with an * have been entered');

		}else{

		 alert("Veuillez vous assurer que tous les champs marqués d'un * ont été remplis.");

		}

	    return false;

	}else{

	    if(!validateEmail(em)){



		if(form.attr('lang') != "F"){

		 alert('Please enter a valid email address.');

		}else{

		 alert("Veuillez entrer une adresse électronique valide.");

		}

	    }else{

		if(pw != pw2){

			if(form.attr('lang') != "F"){

			 alert('Passwords do not match.');

			}else{

			 alert("Les mots de passe ne sont pas les mêmes.");

			}

		}else{

		    if(pw.length >= 6 && pw.length <= 10){

			var url = form.attr('action');

			$.post(

			    "/widgets/create/slug/registration_form/render/signup/json/false",

			    {

				url:url,

				form:form.serialize()

			    },

			    function(data){

					 var thanks = $('#thank_you_su_re');

					if(form.hasClass('signup_manu')){

						 thanks = $('#thank_you_su');

					}

				

				$.facebox(thanks.html());

				/*var width = $('#facebox').width();

				$('#facebox table.close_bar').width(width+"px");*/

			    }

			    );

		    }else{



				if(form.attr('lang') != "F"){

				 alert('Your password must be minimum 6 and maximum 10 characters long.');

				}else{

				 alert('Les mots de passe ne sont pas les mêmes.');

				}

		    }

		}

	    }

	}



    });

});



//change password

$(function(){

		function changePwd(e){

			e.preventDefault();

			var form = $(this).closest('form');

			var form_data = form.serializeArray();

			var data = {};

			var lang = form.attr('lang');



			var cc = $.trim(form.find('input[name=Customer_Code]').val());

			var ec = $.trim(form.find('input[name=Employee_Code]').val());

			var cp = $.trim(form.find('input[name=Current_Password]').val());

			var ep = $.trim(form.find('input[name=Enter_Password]').val());

			var cop = $.trim(form.find('input[name=Confirm_Password]').val());



			if(cc.length < 1 || ec.length < 1 || cp.length < 1 || ep.length < 1 || cop.length < 1){

				if(lang != 'F'){

					alert("The required information is incomplete or contains error:\n\n* Please enter your Customer/Manufacturer code\n* Please enter an employee code\n* Please enter the current password\n* Please enter a new password\n* Confirmed password is not the same as new password");

				}else{

					alert("Le(s) champs suivant(s) sont obligatoire(s):\n\n* Code client est obligatoire\n* Code employé est obligatoire\n* Mot de passe actuel est obligatoire\n* Entrez noveau mot de passe est obligatoire\n* Le mot de passe choisit correspond pas à la confirmation de mot de passe que vous avez entré");

				}

			}else{

				if(ep != cop){

					if(lang != 'F'){

						alert("The required information is incomplete or contains error:\n\n* Confirmed password is not the same as new password");

					}else{

						alert("Le(s) champs suivant(s) sont obligatoire(s):\n\n* Le mot de passe choisit correspond pas à la confirmation de mot de passe que vous avez entré")

					}

				}else{

					if(ep.length < 6){

						if(lang != 'F'){

							alert("Your password must be minimum 6 and maximum 10 characters long");

						}else{

							alert("Votre mot de passe doit avoir un minimum de 6 caract­res et un maximum de 10");

						}

					}else{

						for(x in form_data){

							data[form_data[x].name] =  form_data[x].value;

						}



						var url = form.attr('action');

						$.post("/widgets/create/slug/registration_form/render/login/json/false",{

							url:url,

							form:form.serialize()

						},function(data){



							var check = -1;

							if(form.attr('lang') != "F"){

								check = data.search('Your password must be minimum');

							}else{

								check = data.search('Votre mot de passe doit');

							}

							

							var new_check = -1;

							if(form.attr('lang') != "F"){

								new_check = data.search('Your password has been');

							}else{

								new_check = data.search('Votre mot de passe a');

							}

							if(check >= 0){

							if(form.attr('lang') != "F"){

								alert("Your password must be minimum 6 and maximum 10 characters long");

							}else{

								alert("Votre mot de passe doit avoir un minimum de 6 caract­res et un maximum de 10");

							}



							}else{

								if(new_check >= 0){

									if(form.attr('lang') != "F"){

										if(confirm("Your password has been successfully changed.\nPlease use your new Password to login")){

											$('#facebox_overlay').trigger('click');

										}

									}else{

										if(confirm("Votre mot de passe a été changé avec succès.\nVeuillez utiliser votre nouveau mot de passe pour ouvrir la session")){

											$('#facebox_overlay').trigger('click');

										}

									}

								}

							}

						});

					}

				}

			}



		}

		//}

		$('a.change_pwd').live('click',function(e){

			e.preventDefault();

			var rel = $($(this).attr('rel')).html();

			$.facebox(rel);

			/*var width = $('#facebox').width();

			$('#facebox table.close_bar').width(width+"px");*/

			//changePwd(e);

		});



		$('a.change_pwd_submit').live('click',changePwd);

});



$(function(){

var code = '';

	if(location.hash == '#manu'){

		/*$('form.login input[value=R]').attr('checked',false);

		$('form.login input[value=M]').attr('checked',true);

		$('form.login input[name=Customer_Code]').trigger('focus');

		$('.right_col span.existing_text').text('manufacturer');

		 code = $('form.login input[value=M]').attr('code');

		 $('form.login p.code').html(code);

		 $('.right_col a.signupbtn').attr('rel','#signup_manu');

		 $('a.change_pwd').attr('rel','#change_passwd_manu');*/

		 $('form.login input.radio[value=M]').trigger('click');

	}



	if(location.hash == '#retail'){

		/*$('form.login input[value=R]').attr('checked',true);

		$('form.login input[value=M]').attr('checked',false);

		$('form.login input[name=Customer_Code]').trigger('focus');

		 code = $('form.login input[value=R]').attr('code');

		 $('form.login p.code').html(code);

		 $('.right_col a.signupbtn').attr('rel','#signup_retail');

		 $('a.change_pwd').attr('rel','#change_passwd_retail');*/

		 $('form.login input.radio[value=R]').trigger('click');

	}

});var widget_base='/blocks/knf/contact_form/html/';
$(function(){
	
	$('.login_forgot_pw .contact').click(function(e){
		e.preventDefault();
		var form = $('.login_form form');
		var name = $.trim(form.find('input[name=full_name]').val());
		var email = $.trim(form.find('input[name=email]').val());
		var company = $.trim(form.find('input[name=company]').val());
		var subject = $.trim(form.find('input[name=subject]').val());
		var message = $.trim(form.find('textarea[name=message]').val());
		if(name.length > 0){
			if(email.length > 0 && validateEmail(email)){
				if(subject.length > 0){
					if(message.length > 0){
						var block = $('.block_contact_form');
						$.post(
							getBlockPath(block),
							{
								form_submit:true,
								name:name,
								email:email,
								company:company,
								subject:subject,
								message:message
							},
							function(data)
							{
								$.facebox(
									function(){
										var form2 = $('.login_form.contact form');
										form2.find('input[name=full_name]').val('');
										form2.find('input[name=email]').val('');
										form2.find('input[name=company]').val('');
										form2.find('input[name=subject]').val('');
										form2.find('textarea[name=message]').val('');
										$.facebox($("#thank_you").html());

									}
								);
							},
							'json'
						);
					}else{
						alert($.trim($('div.contact_message').text()));
					}
				}else{
					alert($.trim($('div.contact_subject').text()));
				}
			}else{
				alert($.trim($('div.contact_email_valid').text()));
			}
		}else{
			alert($.trim($('div.contact_full_name').text()));
		}
	});
	
	

	$('.login_forgot_pw .clear_contact').click(function(e){
		e.preventDefault();
		var form = $('.login_form.contact form');
		form.find('input[name=full_name]').val('');
		form.find('input[name=email]').val('');
		form.find('input[name=company]').val('');
		form.find('input[name=subject]').val('');
		form.find('textarea[name=message]').val('');
	});
	//$('.login_forgot_pw .clear_contact').trigger('click');
});
var widget_base='/blocks/knf/content_rotator/html/';
$(function(){
    //****** section slider and timer Fade inout Slider *************** //
    $('.block_content_rotator .fadeinout').each(function(){
        $(this).css({
            overflow:'hidden',
            position:'relative'
        });
        var max_height=0;
        var max_width = $(this).width();
        var columns=$(this).find('.slide');
        var use_width=columns.eq(0).width();

        //set width and height
        columns.each(function(){
            if ($(this).height()>max_height) {
                max_height=$(this).height();
            }
        }).height(max_height).width(use_width);

        //add absolute wrapper
        var wrap=$("<div />").addClass('slider_wrapper').css({
            position:'absolute',
            top:0,
            left:0
        });

        //add controller wrapper
        var slider_nav_wrap = $('<div />').addClass('slider_nav_wrapper');
        var slider_nav_play_pause;

        //****** For timer fadeinout

        var timer_delay = 10000;
        var slide_index = 0;
        var timer_id;

        wrap.width(use_width).height(max_height);
        $(this).height(max_height).wrapInner(wrap);

        //hide all slides and reset z-index for each of them
        for(i=columns.length-1,j=0; i>=0; i--,j++){
            if(columns.eq(i)){
                columns.eq(i).hide();
                columns.eq(i).css({
                    zIndex:j
                });
            }
        }

        //fade in first slide
        columns.eq(0).fadeIn("slow");


        function startSlider(){

            if(slide_index == (columns.length-1)){
                slide_index = -1;
            }
            slide_index++;

            slider_nav_wrap.find(".slider_nav_pointer").each(function(){
                if($(this).index() === slide_index){
                    $(this).addClass("selected");
                }else{
                    $(this).removeClass("selected");
                }
            });
            columns.each(function(){
                if($(this).index() === slide_index){
                    $(this).fadeIn("slow");
                }else{
                    $(this).fadeOut("fast");
                }
            })

        }

        //dispay controller when there is more than one slide
        if(columns.length > 1){

            //start timer
            timer_id = setInterval( startSlider, timer_delay);

            //add controller and its style will be in your own css
            var point_button = $('<div/>').addClass('slider_nav_pointer');

            for(p=0; p<columns.length; p++){
                var point = point_button.clone();
                if(p == 0) point.addClass("selected");
                slider_nav_wrap.append(point);
                point.bind("click", function(e){
                    e.preventDefault();
                    clearInterval(timer_id);

                    slide_index = $(this).index();

                    //reset all selected class and add it to the one only
                    $(this).parent().find(".slider_nav_pointer").removeClass("selected");
                    $(this).addClass("selected");

                    //
                    columns.each(function(){
                        if($(this).index() === slide_index){
                            $(this).fadeIn("slow");
                        }else{
                            $(this).fadeOut("fast");
                        }
                    });

                    timer_id = setInterval( startSlider, timer_delay);
                });
            }
            $(this).append(slider_nav_wrap);

            //add Play / Pause button
            slider_nav_play_pause = $('<div />').addClass('slider_nav_play_pause');
            slider_nav_play_pause.bind("click", function(e){
                e.preventDefault();
                if(timer_id){
                    $(this).addClass("pause");
                    clearInterval(timer_id);
                    timer_id ="";
                }else{
                    $(this).removeClass("pause");
                    timer_id = setInterval( startSlider, timer_delay);
                }
            });
            $(this).append(slider_nav_play_pause);

        }

        return;

    });
});

var widget_base='/blocks/knf/date_created/html/';

var widget_base='/blocks/core/parts/bool/html/';
$('.bool_button').live('click',function(){
    var button=$(this).stop();
    if (button.data('state')!="on" && button.data('state')!="off") {
        button.data('state',button.hasClass('bool_state_off')?"off":"on");
    }

    if (button.data('state')=="on") {
        button.children('.off').stop().animate({left:"0px",opacity:1},{duration:200});
        button.children('.on').stop().animate({left:"-50px",opacity:0},{duration:200});
    }else{
        button.children('.off').stop().animate({left:"50px",opacity:0},{duration:200});
        button.children('.on').stop().animate({left:"0px",opacity:1},{duration:200});
    }
    button.data('state',button.data('state')=="on"?"off":"on");
    button.next().val(button.data('state')=="on"?'1':'');
})
var widget_base='/blocks/core/parts/file/html/';
$('.widget_form .ajax_tree span.item_has_children').live('click',function(){
    var clicked_child=$(this);

    if (clicked_child.closest('li').children('ul').length>0) {
        $(this).removeClass('open');
        clicked_child.closest('li').children('ul').remove();
        return;
    }
    clicked_child.addClass('loading');
    $.get($(this).closest('form.widget_form').attr('action'),{piece_id:$(this).closest('li').attr('data-piece-id'),json:false},function(data){
        clicked_child.addClass('open');
        clicked_child.removeClass('loading').closest('li').append(data)
    })
});
var widget_base='/blocks/core/parts/queue/html/';
$(function(){
	$('.queue_manager .delete').live('click',function(){
		if (!confirm('Are you sure you wish to remove this item?')) {
			return;
		}
		$(this).closest('li').fadeOut(function(){
			$(this).remove();
			resort_queue();
		})
	})
	$('.dialog_popup').live('popup',function(){
		
		$('.queue_manager .start_date input,.queue_manager .end_date input').datetimepicker();
		$('.queue_manager .form_ul').sortable({items:'>li:not(.add_entry)',axis:'y',handle:'.sorter',cursor:'n-resize',update:resort_queue})
		
		$('.queue_manager .add_item').unbind('click').click(function(){
			var date_object=new Date();
			var rand_id=date_object.getMilliseconds()+Math.round(Math.random()*10000);
			if (!$(this).prev().is(':visible')) {
				$(this).text('Done').prev().show();
				return;
			}else{
				$(this).text('Add Item').prev().hide();
			}
			
			var cloned_item=$(this).closest('.form_ul').find('.blank_entry').clone().css('display','block').removeClass('blank_entry');
			$(this).closest('.form_ul').find('.blank_entry').before(cloned_item);
			cloned_item.find('.start_date input,.end_date input').each(function(){
				$(this).attr('id',$(this).attr('id')+'-'+rand_id).removeClass('hasDatepicker').datetimepicker();
			});
			cloned_item.find('.title').text($(this).parent().find('option:selected').text());
			resort_queue();
			cloned_item.find('.sorter input').val($(this).parent().find('option:selected').val());
		})
	});
})
function resort_queue()
{
	$('.queue_manager .form_ul').each(function(){
		$(this).find('li dt:visible').each(function(key){
			$(this).text(key+1);
		})
	})
}


var widget_base='/blocks/knf/registration_form/html/';

var widget_base='/blocks/knf/rotator/html/';
$(function(){
    //****** section slider and timer Fade inout Slider *************** //
    $('.block_rotator .fadeinout').each(function(){
        $(this).css({
            overflow:'hidden',
            position:'relative'
        });
        var max_height=0;
        var max_width = $(this).width();
        var columns=$(this).find('.slide');
        var use_width=columns.eq(0).width();

        //set width and height
        columns.each(function(){
            if ($(this).height()>max_height) {
                max_height=$(this).height();
            }
        }).height(max_height).width(use_width);

        //add absolute wrapper
        var wrap=$("<div />").addClass('slider_wrapper').css({
            position:'absolute',
            top:0,
            left:0
        });

        //add controller wrapper
        var slider_nav_wrap = $('<div />').addClass('slider_nav_wrapper');
        var slider_nav_play_pause;

        //****** For timer fadeinout

        var timer_delay = 10000;
        var slide_index = 0;
        var timer_id;

        wrap.width(use_width).height(max_height);
        $(this).height(max_height).wrapInner(wrap);

        //hide all slides and reset z-index for each of them
        for(i=columns.length-1,j=0; i>=0; i--,j++){
            if(columns.eq(i)){
                columns.eq(i).hide();
                columns.eq(i).css({
                    zIndex:j
                });
            }
        }

        //fade in first slide
        columns.eq(0).fadeIn("slow");


        function startSlider(){

            if(slide_index == (columns.length-1)){
                slide_index = -1;
            }
            slide_index++;

            slider_nav_wrap.find(".slider_nav_pointer").each(function(){
                if($(this).index() === slide_index){
                    $(this).addClass("selected");
                }else{
                    $(this).removeClass("selected");
                }
            });
            columns.each(function(){
                if($(this).index() === slide_index){
                    $(this).fadeIn("slow");
                }else{
                    $(this).fadeOut("fast");
                }
            })

        }

        //dispay controller when there is more than one slide
        if(columns.length > 1){

            //start timer
            timer_id = setInterval( startSlider, timer_delay);

            //add controller and its style will be in your own css
            var point_button = $('<div/>').addClass('slider_nav_pointer');

            for(p=0; p<columns.length; p++){
                var point = point_button.clone();
                if(p == 0) point.addClass("selected");
                slider_nav_wrap.append(point);
                point.bind("click", function(e){
                    e.preventDefault();
                    clearInterval(timer_id);

                    slide_index = $(this).index();

                    //reset all selected class and add it to the one only
                    $(this).parent().find(".slider_nav_pointer").removeClass("selected");
                    $(this).addClass("selected");

                    //
                    columns.each(function(){
                        if($(this).index() === slide_index){
                            $(this).fadeIn("slow");
                        }else{
                            $(this).fadeOut("fast");
                        }
                    });

                    timer_id = setInterval( startSlider, timer_delay);
                });
            }
            $(this).append(slider_nav_wrap);

            //add Play / Pause button
            slider_nav_play_pause = $('<div />').addClass('slider_nav_play_pause');
            slider_nav_play_pause.bind("click", function(e){
                e.preventDefault();
                if(timer_id){
                    $(this).addClass("pause");
                    clearInterval(timer_id);
                    timer_id ="";
                }else{
                    $(this).removeClass("pause");
                    timer_id = setInterval( startSlider, timer_delay);
                }
            });
            $(this).append(slider_nav_play_pause);

        }

        return;

    });
});


