$(document).ready( function() {
    var $lang_suffix = $("input[@name='lang_suffix']").val();
    var $site_url = $("input[@name='site_url']").val();

	$("area").each(function() {
		$(this).mouseover(function(e) {
			ajax_showTooltip($(this).attr("href") + "?tooltip=1", this, e.pageX + 5, e.pageY - 20);
		});
		$(this).mouseout(function(e) {
			ajax_hideTooltip();
		});
	});

	// $("area").hover(function(e) {
	// 	ajax_showTooltip($(this).attr("href") + "?tooltip=1", this, e.pageX + 5, e.pageY - 20);
	// }, function() {
	//	ajax_hideTooltip();
	// });

    // NEW PROJECT EFFECT
    newprj_fade();

    // FORM REGISTER
    if ($("form#register").length > 0) {
        $("#register input[@name='title']").keydown(
            function () {
                $remain_character = (45 - $("#register input[@name='title']").val().length);
                $remain_character = $remain_character == '' ? '0' : $remain_character;
                $("#count_title").html($remain_character);
            }
        );
        $("#description").keydown(
            function(e) {
                $kc = e.which;
                //alert ($kc);
                $remain_character = 600 - $("#description").val().length;
                $remain_character = $remain_character == '' ? '0' : $remain_character;
                $("#count_discription").html($remain_character);
                if ($kc != 8 && $kc != 13 && $kc!= 0 && $kc != 37 && $kc != 38 && $kc != 39 && $kc != 40
                    && $kc != 46 && $kc != 33 && $kc != 34 && $kc != 35 && $kc != 36
                    && $kc != 9
                    && $("#description").val().length >= 600) {
                    return false;
                } else {
                    return true;
                }
            }
        );
        $("#description").keyup(
            function(e) {
                $str = $("#description").val();
                if ($str.length > 600) {
                    $("#description").val($str.substr(0, 600));
                    $remain_character = 600 - $("#description").val().length;
                    $remain_character = $remain_character == '' ? '0' : $remain_character;
                    $("#count_discription").html($remain_character);
                }
            }
        );
    }

    // FORM NEWSLETTER CHECK
    $("form#sendnewsletter").submit(
        function () {
            $email = $("#sendnewsletter input[@name='email']").val();
            if ($email == '') {
                alert(eval("txt_enter_email" + $lang_suffix));
                return false;
            } else if (!validate_email($email)) {
                alert(eval("txt_valid_email" + $lang_suffix));
                return false;
            } else {
                return true;
            }
        }
        );

    // FORM SENDFRIEND CHECK
    $("form#sendfriend").submit(
        function () {
            $email = $("#sendfriend input[@name='email']").val();
            if ($email == '') {
                alert(eval("txt_enter_email" + $lang_suffix));
                return false;
            } else if (!validate_email($email)) {
                alert(eval("txt_valid_email" + $lang_suffix));
                return false;
            } else {
                return true;
            }
        }
        );

    // FORM SEARCH CHECK
    $("form#search").submit(
        function () {
            if ($("#search input[@name='keyword']").val() == '') {
                alert(eval("txt_enter_keyword" + $lang_suffix));
                return false;
            } else {
                return true;
            }
        }
        );

    // FORM POLL CHECK
    $("#home_poll_vote").click (
        function () {
            vote_id = $("#poll_vote input[@name='poll_id']").val();
            vote_value = $("input[@name='poll_option']:checked").val();
            if (!vote_value) {
                alert(eval("txt_select_option" + $lang_suffix));
            } else {
                window.open($site_url + 'survey/vote/' + vote_id + '/' + vote_value, "PollWindow", "width=500,height=300");
            }
        }
        );

    $("#home_poll_result").click (
        function () {
            vote_id = $("#poll_vote input[@name='poll_id']").val();
            window.open($site_url + 'survey/result/' + vote_id, "PollWindow", "width=500,height=300");
        }
        );


    // FORM CONTACT CHECK
    if ($("form#contact").length > 0) {
        $("form#contact").validate({
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                },
                content: {
                    required: true
                }
            },
            messages: {
                name: eval("txt_enter_firtname" + $lang_suffix),
                email: eval("txt_enter_valid_email" + $lang_suffix),
                content: "<br />" + eval("txt_enter_contact_content" + $lang_suffix)
            }
        });
    }

    // FORM SEND TO FRIEND CHECK
    if ($("form#sendtofriend").length > 0) {
        $("form#sendtofriend").validate({
            rules: {
                email: {
                    required: true,
                    email: true
                },
                email_friend_1: {
                    required: true,
                    email: true
                },
                comment: "required"
            },
            messages: {
                email: eval("txt_enter_valid_email" + $lang_suffix),
                email_friend_1: eval("txt_enter_valid_email" + $lang_suffix),
                comment: "<br />" + eval("txt_enter_comment" + $lang_suffix)
            }
        });
    }

    // FORM SEND TO FRIEND CHECK
    if ($("form#register").length > 0) {
        $("form#register").validate({
            rules: {
                type: 'required',
                area: 'required',
                price: 'required',
                name: 'required',
                birthday: 'required',
                birthmonth: 'required',
                birthyear: 'required',
                mobile: 'required',
                address: 'required',
                job: 'required',
                company: 'required',
                companyaddress: 'required',
                monthlyincome: 'required',
                email: {
                    email: true
                },
                captcha_code: 'required'
            },
            messages: {
                type: '! Please select property type',
                area: '! (Required)',
                price: '! (Required)',
                name: '! (Required)',
                birthday: '!',
                birthmonth: '!',
                birthyear: '! (Required)',
                mobile: '! (Required)',
                address: '! (Required)',
                job: '! (Required)',
                company: '! (Required)',
                companyaddress: '! (Required)',
                monthlyincome: '! (Required)',
                phone: '! (Required)',
                email: '! (Please enter valid email address)',
                captcha_code: '! (Required)'
            }
        });
    }

    // CHANGE MENU WIDTH
    var menu_padding = 10;
    $("#nav").children("li").each(
        function (i) {
            $(this).children("ul").each(
                function (j) {
                    $max_width = 0;
                    $(this).children("li").each(
                        function (k) {
                            $(this).children("ul").each(
                                function (l) {
                                    $child_max_width = 0;
                                    $(this).children("li").each(
                                        function (m) {
                                            $this_width = $("a", this).width();
                                            if ($this_width >= $child_max_width) {
                                                $child_max_width = $this_width;
                                            }
                                        }
                                        )
                                    $(this).width($child_max_width + menu_padding);
                                }
                                )
                            $this_width = $("a", this).width();
                            if ($(this).attr("class") == "dir") {
                                $this_width += 10;
                            }
                            if ($this_width >= $max_width) {
                                $max_width = $this_width;
                            }
                        }
                        )
                    $(this).width($max_width + menu_padding);
                    $max_width = 0;
                }
                )
        }
        );

    
    
});

function newprj_fade() {
    var time = 5000;
    
    $('#newprj').cycle({
        fx:     'fadeZoom',
        timeout: time,
        before:  newprj_onBefore,
        after:   newprj_onAfter,
        next:   '#newprj_next',
        prev:   '#newprj_prev'
    });
}

function newprj_onBefore() {
    var id = this.alt;
    //$('#newprj_title').html('<a href="index.php/newproject/detail/' + id + '">' + this.title + '</a>');
    $('#newprj_title').html('<a href="newproject/detail/' + id + '">' + this.title + '</a>');
    $('#newprj').click(
        function () {
            //window.location = 'index.php/newproject/detail/' + id;
            window.location = 'newproject/detail/' + id;
        }
    );
}

function newprj_onAfter() {
//$('#newprj_title').html('<a href="index.php/newproject/detail/' + this.alt + '">' + this.title + '</a>');
}

function validate_email(value) {
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

