/**
* @version 1.0
* @requires jQuery Core 1.3.2 - http://www.jquery.com
*/
/**
* @namespace Root namespace for holding all objects created for GP3
*/
var GP3 = window.GP3 || {};
/**
 * @namespace Namespace container for all data properties for the GP3 namespace
 *
 * @memberOf GP3
*/
GP3.data = GP3.data || {};
/**
 * Public object for storing information about Accordions
 *
 * @object
 * @memberOf GT.data
*/
GP3.data.accordion = {
    type1 : {
        name : '',
        attr : {
	    collapsible: true,
	    autoHeight: false,
	    active: 0
	}
    },
    type2 : {
        name : '',
        attr : {
	    collapsible: true,
	    autoHeight: false,
	    active: 1
	}
    },
    type3 : {
        name : '',
        attr : {
	    collapsible: true,
	    autoHeight: false,
	    active: 2
	}
    }
};
/**
 * Public object for storing information about Carousels
 *
 * @object
 * @memberOf GP3.data
*/
GP3.data.carousel = {
    type1 : {
        name : '.name1',
        attr : {
	    collapsible: true,
            active: false
	}
    }
    
};
/**
 * Public object for storing information about Tabs
 *
 * @object
 * @memberOf GP3.data
*/
GP3.data.tabs = {
    
};
/**
 * Public object for storing information about toggle
 *
 * @object
 * @memberOf GP3.data
*/
GP3.data.toggle = {
    type1 : {
	controlExtra: '',
	defaultStatus: 'close',
	animation: '',
	openTxt: 'View all',
	closeTxt: 'View less'	
    },
    type2 : {
	controlExtra: '',
	defaultStatus: 'open',
	animation: 'fast',
	openTxt: 'View all',
	closeTxt: 'View less'	
    },
    type3 : {
	controlExtra: 'tr', /*ie fix for table toggle*/
	defaultStatus: 'close',
	animation: '',
	openTxt: 'View all',
	closeTxt: 'View less'	
    }
};

/**
* Object that stores useful functions throughout the GP3 site.
* @namespace Object that stores useful functions throughout the GP3 site.
* @member GP3
*/ 
GP3.utilities = {
    /**
     * Public boolean flag set to true if browser is Internet Explorer and less than 8
     *
     * @boolean
     * @memberOf GT.utilities
     */
    isLtIE8: false,
    /**
     * Public boolean flag set to true if browser is Internet Explorer and less than 7
     *
     * @boolean
     * @memberOf GT.utilities
     */
    isLtIE7: false,
    /**
     * Public boolean flag set to true if browser is Internet Explorer
     *
     * @boolean
     * @memberOf GT.utilities
     */
    isIE: false,
    /**
     * PNG handling
     * based on supersleight.js
     * Edits: Check data object based on the image name and only png-24 apply filtering
     *
     * @function
     * @memberOf GP3.utilities
     */    
    pngHandling : (function() {	
        var root = false;
        var applyPositioning = false;
        // Path to a transparent GIF image
        var shim = '/assets/img/css/x.gif';	
        // RegExp to match above GIF image name
        var shim_pattern = /x\.gif$/i;
        var bg_fnFixPng = function(obj) {
                var mode = 'scale';
                var bg = obj.currentStyle.backgroundImage;
                var src = bg.substring(5,bg.length-2);
                if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
                        mode = 'crop';
                }
                obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
                obj.style.backgroundImage = 'url('+shim+')';
        };

        var el_fnFixPng = function(img) {
                var src = img.src;
                img.style.width = img.width + "px";
                img.style.height = img.height + "px";
                img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
                img.src = shim;
        };
        var fnLoadPngs = function() { 
                if (root) {
                        root = document.getElementById(root);
                }else{
                        root = document;
                }
                for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) {
                        // background pngs
                        if (obj.currentStyle.backgroundImage.match(/png\-24\-.*\.png/i) !== null) {
                                bg_fnFixPng(obj);
                        }
                        // image elements
                        if (obj.tagName=='IMG' && obj.src.match(/png\-24\-.*\.png$/i) !== null){
                                el_fnFixPng(obj);
                        }
                        // apply position to 'active' elements
                        if (applyPositioning && (obj.tagName=='A' || obj.tagName=='INPUT') && obj.style.position === ''){
                                obj.style.position = 'relative';
                        }
                }
        };
        return {
                init: function(context) {
                    if (GP3.utilities.isLtIE7 === true) {
                        if (context !== undefined) {
                                this.limitTo(context);
                        }
                        fnLoadPngs();
                    }
                },		
                limitTo: function(el) {
                    root = el;
                }
        };
}()),
    
    /**
     * Public function to get the data from a class string
     *
     * @function
     * @memberOf GP3.utilities
     * @param {String} classStr The class attribute value to parse
     * @param {String} ident the identifier for the data wrapper, ie. 'data' for data:(some data)
     * @returns {String} The class data
     */
    
    getData: function(classStr, ident){
	var rg;
	if(!classStr) {
	    classStr = "";
	}
	if(typeof ident !== 'undefined') {
	    rg = new RegExp("(" + ident + "\\:*\\()([^)]+)(\\))");
	} else {
	    rg = new RegExp("(data\\:*\\()([^)]+)(\\))");
	}
	
	var holder = classStr.match(rg);
	if (holder !== null) {
		return holder[2];
	}
	else {
		return false;
	}
    }
    
};

GP3.custom = {
        /**
    * Public function for current CMS, prepare markup for use
    *
    * @function
    * @memberOf GP3.custom
   */
    cms : function(){
	jQuery('.tableContent').each(function(index){
	    var defaultNum =  parseInt(GP3.utilities.getData(this.className, "default"));
	    var controller = jQuery(this).next();
	    var defaultId = "ctrlBy-" + controller.attr('id');
	    var newTbody = "<tbody id='" + defaultId + "'> </tbody> ";
	    var defaultItem = jQuery(this).find('tr')[defaultNum];
	    if(jQuery(this).find('tr')[defaultNum + 1]){
		controller.removeClass('hidden');
		jQuery(this).find('tbody').after(newTbody);
		jQuery(defaultItem).nextAll().appendTo("#"+ defaultId);
	    }	    
	})	
    },
    /**
    * Public function to hold all functionality associated for show and hide elements
    *
    * @function
    * @memberOf GP3.custom
   */
    toggle : (function(){
        var config = {  
            controllerClassName : '.control',
            controlName : '#ctrlBy-'
        };
	var data = {};
        var init = function(){
            var controllers = jQuery(config.controllerClassName);
	    
	    for(var i = 0, j = controllers.length; i < j; i++){
		
		var type = GP3.utilities.getData(controllers[i].className, "data");
		
		data[i] = {
		    controlExtra : GP3.data.toggle[type].controlExtra,
		    defaultStatus : GP3.data.toggle[type].defaultStatus,
		    animation : GP3.data.toggle[type].animation,
		    openTxt: GP3.data.toggle[type].openTxt,
		    closeTxt: GP3.data.toggle[type].closeTxt	
		};
		
                setup(controllers[i], data[i]);
            }
        };

        var setup = function(controller, data){
	    var controllerId = controller.id;
            var controlElem = jQuery(config.controlName + controllerId);
	    
	    if(data.controlExtra != ""){
		controlElem = jQuery(config.controlName + controllerId + " " +  data.controlExtra);
	    }
	    
            if(data.defaultStatus === 'close'){
		jQuery(controlElem).hide();
            }

            jQuery(controller).bind("click", {ctrlElem: controlElem}, function(event){
		
                jQuery(event.data.ctrlElem).toggle();
	
                if(jQuery(this).hasClass('selected')){
                   jQuery(this).removeClass('selected');
                }else{
                   jQuery(this).addClass('selected');    
                }
		
                return false;
            });
        };

        return{
            init: init
        };

    }()),
    
    /**
    * Public function to hold all functionality associated with tabs
    *
    * @function
    * @memberOf GP3.custom
   */
    tabs : function(){
	//initialize team standing table tabs
        jQuery("#standingTable").tabs();
	
	//initialize car three view tabs
	jQuery("#carTabs").tabs({
	    fx: { opacity: 'toggle' , duration: 'fast'} 
	});
	
	//initialize car details view panels
	var detail = jQuery('.carDetails');
	
	detail.each(function(index){
	    var carPanel = jQuery(this).find('.detail .mid');
		carPanel.prepend("<a class='closeBtn' href='#'>Close</a>");
		
	    var closeBtn = carPanel.find('a.closeBtn');
	    var carDetail = jQuery(this).tabs({
		selected: -1,
		event: 'mouseover'
	    });
	    jQuery(closeBtn).click(function(){
		var currentIndex = carDetail.tabs('option', 'selected');
		carDetail.tabs('option', 'deselectable', true);
		carDetail.tabs('select', currentIndex);	
		carDetail.tabs('option', 'deselectable', false);
		return false;
	    }); 
	});
	
    },
    /**
    * Public function to hold all functionality associated with carousel
    *
    * @function
    * @memberOf GP3.custom
   */
    carousel : function(){
        jQuery("#carousel-partner").jCarouselLite({
            auto: 3000,
            btnNext: ".next",
            btnPrev: ".prev",
            visible: 1,
            speed: 500
        });
	jQuery(".carousel-hero").jCarouselLite({
            auto: 8000,
            visible: 1,
            speed: 300,
	    btnGo:
	    [".externalControl .1", ".externalControl .2", ".externalControl .3"],
	    afterEnd: function(obj) {
		var currentIndex = parseInt(obj.prevAll().length);
		var currentTitle = obj.find('.structural').html();	
		if(currentIndex > 3){
		    currentIndex = 1;
		}
		jQuery('.externalControl a').removeClass('selected');		
		jQuery('.externalControl .' + currentIndex).addClass('selected');
		jQuery("#caption").html(currentTitle);
	    }
        });

    },
     /**
    * Public function to hold all functionality associated with accordian
    *
    * @function
    * @memberOf GP3.custom
   */
    accordion : function(){
	var obj = jQuery("#filterAccordion");
	var type = GP3.utilities.getData(obj.attr('class'), "data") || 'type1';
	
	    jQuery("#filterAccordion").accordion(GP3.data.accordion[type].attr);
	
    }

};
/**
 * Public function to set GP3.utilities.isLtIE7, GP3.utilities.isLtIE8 flags
 *
 * @function
 * @memberOf GP3.utilities
*/
GP3.utilities.ieDetect = (function() {
    if(jQuery.browser.msie === true) {
        GP3.utilities.isIE = true;
        if(parseInt(jQuery.browser.version, 10) < 8) {
            GP3.utilities.isLtIE8 = true;
        }
        if(parseInt(jQuery.browser.version, 10) < 7) {
            GP3.utilities.isLtIE7 = true;
        }
    }
}());
jQuery(document).ready(function() {
    GP3.custom.cms();
    GP3.custom.toggle.init();
    GP3.custom.accordion();  
    GP3.custom.tabs();
});
jQuery(window).load(function(){
    GP3.utilities.pngHandling.init();
    GP3.custom.carousel();
});

