var current = 0;

function menuhover(hclr, obj) {
    hclr = "#" + hclr;
    $(obj).mouseover(function() {
        $(obj).css("background-colour", hclr);
    });
    $(obj).mouseout(function() {

    });

}

function placeOpacityLine(item) {
    //Get the opacity value (0 to 1 e.g 0.5)
    var opacity = $(item).children(".opacity_line").attr('value');
    //Get the opacity line color
    var line_color = $(item).children(".opacity_line").attr("color");
    //Get the size of the text and add 4 pixels (2 for top 2 for bottom)
    var text_block_size = $(item).children(".text").children("a,p").height() + 4;
    //Set the size of the opacity bar
    $(item).children(".opacity_line").height(parseInt(text_block_size));
    //Get the vertical align of the text
    var vert_align = $(item).children(".text").attr("vert_align");
    //Get the row height
    var row_height = parseInt($(item).height());

    //Check the vertial position
    if (vert_align == "middle") {
        //If middle work out the margin to for top and bottom
        var total_margin = (row_height - parseInt($(item).children(".opacity_line").height())) / 2;
        //Set margins
        $(item).children(".opacity_line").css({'background-color':line_color,'opacity':opacity,'margin-top':total_margin,'margin-bottom':total_margin});
    } else if (vert_align == "bottom") {
        //If bottom work out the margin top to get text to the bottom of the div
        var total_margin = (row_height - parseInt($(item).children(".opacity_line").height()));
        $(item).children(".opacity_line").css({'background-color':line_color,'opacity':opacity,'margin-top':total_margin});
    } else {
        $(item).children(".opacity_line").css({'background-color':line_color,'margin':''});
    }

}

function placeText(item) {
    //Get vertical align
    $valign = $(item).children(".text").attr("vert_align");
    //Get horizontal align
    $halign = $(item).children(".text").attr("horz_align");

    //Set the horizontal align
    $(item).children(".text").css("text-align", $halign);

    //Get the and set text color
    var text_color = $(item).children(".text").children("a").attr("color");
    $(item).children(".text").children("a").css("color", text_color);

    //Check vertical align and and set line-height
    switch ($valign) {
        case "top" :{
            $(item).children(".text").css("line-height", "20px");
            break;
        }

        case "middle" : {
            $(item).children(".text").css("line-height", $(item).height() + "px");
            break;
        }

        case "bottom" : {
            var text_height = $(item).children(".text").children("a,p").height();
            if ($(item).children(".text").children("a").height() == "") {
            }

            var lineheight = ($(item).height() * 2) - parseInt(text_height);
            $(item).children(".text").css("line-height", lineheight + "px");
            break;
        }

    }
}

function fixPortfolioText() {
    //Get all the portfolios
    var portfolios = $(".news_standard_group.portfolio");

    //Loop through each portfolio
    for (i = 0; i < portfolios.length; i++) {
        //Selected portfolio
        var current = portfolios[i];
        //Get the columns for the current portfolio
        var columns = $(current).children(".newsGroupColumn");
        //Get the number of columns the current portfolio has
        var num_columns = $(columns).length;
        //Create an array to hold the number of blocks each column has
        var num_blocks = Array();
        //Maximum number of blocks variable
        var max_blocks = -1;
        //Loop through the columns
        for (j = 0; j < num_columns; j++) {
            //Get how many blocks in the column
            num_blocks[j] = $(columns[j]).children(".news_standard_block.portfolio_block").length;
            //Check whether its the most amount of items in a column
            if (num_blocks[j] > max_blocks) {
                max_blocks = num_blocks[j];
            }
        }
        //Loop through all the p blocks
        for (j = 0; j < max_blocks; j++) {
            //Get the max height
            var max_height = -1;
            //Loop through each column again
            for (k = 0; k < num_columns; k++) {
                //Get the heigt of the current element from selected column
                var p_height = $(columns[k]).children(".news_standard_block.portfolio_block").eq(j).find("p").height();
                //If the height is not null and is bigger then max_height
                if (p_height != null && p_height > max_height) {
                    //Set the max height
                    max_height = p_height;
                }
            }
            //Loop through each of the columns
            for (k = 0; k < num_columns; k++) {
                //Find the portfolio block within the column and set it to maximum height.
                $(columns[k]).children(".news_standard_block.portfolio_block").eq(j).find("p").height(max_height);
            }
        }

    }

}

$(document).ready(function() {
    /*if ($("#google_map").length > 0) {
        var map_div = $("#google_map")
        if (map_div.attr("postcode") != null) {
            mapLoad();
            var postcode = $("#google_map").attr("postcode");
            var zoom = $("#google_map").attr("zoom");
            usePointFromPostcode(postcode, placeMarkerAtPoint);
        }
    }*/


    var browserName = navigator.appName;
    if (browserName == "Microsoft Internet Explorer") {
        var mi_bottom_margin = parseInt($(".mainImage li").css("margin-bottom"));
        mi_bottom_margin = mi_bottom_margin / 2;
        $(".mainImage li").css("margin-bottom", mi_bottom_margin);
    } else {
        //alert("Your are using browser : " + browserName + " | Version : " + navigator.appVersion);
    }

    $(".mainImage li").each(function() {
        placeText($(this));
        placeOpacityLine($(this));
    });

    $(".tower_menu li").not(".title").mouseover(function () {
        var hoverColor = $(this).parent().attr("hover_color");
        $(this).css({'background-color' : hoverColor});
    });

    $(".tower_menu li").not(".title").mouseleave(function () {
        var normColor = $(this).parent().attr("color");
        $(this).css({'background-color' : normColor});
    });


    var mi_effect = $('.sequence_images').attr('effect');
    var mi_speed = parseInt($('.sequence_images').attr('speed'));
    var mi_delay = parseInt($('.sequence_images').attr('delay'));
    var mi_timeout = parseInt($('.sequence_images').attr('timeout'));
    /*
     if ($('.sequence_images').length > 0) {
     $('.sequence_images').cycle({
     fx: mi_effect,
     delay: Math.abs(mi_delay),
     speed: mi_speed

     });
     }
     */

    $('.sequence_images').each(function(intIndex){
        var effect = $(this).attr("effect");
        var speed = $(this).attr("speed");
        var delay = $(this).attr("delay");
        var timeout = $(this).attr("timeout");
        $(this).cycle({
        fx: effect,
        delay: Math.abs(delay), // First Slide Delay
        speed: speed,
        timeout: timeout,
        slideExpr : "div.mi"
        });
    })


    if ($('.sequence_topbanners').length > 0) {
        $('.sequence_topbanners').cycle({
            fx: 'scrollUp',
            delay: -15000,
            speed: 4000
        });
    }
    if ($('.sequence_bottombanners').length > 0) {
        $('.sequence_bottombanners').cycle({
            fx: 'scrollUp',
            delay: -15000,
            speed: 4000

        });
    }

    $(".news_standard").each(function() {
        $(this).children(".news_standard_group:first").show();
    });

    $(".news_standard_header li").css("cursor", "pointer");
    $(".news_standard_header li").click(function() {
        var news_standard_idx = $(".news_standard").index($(this).parents(".news_standard"));
        var tab_idx = $(".news_standard").eq(news_standard_idx).children(".news_standard_header").children("ul").children("li").index(this);
        var visible_idx = $(".news_standard").eq(news_standard_idx).children(".news_standard_group").index($(".news_standard").eq(news_standard_idx).children(".news_standard_group:visible"));
        if (tab_idx != visible_idx) {
            $(".news_standard").eq(news_standard_idx).children(".news_standard_group:visible").fadeOut("200", function() {
                $(".news_standard").eq(news_standard_idx).children(".news_standard_group").eq(tab_idx).fadeIn("200");
            });
        }

    });

    $(".gallery_block img").click(function() {
        if ($("#img_viewer").length > 0) {
            $("#img_viewer").remove();
        }
        var path = $(this).attr("src");
        var desc = $(this).attr("alt");
        var newbox = "<div id='img_viewer' style='display:none'><img src='" + path + "' /><div id='img_viewer_text' style='background-color:#ebebeb>" + desc + "</div>";
        $("body").append(newbox);
        $("#img_viewer").bPopup();
    });

    $(".gradient").each(function() {
        var color1 = $(this).attr("c1");
        var color2 = $(this).attr("c2");
        var loc1 = $(this).attr("l1");
        var loc2 = $(this).attr("l2");
        var direction = $(this).attr("dir");
        $(this).css('background', '-moz-linear-gradient(' + direction + ', #' + color1 + ' ' + loc1 + '%, #' + color2 + ' ' + loc2 + '%)');
    });

    $(".required").each(function() {
        var current_text = $(this).parent().siblings(".captions").html();
        $(this).parent().siblings(".redstar").html("*");
    });

    function checkRequiredFormFields() {
        var required_fields = $(".required");
        for (i = 0; i < required_fields.length; i++) {
            if ($(required_fields[i]).val() == "" || $(required_fields[i]).val() == null) {
                alert("Please ensure all the required fields are completed!");
                return false;
            }
        }
        alert("Form submit");
        return true;
    }

    $("#submit_enq").click(function() {
        if (checkRequiredFormFields()) {

            data_array = array();
            data_array[0] = "STRING";
            data_array = JSON.stringify(data_array);
            $.ajax({
                type: 'post',
                cache: false,
                url: 'http://www.server100.co.uk/engine/ajax/enquiry.php',
                data: data_array,
                processData: false,
                contentType: 'application/json',
                success: function(data) {
                    var result = $.parseJSON(data);
                }
            });
        }
    });

    fixPortfolioText();

});
