
$.fn.flash.replace = function(htmlOptions){
    $(this).replaceWith($.fn.flash.transform(htmlOptions));
}

var companyColor = "ff9702";

$(document).ready(function() {
	makeHeaderFlash();

    var comboboxClasses = { 
		comboboxContainerClass: "comboboxContainer", 
		comboboxValueContainerClass: "comboboxValueContainer", 
		comboboxValueContentClass: "comboboxValueContent", 
		comboboxDropDownClass: "comboboxDropDownContainer", 
		comboboxDropDownButtonClass: "comboboxDropDownButton", 
		comboboxDropDownItemClass: "comboboxItem", 
		comboboxDropDownItemHoverClass: "comboboxItemHover", 
		comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader", 
		comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer"
    };

   var width = $('#shortcuts .box_right').width();
   if($.browser.msie && $.browser.version < 7) // ie6 fix
        width -= 9; 
        
   if(!$.browser.safari){
       $('#shortcuts SELECT').combobox( comboboxClasses, {
		    animationType: "fade",
		    width: width
	    });
    	
       $('#CategoryListContainer SELECT').combobox( comboboxClasses, {
		    animationType: "fade",
		    width: 440 
	    });	
	
	} else {
        $('#shortcuts SELECT').show();
        $('#CategoryListContainer SELECT').show();
	}
	
	$('img[@src$=.png]').ifixpng("/Customer2/images/pixel.gif"); 
   
	$("INPUT[@type='file']").each(function(){
	    $(this).filestyle({
		    image: "/Customer2/images/form/file_select.jpg", 
		    imagewidth: 55,
		    imageheight: 19,
            width: ($(this).parents("#rightColumn").length > 0 ? 120 : 100)
	    });
	});
	
	$("#quicksearch").example("Search").keydown(DoSearch).click(DoSearch);
	$("*:not(#quicksearchResult)").click(function(){
	    $("#quicksearchResult").hide().remove();
	});

	$("#searchToolbar input[@type='text']").keypress(function(e){
	    if(e.which == 13){
	        if(__doPostBack){
	            __doPostBack();
	        }
	    }
	});
	
	$("#flags IMG:not([@src*=_active])").hover(function(){
	    $(this).attr("src",$(this).attr("src").replace(".gif","_active.gif"));
	}, function(){
	    $(this).attr("src",$(this).attr("src").replace("_active",""));	
	});
	
	$(".accordion").accordion({autoHeight: false, header: "h2", selectedClass: "selected", alwaysOpen: false}).show();
	
	if(location.hash && location.hash.indexOf("case") > -1){
	    var caseid = location.hash.replace("#case","");
	    popupCaseStudy(caseid);
	}
	
	var qs = new Querystring();
	if(qs.contains("caseid")){
	    popupCaseStudy(qs.get("caseid"));
	}
	
	$(".caseStudy").click(function(){
	    $("a", this).click();
	});
}); 

function makeHeaderFlash(){
	$('.flashheader, #CaseStudyBox .content H2').each(function(index, domElement){
		var lineHeight;
		var characterWidth;
		var allCaps;
		var flash;
		var flashvars = {};
		
		if(this.tagName == "H1"){
		    lineHeight = 30;
		    characterWidth = 14;
            flash = "/Customer2/flash/header1.swf";
            flashvars.fontcolor = "0x"+ companyColor;
		} else {
		    
		    characterWidth = 8;
		    flash = "/Customer2/flash/header2.swf";
		    if($(this).parents("#mainContent, #mainContentWide, #mainContentFull,#mainContentMax").length != 1){
		        flashvars.allcaps = true;
		        lineHeight = 16;
		    } else {
		        lineHeight = 18;
		    }
		}
		
		if($("a", this).length > 0){
//		    flashvars.link = escape($("a", this).attr("href"));
		    flashvars.link = $("a", this).attr("href");
    		flashvars.hovercolor = "0x"+ companyColor; 
		}
		
		flashvars.str = escape($(this).text());
		
		if($(this).parents(".content").parents("#CaseStudyBox").length > 0) {
		    flashvars.fontcolor = "0x"+ companyColor;
		}
		
	    makeHeader(this, $(this).parent().width(), lineHeight, characterWidth, flash, flashvars);
        $(this).removeClass("flashheader");
	});	
}

function makeHeader(domElement, maxWidth, lineHeight, characterWidth, flash, flashvars){
		var characters = unescape(flashvars.str).length;
		var lines;
		var width;
		if(characters * characterWidth > maxWidth){
			width = maxWidth;	
			lines = Math.ceil((characters * characterWidth) / maxWidth);
		} else {
			width = characters * characterWidth + 1 * characterWidth; // add some extra padding
			lines = 1;	
		}
		
        $(domElement).flash({
                src: flash,
                width: width,
                height: (lineHeight * lines),
                flashvars: flashvars,
                wmode: "transparent",
                bgcolor: "#000000"
              }, 
              {
                version: 9,
                update: false
              });
}

function DoSearch(){
    if($("#quicksearch").val().length >= 3){
        $.post("/Customer2/handlers/Search.ashx",{q:$("#quicksearch").val(), s:171, l:'en'},function(data, textStatus){
            $("#quicksearchResult").remove();
            var html = $("<ul id=\"quicksearchResult\">");            
            if(data && data.length > 0){
                var current;
                for(var i=0;i<data.length;i++){
                    current = $("<li>");
                    $(current).append("<a href=\""+ data[i].URL +"\">"+ data[i].PageName +"</a><br/>");
                    $(current).append(data[i].Description);
                    $(current).append("</li>");

                    // events
                    $(current).hover(function() {
                        $(this).addClass("hover");
                    }, function(){
                        $(this).removeClass("hover");
                    }).click(function(){
       					window.location = $("a", this).attr("href");
                    });
                    html.append(current);
                }
            } else { // no hits
                html.append("<li>Your search did not match any documents.</li>");
            }
            $("#topright").append(html);
            $("#quicksearchResult").slideDown("fast");                            
        }, "json");
    } else {
        $("#quicksearchResult").hide().remove();
    }
}

function loadCase(targetId, href){
    var url = href += "&random="+ (new Date().getTime());
    $.ajax({
        url: url,
        type: "GET",
        success: function(html){
            manageCase($("#"+ targetId).parent(), $(html));
        }
    });
}

function manageCase(target, result){
    var rootDir = "http://www.cordovangroup.com/";
    $("IMG:not([$src^=rootDir])", result).each(function(){ 
        var newSrc = $(this).attr("src");
        var result = newSrc.match(/(?:images|Global|PageFiles|Documents).*/);
        if (result != null) {
            newSrc = result[0];
        } 
       
        if(newSrc.indexOf("Global") == 0 || newSrc.indexOf("Documents") == 0 || newSrc.indexOf("PageFiles") == 0){ // file
            newSrc = rootDir + newSrc;    
        } else { // "internal" link
            newSrc = rootDir + "Customer2/" + newSrc; 
        }
        $(this).attr("src", newSrc);
     });
    
    $("IMG", result).css("display","block");
    target.empty();
    target.append($(result));
    makeHeaderFlash();
    $(".content", target).jScrollPane();
}

function popupCaseStudy(caseID){
    if(caseID != ""){
        tb_show("", "/Customer2/templates/CaseStudyPage.aspx?width=737&height=496&caseid="+ caseID+"&lang=en", "case");
    }
}

function popupCaseStudyFlash(caseID){
    if(caseID != ""){
        tb_show("", "/Customer2/templates/CaseStudyPageFlash.aspx?width=737&height=496&caseid="+ caseID+"&lang=en", "case");
    }
}

function popupCallingCard(pageID){
    if(pageID != ""){
        tb_show("", "/Customer2/templates/PersonCallingCardPage.aspx?width=500&height=250&pageID="+ pageID +"&lang=en", "callingcard");
    }
}

function manageCallingCard(target, result){
    var rootDir = "http://www.cordovangroup.com/";
    $("IMG:not([$src^=rootDir])", result).each(function(){ 
        var newSrc = $(this).attr("src");
        var result = newSrc.match(/(?:images|Global|PageFiles|Documents).*/);
        if (result != null) {
            newSrc = result[0];
        } 
       
        if(newSrc.indexOf("Global") == 0 || newSrc.indexOf("Documents") == 0 || newSrc.indexOf("PageFiles") == 0){ // file
            newSrc = rootDir + newSrc;    
        } else { // "internal" link
            newSrc = rootDir + "Customer2/" + newSrc; 
        }
        $(this).attr("src", newSrc);
     });
    
    $("IMG", result).css("display","inline");
    target.empty();
    target.append($(result));
    $(result).jScrollPane();
}