﻿var $ = jQuery.noConflict();
$(document).ready(function () {

    isIE6 = !$.support.opacity && !window.XMLHttpRequest,
	isIE = !$.support.opacity
    // add CSS enhancements for anything other than ie
    if ((navigator.userAgent.toLowerCase().indexOf('msie') == -1)) {
        var link = "<link type=\"text/css\" rel=\"stylesheet\" href=\"\styles/css3.css\"/>";
        $("head link[href='styles/tweaks.css']").after(link);
    }
    else {
        var link = $("<link/>").attr({ type: 'text/css', media: 'screen', rel: 'stylesheet', href: '/app_themes/extras/overlay.css' });
        //$("head").append( link ); 
        //$("head link[href='/scripts/modal/jquery.fancybox-1.3.1.css']").after(link)	
    }

    //if (isIE6){$(".rightPane .box .content").css({position:"relative"})}
    checkmaxheight()
    $(document).bind('fontresize', function () {
        checkmaxheight()
    });

    //$("a.video").click(function(e){
    //e.preventDefault();
    var flashvars = {};
    var params = { wmode: "transparent", autoplay: "true" };
    var attributes = { id: 'seneddtv' };
    swfobject.embedSWF($("a.video").attr("href"), "senedd", "530", "260", "9.0.0", false, flashvars, params, attributes);

    //})
    // link behaviours
    $("body a[href]").each(function () {
        var $atag = $(this);
        // accessibility focus  
        $atag
			.focus(function () {
			    $atag.parents(".tabs").length > 0 ? $atag.parent().toggleClass("focus") : $atag.toggleClass("focus")
			    if ($atag.parents(".hide").length > 0) {
			        $atag.parent().removeClass("hide").addClass("rehide")
			    }
			})
			.blur(function () {
			    $atag.parents(".tabs").length > 0 ? $atag.parent().toggleClass("focus") : $atag.toggleClass("focus")
			    if ($atag.parents(".rehide").length > 0) {
			        $atag.parent().removeClass("rehide").addClass("hide")
			    }
			})
			.mousedown(function () {
			    $atag.removeClass("focus").unbind("focus").unbind("blur")
			})

        //external links
//        if (this.hostname && (this.hostname !== location.hostname) && ($("span.external", this).length == 0) && (this.href.indexOf("javascript") == -1) && (this.href.length != -1)) {
//            if (($atag.parent().parent().parent(".tabs").length == 0) && ($atag.parent().parent(".share").length == 0)) {
//                $atag.attr("target", "_new")
//                var $title = ""
//                var $title = $atag.attr("title")
//                $atag.attr("title", $title + " (opens in a new tab or window)")
//                if ($("img", this).length == 0) {
//                    $atag.text(function () { return $atag.text().toString() }).append("<span class=\"external\" style=\"display:inline-block\" ><span class=\"hide\"> [External link]</span></span>")
//                }

//            }
//        }


        // general doc links icons
        if (($atag.attr("href").indexOf(".pdf") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("pdf")
        }
        if (($atag.attr("href").indexOf(".doc") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("doc")
        }

        if (($atag.attr("href").indexOf(".xls") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("xls")
        }
        if (($atag.attr("href").indexOf(".xlsx") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("xls")
        }
        if (($atag.attr("href").indexOf(".ppt") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("ppt")
        }
        if (($atag.attr("href").indexOf(".pptx") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("ppt")
        }
        if (($atag.attr("href").indexOf(".zip") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("zip")
        }
        if (($atag.attr("href").indexOf(".csv") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("xls")
        }
        if (($atag.attr("href").indexOf(".docx") > -1) && ($atag.parent("li").length > 0)) {
            $atag.parent("li").addClass("doc")
        }


    })

    $(".cols h3 a").css({ "word-wrap": "break-word" })
    $(".elipsis").css({ "display": "inline-block" })

    // table row styles
    $(".cms table tr:odd").addClass("odd")
    // table interaction 
    $(".cms table.expand")
		.attr("tabindex", "0")
		.before($("<a class=\"open\">Expand data table</a>")
					.click(function () {
					    if ($(this).html().indexOf("Expand") >= 0) {
					        $(this).html($(this).html().replace("Expand", "Reset"));
					        $(this).next().css({ position: "absolute", width: "50%", "box-shadow": "-1px 2px 2px #000", "-moz-box-shadow": "0px 0px 5px #888" })
					    }
					    else {
					        $(this).html($(this).html().replace("Reset", "Expand"));
					        $(this).next().css({ position: "", width: "500px", "box-shadow": "0 0 0 #000", "-moz-box-shadow": "0 0 0 #000" })
					    }
					})
				)

    //  flash accessiblity
    $(".flash-text h2.title").each(function () {
        var detail = $(this).next().addClass("detail").hide().attr('aria-hidden', "true")
        $(this).after($("<p><a class=\"open\" tabindex=\"0\" href=\"#\">show video description</a></p>"))

    })

    $("a.open").focus(function (e) {
        $(this).click(e)
    })


    $("a.open").click(function (e) {
        e.preventDefault();
        var $detail = $(this).parent().next("div.detail")
        if ($detail.is(":hidden")) {
            $detail.slideToggle("fast")
            $(this).text(function () { return $(this).text().replace("show", "hide") })
            $detail.attr('aria-hidden', "false");
        }
        else {
            $detail.slideToggle()
            $(this).text(function () { return $(this).text().replace("hide", "show") })
            $detail.attr('aria-hidden', "true");
        }
        $(this).toggleClass("show")
    })



    // A-Z links behaviour
    $("#divDateRange a").click(function () {
        $("#divDateRange a").removeClass("active")
        $(this).addClass("active")
    })
    // policy browser A-Z links behaviour
    $("#divAlphaRange a").click(function () {
        $("#divAlphaRange a").removeClass("active")
        $(this).addClass("active")
    })
    // in page tabs behaviour
    $(".contentPane .tabs li").click(function () {
        $(".contentPane .tabs li").removeClass("active")
        $(this).addClass("active")
    })
    // CSS dropdown from <select> element
//    $("select").each(function (i) {

//        var $ul = $("<ul/>").addClass("droplist").hide()
//        var $sel = $(this)
//        $sel.addClass("hide")
//        $("option", this).each(function (j) {
//            var $opt = $(this)
//            $ul.append($("<li/>").text($opt.text())
//							.click(function () { //.attr("tabindex", $sel.attr("tabindex"))
//							    $sel.val($opt.val());
//							    $("li", $ul).removeClass("selected")
//							    $(this).addClass("selected");
//							    $("span.selected", $ul.parent()).text($(this).text())
//							    $ul.hide()
//							    $ul.parent().css({ "z-index": 0 })
//							    return false;
//							})
//							.focusin(function () {
//							    $sel.val($opt.val());
//							    $("span.selected", $ul.parent()).text($(this).text())

//							})
//							.hover(function () {
//							    $(this).toggleClass("hover");
//							})
//					)

//        })
//        // fix for incorrect event bubbling in ie
//        $(document).click(function (e) {
//            if ((e.target.className.indexOf("selected") + e.target.className.indexOf("btn")) != -1) {
//                $(".select ul").hide()
//            }
//        })
//        //write selected option <span> into the DOM
//        var $wrap = $("<span/>")
//						.addClass("select")
//						.attr("tabindex", $sel.attr("tabindex"))
//						.append($("<span/>")
//							.addClass("selected")
//							.text($sel.find(":selected").text())
//						)
//						.append($("<span/>")
//							.addClass("btn")
//							.hover(function () {
//							    $(this).toggleClass("hover")
//							})
//							.click(function (e) {
//							    e.preventDefault();
//							    $(this).prev(".selected").click();
//							})
//						)
//						.click(function () {
//						    $(".select ul").hide().parent().css({ "z-index": 0 });
//						    $ul.show();
//						    $(this).css({ "z-index": 50 })
//						})

//						.blur(function () {
//						    $ul.hide()
//						    $ul.parent().css({ "z-index": 0 })
//						    return false;
//						})
//						.focusin(function (e) {
//						    e.preventDefault();
//						    var selIndex = ($sel.find(":selected").index() + 1)
//						    var lastchar = ""
//						    $(this).keydown(function (e) {
//						        e.preventDefault();

//						        // handle arrow and alphanumeric keys
//						        switch (e.keyCode) {
//						            case 38:

//						                $("li:eq(" + (selIndex -= 1) + ")", this).click();

//						                break;
//						            case 40:
//						                $("li:eq(" + (selIndex += 1) + ")", this).click();

//						                break;

//						            //case 39: console.log('right'); break;    
//						            //case 37: console.log('left'); break;    
//						            default:
//						                var C = String.fromCharCode(e.keyCode).toUpperCase();
//						                var matchEnd = false
//						                $("li", this).each(function () {
//						                    if ($(this).text().indexOf(C) === 0) {
//						                        //matched first time
//						                        if (lastchar != C || matchEnd == true) {
//						                            lastchar = C
//						                            $(this).click()
//						                            //    console.log("1st")
//						                            return false;
//						                        }
//						                        else {
//						                            lastchar = C
//						                            if ($("li.selected", $(this).parent()).next().text().indexOf(C) === 0) {
//						                                $("li.selected", $(this).parent()).next().click()
//						                                return false
//						                            }
//						                            else {
//						                                matchEnd = true
//						                                return false
//						                            }
//						                        }
//						                    }
//						                })
//						        }
//						        //console.log(C +" "+ lastchar + " "+ matchEnd)
//						    })


//						})
//        $sel.after($wrap.append($ul))
//        if (!isIE) {
//            $sel.parent().css({ "position": "absolute", "padding-bottom": "2em" }).after("<div style=\"height:4em\"></div>")
//        }

//    })



})



function checkmaxheight() {
    $(".maxheight").each(function () {
        $(".elipsis", this).remove();
        var txt = $(this).parent("a").attr("title")
        //console.log(txt + " " + $(this).height())
        if (($(this).height() >= "20") && (txt.length >= 26)) {
            var splitindex = 0
            switch ($(this).height()) {
                case 22:
                    splitindex = 28
                    break;
                case 23:
                    splitindex = 27
                    break;
                case 21:
                    splitindex = 30
                    break;
                case 20:
                    splitindex = 30
                    break;
                case 24:
                    splitindex = 26
                    break;
                case 26:
                    splitindex = 24
                    break;
                case 30:
                    splitindex = 20
                    break;
                case 34:
                    splitindex = 16
                    break;
                case 40:
                    splitindex = 14
                    break;
                case 48:
                    splitindex = 10
                    break;
                case 60:
                    splitindex = 6
                    break;
                default:

            }
            var splittext = txt.substring(0, splitindex)
            var rem = txt.substring(splitindex)
            $(this).text(splittext)
            $(this).append("<span class=\"elipsis\" style=\"display:inline-block\" >" + rem + "</span>")
        }
        else {
            $(this).text(function () { return ($(this).parent("a").attr("title")) })
        }
    })
}
function gup(name, source) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(source);
    if (results == null) return "";
    else return results[1];
}
