/// <reference path="jquery.intellisense.js"/>

/**
 * Datacard IDS User Interface
 */ 
var DATACARD = {

    /**
     * Delay before panel's reveal in miliseconds
     */         
    navigation_delay: 45,
    
    /**
     * Time of panel's scaling in miliseconds
     */          
    navigation_scaling: 300,

    /**
     * IDs of tabs on the current page.
     */
    tabIds: new Array(),

    /**
     * Init UI functions
     */         
    init: function () {    
        $(".tabs .tabs-nav").tabs();
        DATACARD.expandableContents();
        DATACARD.updateProductsNumbers();
        DATACARD.formFieldsFocus();
        DATACARD.lastTabWidth();
        DATACARD.topSearch();
        DATACARD.navigation();
        DATACARD.checkBrowseProductsTab();
        DATACARD.setTabLinks();
        DATACARD.designASolutionHelp();

        // DATACARD.rollovers();
        DATACARD.preloadImages(["/images/bg/search-text-focus.gif", 
                                "/images/bg/panel1.gif", 
                                "/images/bg/panel2.gif", 
                                "/images/bg/panel3.gif", 
                                "/images/bg/panel4.gif", 
                                "/images/bg/panel5.gif",
                                "/images/bg/top-tools-panel1.gif",
                                "/images/bg/top-tools-panel2.gif",
                                "/images/bg/top-tools-panel3.gif",
                                "/images/buttons/help.png",
                                "/images/buttons/help-h.png",
                                "/images/buttons/reset.png",
                                "/images/buttons/reset-h.png"
                                ]); 
        if($.browser.msie){
            DATACARD.fixIE6flicker(true);
        }
    },
    
    setTabLinks: function()
    {
        $('a.tab-link').click(function()
        {
            document.location = $(this).attr('href');
            DATACARD.checkBrowseProductsTab();
        });
    },
    
    /**
     * Expandable products content zones
     */
    expandableContents: function () {
        
        /**
         * These settings can affect the overall performance of the page -
         * longer duractions provide better animation effects, but seem
         * to degrade performance more.
         */
        // Duration of page scrolling to expandable zone
        var scrolling_duration = 40; //500
        
        // Duration of closing all expandable zones at once except current
        var scrolling_offset = -20; //-20
        
        // Duration of expanding expandable zone
        var expanding_duration = 100; //400
        
        // Duration of closing all expandable zones at once except current
        var collapsing_all_duration = 100; //1200
        
        // Expanding / Collapsing Content Zones - used when user clicks on header bar of current zone
        $('.expandable h2 a').click(function () {
            var link = $(this);
            var expandable = $(link).parents('.expandable');
            var expandable_inner = expandable.find('.expandable-inner');
            var is_closed = expandable.hasClass("expandable-closed");

            // Target expandable is closed - scroll to it and open it
            if (is_closed) {
                
                // Do not use scroll for Opera until jQuery.ScrollTo is fixed, then this condition can be removed
                if ($.browser.opera) {
                     expandable_inner.slideDown(expanding_duration);
                     expandable.removeClass("expandable-closed");
                } else {
                    $.scrollTo(link, scrolling_duration, {
                        offset: scrolling_offset, 
                        onAfter: function () {
                            expandable_inner.slideDown(expanding_duration);
                            expandable.removeClass("expandable-closed");
                        }
                    });
                }
            
            // Target expandable is opened - just close it
            } else {
                expandable_inner.slideUp(expanding_duration, function callback() {
                    expandable.addClass("expandable-closed");
                });
            }
            return false;
        });
        
        
        // [ADD] Expanding / Collapsing Content Zone for How to Design a Solution instructions
        $('.instructions-expandable h2 a').click(function () {
            var link = $(this);
            var expandable = $(link).parents('.instructions-expandable');
            var expandable_inner = expandable.find('.instructions-expandable-inner');
            var is_closed = expandable.hasClass("instructions-expandable-closed");

            // Target expandable is closed - scroll to it and open it
            if (is_closed) {
                // Do not use scroll for Opera until jQuery.ScrollTo is fixed, then this condition can be removed
                if ($.browser.opera) {
                     expandable_inner.slideDown(expanding_duration);
                     expandable.removeClass("instructions-expandable-closed");
                } else {
                    $.scrollTo(link, scrolling_duration, {
                        offset: scrolling_offset, 
                        onAfter: function () {
                            expandable_inner.slideDown(expanding_duration);
                            expandable.removeClass("instructions-expandable-closed");
                        }
                    });
                }            
            // Target expandable is opened - just close it
            } else {
                expandable_inner.slideUp(expanding_duration, function callback() {
                    expandable.addClass("instructions-expandable-closed");
                });
            }
            return false;
        });
        
        // Expanding / Collapsing Content Zones - used when user clicks on View all link
        $('.expandable .view-all a').click(function () {
            
            var id = $(this).parents('.expandable').attr("id");
            var expandable = $('#' + id);
            var additional_products = expandable.find('.additional-products');
            
            // Do not use scroll in Opera until jQuery.ScrollTo is fixed, then this condition can be removed
            if ($.browser.opera) {
                  additional_products.slideDown(expanding_duration, function callback() {
                      expandable.find('.view-all').slideUp(expanding_duration);
                  });
            } else {
                $.scrollTo($(expandable), scrolling_duration, {
                    offset: scrolling_offset, 
                    onAfter: function () {
                        additional_products.slideDown(expanding_duration, function callback() {
                            expandable.find('.view-all').slideUp(expanding_duration);
                        });
                    }
                });
            }
            return false;
        });        
        
        // Expanding / Collapsing Content Zones Within Tabbed Area (Browse Products) - used when user clicks on subcategory top navigation or sub-category link in the Products and Services navigation panel
        $('.tabs-products .top-nav li a, .panel1 .list1 a, .panel1 .list2 a').click(function () {
            var link = this;
            var id = this.hash.replace("#", "");
            var expandable = $('#' + id);
            var expandable_inner = expandable.find('.expandable-inner');
            var additional_products = expandable.find('.additional-products');
            var is_closed = expandable.hasClass("expandable-closed");
            
            // Mark current state of subcategory top navigation
            $('.tabs-products .top-nav li a').removeClass('current');
            
            $('.tabs-products .top-nav li a').each(function (i) {
                if (link.hash === this.hash) {
                    $(this).addClass('current');    
                }
            });
                        
            // Target expandable is closed - close all others expandables, scroll to target and open it including additional products
            if (is_closed) {
                
                $('.expandable-inner').not($(expandable_inner)).slideUp(collapsing_all_duration, function callback() {
                
                    // Do not use scroll in Opera until jQuery.ScrollTo is fixed, then this condition can be removed
                    if ($.browser.opera) {
                        expandable_inner.slideDown(expanding_duration);
                        expandable.removeClass("expandable-closed");
                    } else {
                        $.scrollTo($(expandable), scrolling_duration, {
                            offset: scrolling_offset, 
                            onAfter: function () {
                                expandable_inner.slideDown(expanding_duration, function callback() {
                                    additional_products.slideDown(expanding_duration, function callback() {
                                        expandable.find('.view-all').slideUp(expanding_duration);
                                    });
                                });
                                expandable.removeClass("expandable-closed");
                            }
                        });                
                    }
                }).parent().addClass("expandable-closed");
                
            // Target expandable is opened - close all others expandables, scroll to target and open additional products
            } else {
                $('.expandable-inner').not($(expandable_inner)).slideUp(collapsing_all_duration, function callback() {
                    
                    // Do not use scroll in Opera until jQuery.ScrollTo is fixed, then this condition can be removed
                    if (!$.browser.opera) {
                        $.scrollTo($(expandable), scrolling_duration, { 
                            offset: scrolling_offset,
                            onAfter: function () {
                                additional_products.slideDown(expanding_duration, function callback() {
                                    expandable.find('.view-all').slideUp(expanding_duration);
                                });
                            } 
                        });
                    } else {
                        additional_products.slideDown(expanding_duration, function callback() {
                            expandable.find('.view-all').slideUp(expanding_duration);
                        });
                    }
                }).parent().addClass("expandable-closed");
            }
            return false;
        });
    },
    
    /**
     * Update products number in all content zones
     * Call this functions when you insert products to content zones      
     */         
    updateProductsNumbers: function () {
        $('.expandable').each(function (i) {
            
            var number;
            
            if ($(this).find('.product').length) {
                number = $(this).find('.product').length;
            } else {
                number = $(this).find('.item').length;
            }
            
            $(this).find('.number').text(number);

        });
    }, 
    
    /**
     * Correct last tab width so it is aligned when it's selected
     */         
    lastTabWidth: function () {
        var tabs_width = 0;
        
        $('.tabs-nav > li').each(function (i) {
            tabs_width += $(this).width();
        });
        
        if (tabs_width < 669) {
            var padding_right = 17 + (669 - tabs_width);
            $('.tabs-nav #last-tab a').css('padding-right', padding_right)
        }
    },

    /**
     * Clear text inputs, selects and textareas set border color on focus 
     */          
    formFieldsFocus: function () {
        
        $(':text:not(.search-text)').addClass('text-input');
       
        // Selects
        $('select').focus(function () {
            $(this).addClass('focus');
        }); 
        
        // Inputs and textareas
        $(':text:not(.search-text), textarea').each(function (i) {
            
            var initial_value = $(this).attr('value');
        
            $(this).focus(function () {
                $(this).addClass('focus');
            
                // Clear only initial value
                if ($(this).attr('value') === initial_value) {
                    $(this).attr('value', '');
                }
            }); 
        });
        
        // Restore border line color on focus
        $(':text, select, textarea').blur(function () {
            $(this).removeClass('focus');
        });
    },
    
    /**
     * Clear top search input on focus and set white background
     */          
    topSearch: function () {
       $('#top-search .search-text').focus(function () {
            $(this).addClass('search-focus');
            $(this).attr('value', '');
        }); 
    },

    /**
     * Main and Top Tools Navigation
     */
    navigation: function () {
        $("#nav > li, .top-tools #tool1, .top-tools #tool2, .top-tools #country-select").not("#nav03").hoverIntent({
            sensitivity: 7,
            interval: DATACARD.navigation_delay,
            over: expand,
            timeout: 0,
            out: collapse                
        });

        function expand() {
            var panel = $(this).find(".panel");
            $(this).addClass("hover");
            
             /* IE 6 select bug fix - set height of iframe dynamically */
            panel.find('iframe').height(panel.height());

            panel.css('z-index', 150);
            panel.slideDown(DATACARD.navigation_scaling);
        }
        
        function collapse() {
            var panel = $(this).find(".panel");
            panel.css('z-index', 100)
            panel.slideUp(DATACARD.navigation_scaling, function callback() {
                $(this).parent().removeClass("hover");
            });
        }
    },

    designASolutionHelp: function () {
    
    
    		$('.design-solution #top-radio a.help').hover(
				function() {
				$('.design-solution #top-radio .balloon').parent().css('z-index',14000);
				
				
				//alert($('.balloonHeader').css('display'));
				//$('.balloonHeader').css('display');
				//$(this).children('.content .balloonHeader').show();
				//$(this).siblings('.balloonHeader').fadeIn('250');
				
				//alert($(this).children('.content .balloonHeader').css('display'));
				
					//alert($('.design-solution .top').css('z-index'));
					//$(this).css('z-index', 14000);
					//$(this).parent().parent().children().css('z-index', 14000);
					
					$(this).parent().children('.balloon').fadeIn('250');
					//$(this).children().css('z-index', 15000);
					//$('.balloonIframe').css('z-index', 14000);		
	         
				},
				function() {
					//$(this).children().fadeOut('100');
	       $(this).parent().css('z-index', 0);
	        $(this).children().css('z-index', 0);	
	        
	        $(this).parent().children('.balloon').fadeOut('100');
	        //$(this).children('.balloon').hide();
	
			}
  	);  
    

		$('.design-solution a.help').hover(
			function() {

//$('div .balloon').css('left', -276);
////$(this).parent().children('.balloon').css('left', -276);
////$(this).parent().children('.balloon').css('bottom', 20);


				//alert('p');
				//$(this).css('z-index', 14000);
				//$(this).parent().parent().children().css('z-index', 14000);
				
				$(this).parent().children('.balloon').fadeIn('250');
				
//font-size: 14px; line-height: 32px;				

				
////  sIFR.replaceElement(".balloonHeader h2", named({sFlashSrc: "/id/flash/fonts/sifr2/tradegothic-bold-cond20.swf", sColor: "#336699", sWmode: "transparent"}));
				
				//$(this).children().css('z-index', 15000);
				//$('.balloonIframe').css('z-index', 14000);		
  //sIFR.replaceElement(".balloonHeader h2", named({sFlashSrc: "/id/flash/fonts/sifr2/tradegothic-bold-cond20.swf", sColor: "#336699", sWmode: "transparent"}));

         
			},
			function() {
				//$(this).children().fadeOut('100');
       //$(this).parent().css('z-index', 0);
        //$(this).children().css('z-index', 0);	
        
        $(this).parent().children('.balloon').fadeOut('100');
        //$(this).children('.balloon').hide();
  //sIFR.replaceElement(".balloonHeader h2", named({sFlashSrc: "/id/flash/fonts/sifr2/tradegothic-bold-cond20.swf", sColor: "#336699", sWmode: "transparent"}));

			}
		);


    },    
    
    /**
     * Fix background image flicker in IE6
     */         
    fixIE6flicker: function (fix) {
        try {
            document.execCommand("BackgroundImageCache", false, fix);
        } catch(err) { }
    },  
    
    /**
     * Rollovers buttons
     */
    rollovers: function () {
    
        $('.rollover').hover(
            function () {
                var rollover_src = $(this).attr('src').replace(/.png/, '_over.png');
                if ($.browser.msie) {
                     $(this).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + rollover_src + "\', sizingMethod='scale');");
                } else {
                     $(this).attr('src', rollover_src);
                }
            },
            function () {
                $(this).attr('src', $(this).attr('src').replace(/_over.png/, '.png'));
            }
        );
     },
    
    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
        }
    },
    
    /** 
     * Determine if we should jump to content within a tab on the current page. If so, highligth tab and jump down.
     * this is a work around for linking deep into a page when the desired id is hidden under a second tab.
     * this functionality is specific to the browse products page.
     * Added 8/25/2008 ef / bswing
     */
    /**
     * Attempts to match a fragment identifier to an expandable box in the current page
     * and if found, opens that expandable box.
     */
    checkBrowseProductsTab:function()
    {
        // Get the fragment identifier from the location (if present).
        var loc = document.location.toString();
        var start_index = loc.indexOf("#");
        var end_index = loc.length;

        if(start_index > -1) // A fragment identifier exists
        {
            // Now attempt to find a matching expandable box in the page. 
            var anchor_text = loc.substring(start_index,end_index);
            var numTabIds = DATACARD.tabIds.length;
            var isMatch = false;
            var isMatchTabIndex = 0;
            
            var browseAllRegex = new RegExp("browse-all");
            var isBrowseAllLink = browseAllRegex.test(anchor_text);
            
            if (isBrowseAllLink)
            {
                // Browser check        
	            var minMSIEVersion = 8;
	            var uBrowser;
	            var uVersion;
	            jQuery.each(jQuery.browser, function(i, val)
                {
                    if ((i == "msie") && (val == true))
                    {
                        uBrowser = i;
                    }
                    else if ((i == "mozilla") && (val == true))
                    {
                        uBrowser = i;
                    }
                    else if ((i == "safari") && (val == true))
                    {
                        uBrowser = i;
                    }
                    else if ((i == "opera") && (val == true))
                    {
                        uBrowser = i;
                    }
                    
                    uVersion = new Number(jQuery.browser.version.substr(0,3));
                });
                
                if (anchor_text == "#standard-browse-all")
                {
                    // Open the corresponding parent tab.
                    $(".tabs .tabs-nav").tabs('select', 0);
                    // Reset the body links.
                    $('#standard-products .top-nav li a').removeClass('current');
                    // Expand contents -
                    // If IE7, handle as default page entry (IE7 has trouble w/transparent images)
                    if (!((uBrowser == 'msie') && (uVersion == 7)))
                    {
                        $('#standard-products .expandable').each(function()
                        {
                            $(this).find('.expandable-inner').slideDown(0);
                            $(this).removeClass("expandable-closed");
                        });
                    }
                }
                else if (anchor_text == "#enterprise-browse-all")
                {
                    // Open the corresponding parent tab.
                    $(".tabs .tabs-nav").tabs('select', 1);
                    // Reset the body links.
                    $('#enterprise-products .top-nav li a').removeClass('current');
                    // Expand contents - 
                    // If IE7, handle as default page entry (IE7 has trouble w/transparent images)
                    if (!((uBrowser == 'msie') && (uVersion == 7)))
                    {
                        $('#enterprise-products .expandable').each(function()
                        {
                            $(this).find('.expandable-inner').slideDown(0);
                            $(this).removeClass("expandable-closed");
                        });
                    }
                }
            }
            else
            {
                // Attempt to find a matching expandable box.
                for (var i = 0; i < numTabIds; i++)
                {
                    if ($(anchor_text).parents(DATACARD.tabIds[i]).length > 0)
                    {
                        isMatch = true;
                        isMatchTabIndex = i;
                        break;
                    }
                }

                if (isMatch == true) // Found a matching expandable box
                {
                    // Open the corresponding parent tab.
                    $(".tabs .tabs-nav").tabs('select', isMatchTabIndex);
                    
                    // Trigger the same click event as the body links (within the tabs).
                    var a = $('.top-nav li a[href=' + anchor_text + ']');
                    a.click();
                }
            }
        }
    }
}

function showhide(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		if (obj.style.display == "none"){
			obj.style.display = "";
		} else {
			obj.style.display = "none";
		}
	}
}