var curvyCornersVerbose = false;

/*
Javascript code supporting ajax functionalities.
*/

var NotesCookie = false;
var PrintCartCookie = false;
var LinkCartCookie  = false;
var dldInProgress = false;
var basepath = '/';
var notesUrl = "";
var printUrl = "";
var LinksUrl = "";
var mailerUrl = "";
var feedbackUrl = "";
var downloadsUrl = "";
var exitUrl = "";
var options = false;

function establishPgId()
{
	var contentClass = $('body').attr("class");
	var rg = new RegExp("pg\\d+");
	var res = rg.exec(contentClass);
	contentClass = res;
	return res;
}		


function isInNotepad(){
	var pg = $('body').attr('class');
	var rg = new RegExp("pg\\d+");
	var res = rg.exec(pg);
	pg=res;
	var MyCookie = NotesCookie;
	return (MyCookie && MyCookie.indexOf('###'+pg) > -1);
}

function setNotepadPageStatus(){
    if (isInNotepad()){
		$('li.note > #notes').text('Edit');
	}
}


function isInCart(){
		var value =  PrintCartCookie;
		return (value != null && value.indexOf(establishPgId())>-1)
}
	
function isInLinkCart(){
		var value =  LinkCartCookie;
		return (value != false && value.indexOf(establishPgId())>-1);
}
	
function setCartPageStatus(){
		if (isInCart()){
			$('li.printBasketAdd > #printbasket').remove();
			$('li.printBasketAdd > span.slashsep').remove();
		}
}

function setLinkCartStatus(){
    if (isInLinkCart()){
		$('li.linkBasketAdd > #linkbasket').remove();
		$('li.linkBasketAdd > span.slashsep').remove();	}
}

function getPageFromLink(link) {
    var pg = $(link).attr('href');
    var rg = new RegExp("pg\\d+");
    var res = rg.exec(pg);
    if (res != null) return res[0];
    else return 0;
}
function displayPopup(){
	
	//<img class="close" style="float:right;" src="'+basepath+'/images/buttons/closeX.gif" alt="Close window" title="Close window"/>
	
	$('body').append('<div id="dialog" class="jqmWindow"><a class="close">Close window</a><div class="content"></div><div id="splash"><h2>Processing...</h2><img src="'+basepath+'images/loading.gif" /></div></div>');
	$('#dialog').jqm({onShow: showEvent, modal: true}).jqmShow();
	$('div.jqmOverlay').click(function(){ClosePopup();});
	$('.close').click(function(){ClosePopup();});
}
function showEvent(hash){
		SetSplashStatus(true);
		hash.w.show();
		$.get(options['url'],options['params'],function (data){
				loadEvent(data);
			}
	);
}
function loadEvent(data){
	$('#dialog > div.content').html(data);
	SetSplashStatus(false);
	if (options['CancelAction']){
			$(options['CancelAction']).click(function(){
				ClosePopup();
				return false;
			});
			$(options['CancelAction']).show();
	}		
	if (options['DoAction']) {
		$(options['DoAction']).click(submitEvent);
	}
	
	if (options['loadCallback']) {
		options['loadCallback']();
	}
}

function submitEvent(data){
	  SetSplashStatus(true);
	  var fields = $('#dialog > div.content').find(":input");
	  var inputs = getAjaxFormFields(fields);
	  $.get(options['url'],inputs,function (data){
	  		loadEvent(data);
	  		if (options['submitCallback'] != null){
	  			options['submitCallback']();
				}
	  	});
		return false;
}
function ClosePopup(){
	$('#dialog').jqmHide();
	$('#dialog').remove();
}

function getAjaxFormFields(fields){
	var inputs = new Object();
	inputs['ajax']=1;
	for (i=0;i<fields.length;i++){
	  	if (fields[i].name != '') {
			if ((fields[i].type == 'radio' || fields[i].type == 'checkbox')){
				if (fields[i].checked) inputs[fields[i].name]= fields[i].value;
			}
			else
				inputs[fields[i].name]= fields[i].value;
		}
	}
	return inputs;
}

function SetSplashStatus(visible){
	if (visible){
		$('#splash').show();
		$('#dialog > div.content').hide();
	}
	else{
		$('#splash').hide();
		$('#dialog > div.content').show();
	}
}

$(function() {
		var f = function(){
			if ($('div#dialog') && options['url'] == downloadsUrl && dldInProgress){
				 $.get(options['url'],{ajax:1,ready:1},function (data){
						if (data != 'WAITING'){
							$('div.download_container').html(data);
							dldInProgress = false;
						}
					});
			}
		}   
    	setInterval( f, 2000 );
});


$(document).ready(function() {

	$("#zoom_query").click(function() {
		$(this).val('');
	});
	

    $("#sitemap").hide();
    $('#showMap').toggle(function() {
        $("#showMap").removeClass('closed');
        $("#showMap").addClass('open');
        $("#showMap").text("Hide Sitemap");
        $("#sitemap").slideDown('slow');
        setTimeout("$.scrollTo($('#footer'), 1000)", 900);
    }, function() {
        $("#showMap").removeClass('open');
        $("#showMap").addClass('closed');
        $("#showMap").text("Show the content of this report");
        $("#sitemap").slideUp('slow');
    });

    /*Report tools start here*/
    basepath = $('#header h1 a').attr('href').replace("index.html", "");
	basepath1 = $('#submit').attr('src').replace("/images/buttons/btnGo.gif", "");
    //define urls
    //var x = document.location.href.replace('http://','');
    //var y = $('#header > h1 > a').attr('href');

    //basepath = x.substring(x.indexOf('/'),x.lastIndexOf('/')+1)+y.replace('index.html','');
    //define urls
    notesUrl = basepath + 'scripts/notepad.asp';
    printUrl = basepath + 'scripts/print.asp';
    linksUrl = basepath + 'scripts/links.asp';
    mailerUrl = basepath + 'scripts/email.asp';
    exitUrl = basepath + 'scripts/exit.asp';
    feedbackUrl = basepath + 'scripts/feedback.asp';
    downloadsUrl = basepath + 'scripts/downloads.asp';
    $('#print_all').click(function() {
        $('input.' + $(this).attr('class')).attr('checked', $(this).attr('checked'));
    });
    $('#delete_all').click(function() {
        $('input.' + $(this).attr('class')).attr('checked', $(this).attr('checked'));
    });
    $('#email_all').click(function() {
        $('input.' + $(this).attr('class')).attr('checked', $(this).attr('checked'));
    });

    $('a.printButton').click(function() {
        window.print();
    });
    NotesCookie = $.ajax({ type: 'GET', url: notesUrl, async: false, data: { ajax: 1, ajxcmd: 'getcookie' }, dataType: 'text' }).responseText;
    PrintCartCookie = $.ajax({ type: 'GET', url: printUrl, async: false, data: { ajax: 1, ajxcmd: 'getcookie' }, dataType: 'text' }).responseText;
    LinkCartCookie = $.ajax({ type: 'GET', url: linksUrl, async: false, data: { ajax: 1, ajxcmd: 'getcookie' }, dataType: 'text' }).responseText;

    $('#notes').click(function() {
        var callbackFn = function() {
            NotesCookie = $.ajax({ type: 'GET', url: notesUrl, async: false, data: { ajax: 1, ajxcmd: 'getcookie' }, dataType: 'text' }).responseText;
            setNotepadPageStatus();
        };
        options = { url: notesUrl, params: { ajax: 1, add: getPageFromLink('a#notes') }, CancelAction: '#CancelNote', DoAction: '#SaveNote', submitCallback: callbackFn };
        displayPopup();
        return false;
    });

    $('li.email a').click(function() {
        options = { url: mailerUrl, params: { ajax: 1, pg: getPageFromLink('li.email a') }, CancelAction: '#CancelSend', DoAction: '#ActionSend' };
        displayPopup();
		$(window).scrollTo($('body'), 800);
        return false;
    });

    $('li.feedback a').click(function() {
        options = { url: feedbackUrl, params: { ajax: 1 }, CancelAction: '#CancelSend', DoAction: '#ActionSend' };
        displayPopup();
        return false;
    });
	
	
    $('.case-study a').click(function() {
        caseStudyUrl = $(this).attr('href');
		options = { url: caseStudyUrl, params: { ajax: 1}, CancelAction: '#CancelSend', DoAction: '#ActionSend' };
        displayPopup();	
		$(window).scrollTo($('body'), 800);
        return false;
    });	


    $('a.external').click(function() {
        var surl = (this.href.indexOf('url=') > 0) ? this.href.substring(this.href.indexOf('url=') + 4) : this.href;
        options = { url: exitUrl, params: { url: surl }, CancelAction: '#CancelExit' };
        displayPopup();
		$(window).scrollTo($('body'), 800);
        return false;
    });

    $('.downloadButton').click(function() {
        var dlds = $('input[name=dld]:checked');
        var docs = '';
        for (i = 0; i < dlds.length; i++) docs += ',' + dlds[i].value;
        if (docs.length > 0) {
            docs = docs.substr(1);
            var loadCallback = function() { dldInProgress = true; };
            options = { url: downloadsUrl, params: { ajax: 1, dld: docs }, loadCallback: loadCallback };
            displayPopup();
        }
        return false;
    });
    // Cookie js finds if persistent cookies are enabled
    setCartPageStatus();
    setNotepadPageStatus();
    setLinkCartStatus();

    $('a#goto')
		.click(function() {
		    window.open($(this).attr('href'));
		    return false;
		});

    /*Report tools end here*/
    // breadcrumbs slash apend and remove
    $('#breadcrumb li').append('<span class="slash"> /</span>');
    $('#breadcrumb li.last .slash').remove();





    // facybox init
    //$('a[rel*=facybox]').facybox({
    //noAutoload: true
    //     });


    //heading replace
    $('.financial-highlights .mainimage h1').replaceWith("<h1>In an increasingly connected society, Gemalto is the leader in making digital interactions secure and easy</h1>");
    $('.hidden').hide();


    //backtotops
    var mainHeight = $("#main").height();
    if (mainHeight >= 700) {
        $('#main').append('<p class="backtotop"><a href="#navarea">Back to top</a></p>');
    };

	/*
    // Even nav
    function evenNav(items, width) {
        var theWidth = 0;
        items = items * 2;
        $('#navigation li').each(function() {
            theWidth = theWidth + $(this).width();
        });
        var spacing = width - theWidth;
        spacing = spacing / items;
        spacing = Math.floor(spacing);
        $('#navigation li a').css("padding-left", spacing).css("padding-right", spacing)
        $('#navigation li.last a').css("padding-right", spacing + 2)
    };
    evenNav("7", "984");
	*/



    //TOGGLE!

    $('.col p.more-toggle').addClass('pointer');

    //Hide (Collapse) the toggle containers on load
    $(".more-information").hide();

    //Switch the "Open" and "Close" state per click
    $("p.more-toggle").toggle(function() {
        $(this).addClass("active");
        $(this).html("Hide information");
        $(this).next(".more-information").slideDown("medium");
    }, function() {
        $(this).removeClass("active");
        $(this).html("See more information");
        $(this).next(".more-information").slideUp("medium");
    });





    //ADDISON DEFAULT ACCORDION
    if ($('#accordion').length != 0) {
        $('#accordion h3').next("div").each(function() {
            if ($('body').css('minWidth') == undefined) {
                $(this).attr("rel", $(this).height() + 30);
            } else {
                $(this).attr("rel", $(this).height());
            };
        });
        // Open first one
        //$('#accordion h3:first').addClass("ui-state-active").next('div');
        $('#accordion h3')//.not('#accordion h3:first')
			.each(function() {
			    $(this).next("div").css("height", "0");
			    $(this).next("div").addClass("closedAccordion");
			});
        $('#accordion h3').live("click", function() {
            accordion($(this), "#accordion h3", "", $('#accordion h3').index(this));
            activeAccSubSection = false;
            return false;
        });
    };
    // END ADDISON DEFAULT ACCORDION

    // variable for accordion section
    activeAccSubSection = false;
    // variable for accordiion subsection and anchor
    activeAccSubSectionAnchor = false;
    if (location.hash) {
        // remove the # (the hash can container either a number or text)
        var activeAcc = (location.hash).replace('#', '');
        // test to find subsection anchor
        activeAccSubSectionTest = activeAcc.indexOf('=');
        activeAccSubSection = false;
        // if test is true
        if (activeAccSubSectionTest != '-1') {
            // split the varibale example 2(accordion sub section) and animal(anchor within subsection)
            activeAccSubSection = activeAcc.split('=', [2]);
            // place sub section accordion varibale within activeAcc
            activeAcc = activeAccSubSection[0];


        } else {
            // else test if activeAcc is either a number or text
            if (/^\d*$/.test(activeAcc)) {
                // if a number turn into a interger
                activeAcc = parseInt(activeAcc);


            } else {
                // else activeAcc is a text anchor
                activeAccSubSectionAnchor = activeAcc;
            }
        }
        activeAcc = parseInt(activeAcc);

        if (activeAccSubSectionAnchor) {
            // them scroll to anchor
            $(window).scrollTo($('#' + activeAccSubSectionAnchor), 800);
        } else {
            accordion($('#accordion h3').eq(activeAcc), "#accordion h3", "true", activeAcc);
        }
    };
	/*
	else {
        // no hash then false
        var t = setTimeout(firstItem, 1000);
    };
	*/





    // Accordion function with options - pass in the object (Header) thats been clicked
    function accordion(element, header, type, index) {


        var height = element.next("div").attr('rel');

        if (element.hasClass("ui-state-active")) {

            element.next("div").animate({
                height: "0"
            }, 500, function() {
                element.next("div").animate({
                    marginTop: "0"
                }, 50, function() { })
            }).addClass("closedAccordion").prev(header).removeClass("ui-state-active");
        } else {

            element.next("div").animate({
            //marginTop: "15px"


        }, 50, function() {
            element.next("div").animate({
                height: height
            }, 500, function() {
                if (type != 'false') {
                    if (!activeAccSubSection) {
                        $(window).scrollTo($(header).eq(index), 800);

                    } else {
                        $(window).scrollTo($('#' + activeAccSubSection[1]), 800);
                    }
                }
            }).removeClass("closedAccordion").prev(header).addClass("ui-state-active");
        });
    };
};
/*
function firstItem() {
    accordion($('#accordion h3:first'), '#accordion h3', 'false', 0);
};
*/
// End Accordion function with options - pass in the object (Header) thats been clicked






$('#content a').not('#shape-slider #navigation a').not('#content #business-drivers #navigation li a').click(function() {
    theLink = false;
    theLink = $(this).attr('href');
    theLinkTest = theLink.indexOf('#');
    if (theLinkTest != '-1') {
        var splitHash = $(this).attr('href').split('#');
        // remove #
        var activeAcc = splitHash[1];
        // test to find subsection anchor
        activeAccSubSectionTest = activeAcc.indexOf('=');
        activeAccSubSection = false;
        // if test is true
        if (activeAccSubSectionTest != '-1') {
            // split the varibale example 2(accordion sub section) and animal(anchor within subsection)
            activeAccSubSection = activeAcc.split('=', [2]);
            // place sub section accordion varibale within activeAcc
            activeAcc = activeAccSubSection[0];
        }
        // test if activeAcc is either a number or text
        if (/^\d*$/.test(activeAcc)) {
            activeAcc = parseInt(activeAcc);
            $(window).scrollTo($('body'), 800, function() {
                // if a sub section with a anchor exist then scroll that anchor after accordion has opened correct section
                isclosed = $('#accordion h3').eq(activeAcc).next().hasClass('closedAccordion');

                if (activeAccSubSection) {
                    if (isclosed) {
                        accordion($('#accordion h3').eq(activeAcc), "#accordion h3", "true", activeAccSubSection[1]);
                    } else {
                        $(window).scrollTo($('#' + activeAccSubSection[1]), 800);
                    }
                } else {
                    // if no anchor within a sub section exist then scroll to open accordion
                    if (isclosed) {
                        accordion($('#accordion h3').eq(activeAcc), "#accordion h3", "true", activeAcc);
                    } else {
                        $(window).scrollTo($('#accordion h3').eq(activeAcc), 800);
                    }
                }
            });
        } else {
            // if #hash change was for a anchor within already open accordion then just go the right anchor without any accodion effect
            $(window).scrollTo($('#' + activeAcc), 800);
        }
    }
});



$(document).ready(function(){
	if ($('#notes_nav ul').length != 0) {
		$('#notes_nav ul').makeacolumnlists({cols:2,colWidth:0,equalHeight:true,startN:1});
	}
	
});



//multi col lists

//$('.cols2 ul').makeacolumnlists({cols:2,colWidth:300});





	// BOARD OF DIRECTORS SECTION
	
	$('#board-of-directors-app #left-panel div').not(':first').hide();
	
	var thisImg=basepath1+'/images/content/board/';
	$('#board-of-directors-app #right-panel li').hover(function(){
		var thisImg=basepath1+'/images/content/board/';
    	$(this).find('img').attr('src',thisImg+$(this).find('img').attr('id')+'-over.jpg');
	},function(){
    	var thisImg=basepath1+'/images/content/board/';
        $(this).not(".current_board").find('img').attr('src',thisImg+$(this).find('img').attr('id')+'.jpg');
	});
	
    $('#board-of-directors-app #right-panel li').live('click',function(){
    	var thisImg=basepath1+'/images/content/board/';
		$(".current_board").find('img').attr('src',thisImg+$(".current_board").find('img').attr('id')+'.jpg');
		$(this).find('img').attr('src',thisImg+$(this).find('img').attr('id')+'-over.jpg');
		$('#board-of-directors-app #right-panel li').removeClass("current_board");
		$(this).addClass("current_board");
		$('#board-of-directors-app #left-panel div').hide();
        $('#board-of-directors-app #left-panel div').eq($('#board-of-directors-app #right-panel li').index(this)).show();
		$('#board-of-directors-app #content-panel div.sets').hide();
		$('#board-of-directors-app #content-panel div.sets').eq($('#board-of-directors-app #right-panel li').index(this)).show();
	});
	

	$('#board-of-directors-app #content-panel div.sets').not(':first').hide();
	$('#board-of-directors-app #content-panel div.sets div').hide();
	$('#board-of-directors-app #content-panel div.default').show();

    $('#board-of-directors-app #left-panel div:visible .member-nav li').live('click',function(){
		$('#board-of-directors-app #left-panel div:visible .member-nav li').removeClass("current_board_member");
		$(this).addClass("current_board_member");
		$('#board-of-directors-app #content-panel div:visible div').hide();	
        $('#board-of-directors-app #content-panel div:visible div').eq($('#board-of-directors-app #left-panel div:visible .member-nav li').index(this)).show();
		return false;
	});
	
	// BOARD OF DIRECTORS SECTION ENDS


	var marginleft = $('#business-drivers #right-panel ul#sliding-content').css("marginLeft");
	
	if(marginleft == '0px'){
		$('#business-drivers #navigation #back a').hide();
	}
	
	
	$('#business-drivers #right-panel ul li.first').addClass('current');


	$('#business-drivers #left-panel ul li').not(':first').hide();
	$('#business-drivers #right-panel ul li').css('float','left');
	$('#business-drivers #navigation #more a').click(function(){
		var marginleft = $('#business-drivers #right-panel ul#sliding-content').css("marginLeft");
		if(marginleft == '0px'){
			$('#business-drivers #right-panel ul li.first').removeClass('current');
			$('#business-drivers #right-panel ul#sliding-content').animate({'marginLeft': '-578px'}, function(){
				$('#business-drivers #right-panel ul li.third').addClass('current');
				$('#business-drivers #right-panel ul li.shorter').removeClass('current');																												  																										
			});
			$('#business-drivers #navigation #back a').show();
		}
		else if(marginleft == '-578px'){
			$('#business-drivers #right-panel ul li.third').removeClass('current');			
			$('#business-drivers #right-panel ul#sliding-content').animate({'marginLeft': '-1156px'}, function(){
				$('#business-drivers #right-panel ul li.second').addClass('current');
			});
		}
		else if(marginleft == '-1156px'){
			$('#business-drivers #right-panel ul li.second').removeClass('current');
			$('#business-drivers #right-panel ul#sliding-content').animate({'marginLeft': '-1734px'}, function(){
				$('#business-drivers #right-panel ul li.shorter').addClass('current');
			});
			$('#business-drivers #navigation #more a').hide();
		}
		return false;
	});
	


	$('#business-drivers #navigation #back a').click(function(){
		var marginleft = $('#business-drivers #right-panel ul#sliding-content').css("marginLeft");
		if(marginleft == '-1734px'){
			$('#business-drivers #right-panel ul li.shorter').removeClass('current');
						
			$('#business-drivers #right-panel ul#sliding-content').animate({'marginLeft': '-1156px'}, function(){
				$('#business-drivers #right-panel ul li.second').addClass('current');																								   	   
			});
			$('#business-drivers #navigation #more a').show();
		}
		else if(marginleft == '-1156px'){
			$('#business-drivers #right-panel ul li.second').removeClass('current');									
			$('#business-drivers #right-panel ul#sliding-content').animate({'marginLeft': '-578px'}, function(){
				$('#business-drivers #right-panel ul li.third').addClass('current');			
				$('#business-drivers #right-panel ul li.shorter').removeClass('current');																																	 																										
			});
			$('#business-drivers #navigation #more a').show();
		}
		else if(marginleft == '-578px'){
			$('#business-drivers #right-panel ul li.third').removeClass('current');		
			$('#business-drivers #right-panel ul#sliding-content').animate({'marginLeft': '0px'}, function(){
				$('#business-drivers #right-panel ul li.first').addClass('current');																										      
			});
			$('#business-drivers #navigation #back a').hide();
		}
		return false;
	});



	i = 0;
	$('#shape-container > div').each(function(){	
		i++;
	});
	width = (770*i);
	$('#shape-container').css('width', width);
	$('#shape-container > div').css('float', 'left');
	$('#shape-slider #navigation li').live('click',function(){
		$('#shape-slider #navigation li').removeClass("current");
		$(this).addClass("current");
		marginLeftSlider = $(this).index();
		marginLeftSlider = (-770*marginLeftSlider);
		$('#shape-container').animate({'marginLeft': marginLeftSlider});
		return false;
	});

	//fire off external links in a new window
	$('a[href^="http://"]').attr("target", "_blank");

});


