function refreshJS() {
	$('.viewResultsButton').pluginbutton({ x: '0px', y: '0', xHover: '0', yHover: '-30px', xActive: '0', yActive: '-60px' });
	$('.viewResultsBackButton').pluginbutton({ x: '0px', y: '0px', xHover: '0', yHover: '-30px', xActive: '0', yActive: '-60px' });
	$('.panelButton .middle').pluginbutton({ siblings: true, x: '0px', y: '0px', xHover: '0', yHover: '-30px', xActive: '0', yActive: '-60px' });
	//setTimeout("refreshJS()", 2000);
    overlay_manip();
}

function gridLines() {
	$('.handsetResultsRow').each(function () {
		$('.handsetPanel', $(this)).each(function (i) {
			if (i % 2 == 0) {
				$(this).addClass('middleHandsetColOdd');
			} else {
				$(this).addClass('middleHandsetColEven');
			}
		});
	});
}

(function ($) {
	$.fn.pluginbutton = function (options) {
		var myoptions = $.extend({ siblings: false, x: '0px', y: '0px', xHover: '0px', yHover: '0px', xActive: '0px', yActive: '0px' }, options);
		$(this).each(function () {
			if (myoptions.siblings === false) {
				var focus = false;
				if (focus === false) {
					$(this).hover(function(event) {
						$(this).animate({ backgroundPosition: myoptions.xHover + " " + myoptions.yHover }, { duration: 0 });
						$(this).removeClass('VBfocus').addClass('VBHover');
					}, function () {
						$(this).animate({ backgroundPosition: myoptions.x + " " + myoptions.y }, { duration: 0 });
						$(this).removeClass('VBfocus').removeClass('VBHover');
					});
				}
				$(this).mousedown(function(event) {
                    if( event.which == 1 ) {
					    focus = true;
					    $(this).animate({ backgroundPosition: myoptions.xActive + " " + myoptions.yActive }, { duration: 0 });
					    $(this).addClass('VBfocus').removeClass('VBHover');
                        //window.location.href = $(this).find('a').attr('href');
                    }
				}, function (event) {
                    if( event.which == 1 ) {
					    focus = false;
					    $(this).animate({ backgroundPosition: myoptions.x + " " + myoptions.y }, { duration: 0 });
					    $(this).removeClass('VBfocus').addClass('VBHover');
                    }
				});
			} else {
				var focus = false;
				if (focus === false) {
					$(this).parent().children().hover(function () {
						$(this).parent().children().animate({ backgroundPosition: myoptions.xHover + " " + myoptions.yHover }, { duration: 0 });
						$(this).parent().children().removeClass('VBfocus').addClass('VBHover');
						$(this).parent().find('a').addClass('VBHover');
					}, function () {
						$(this).parent().children().animate({ backgroundPosition: myoptions.x + " " + myoptions.y }, { duration: 0 });
						$(this).parent().children().removeClass('VBfocus').removeClass('VBHover');
						$(this).parent().find('a').removeClass('VBHover');
					});
				}
				$(this).parent().mousedown(function(event) {
                    if( event.which == 1 ) {
					    focus = true
					    $(this).parent().children().animate({ backgroundPosition: myoptions.xActive + " " + myoptions.yActive }, { duration: 0 });
					    $(this).parent().children().addClass('VBfocus').removeClass('VBHover');
                    }
				}, function(event) {
                    if( event.which == 1 ) {
					    focus = false;
					    $(this).parent().children().animate({ backgroundPosition: myoptions.x + " " + myoptions.y }, { duration: 0 });
					    $(this).parent().children().removeClass('VBfocus').addClass('VBHover');
                    }
				});
			}
		});
	}
})(jQuery);

$(document).ready(function () {

    $('.buyButtonBlue').hover(function () {
        var bObj = $(this);
        var bObjCon = (bObj.parent('.buyNowToolTip').length != 0 ? bObj.parent('.buyNowToolTip') : bObj.parent('.buyNowToolTipCarousel'));
        $('.bnToolTip2, .bnToolTip', bObjCon).show();
    }, function () {
        var bObj = $(this);
        var bObjCon = (bObj.parent('.buyNowToolTip').length != 0 ? bObj.parent('.buyNowToolTip') : bObj.parent('.buyNowToolTipCarousel'));
        $('.bnToolTip2, .bnToolTip', bObjCon).hide();
    });

    $('.showToolTip').hover(function () {
        var bObj = $(this);
        bObj.parent().parent().children(':last').show();
    }, function () {
        var bObj = $(this);
        bObj.parent().parent().children(':last').hide();
    });

    $('.showToolTip').live('click', function () {
        return false;
    });



    $(".panelButton .middle a").addClass('jsButton');
    $(".newAddButtonContainer input").addClass('jsButton');
    $(".buyButtonBlue a").addClass('jsButton');
    $(".buyButton a").addClass('jsButton');
    $(".viewResultsButton").addClass('jsButton');
    $(".viewResultsBackButton").addClass('jsButton');

    // New Con-Op Single Col Carousel

    $('.singleColCarousel .noJS').removeClass("noJS");
    $('.singleColCarousel').each(function (i) {
        var $this = $(this);
        $this.addClass('singleColCarouselNumber' + i);
        $('.carouselFrames', this).cycle({
            fx: 'scrollHorz',
            timeout: 6000,
            speed: 500,
            next: $this.find('.next'),
            prev: $this.find('.prev'),
            cleartypeNoBg: true,
            pager: $this.find('.carouselPaging'),
            pagerAnchorBuilder: function (idx, slide) {
                return '.singleColCarouselNumber' + i + ' .carouselPaging li:eq(' + idx + ') a';
            }
        });
    });

    $('.newDoubleColCarouselCO').each(function (i) {
        var $this = $(this);
        $this.addClass('newDoubleColCarouselNumber' + i);
        $('.carouselFrames', this).cycle({
            fx: 'scrollHorz',
            timeout: 6000,
            speed: 500,
            next: $this.find('.next'),
            prev: $this.find('.prev'),
            cleartypeNoBg: true,
            pager: $this.find('.carouselPaging'),
            pagerAnchorBuilder: function (idx, slide) {
                return '.newDoubleColCarouselNumber' + i + ' .carouselPaging li:eq(' + idx + ') a';
            }
        });

        $('.newDoubleColCarouselNumber' + i + ' .newCarouselPause').live("click", function () {
            $(this).removeClass("newCarouselPause").addClass("newCarouselPlay").text("play");
            $('.carouselFrames', $this).cycle('pause');
            return false;
        });

        $('.newDoubleColCarouselNumber' + i + ' .newCarouselPlay').live("click", function () {
            $(this).removeClass("newCarouselPlay").addClass("newCarouselPause").text("pause");
            $('.carouselFrames', $this).cycle('resume');
            return false;
        });
    });

    /* full width carousel */

    $('.fullWidthCarousel .noJS').removeClass("noJS");

    $('.fullWidthCarousel').each(function (i) {
        var $this = $(this);
        $this.addClass('FullWidthCarouselNumber' + i);
        $('.carouselFrames', this).cycle({
            fx: 'scrollHorz',
            timeout: 7000,
            speed: 500,
            next: $this.find('.next'),
            prev: $this.find('.prev'),
            cleartypeNoBg: true,
            pager: $this.find('.carouselPaging'),
            pagerAnchorBuilder: function (idx, slide) {
                return '.FullWidthCarouselNumber' + i + ' .carouselPaging li:eq(' + idx + ') a';
            }
        });

        $('.FullWidthCarouselNumber' + i + ' .newCarouselPause').live("click", function () {
            $(this).removeClass("newCarouselPause").addClass("newCarouselPlay").text("play");
            $('.carouselFrames', $this).cycle('pause');

            return false;
        });

        $('.FullWidthCarouselNumber' + i + ' .newCarouselPlay').live("click", function () {
            $(this).removeClass("newCarouselPlay").addClass("newCarouselPause").text("pause");
            $('.carouselFrames', $this).cycle('resume');
            return false;
        });
    });

    // Add Button States

    $('.newAddButtonContainer input').each(function () {
        var linkHasFocus = false;

        $(this).hover(function () {
            var $buttonElements = $(this).closest(".newAddButtonContainer").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-30px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -30px");
                    $(this).css("background-position", position);
                }
            });
        },
		function () {
		    if (!linkHasFocus) {
		        var $buttonElements = $(this).closest(".newAddButtonContainer").children();
		        jQuery.each($buttonElements, function () {
		            if ($.browser.msie) {
		                $(this).css("background-position-y", "0px");
		            }
		            else {
		                var position = $(this).css("background-position").replace("px -30px", "px 0px");
		                $(this).css("background-position", position);
		            }
		        });
		    }
		});

        $(this).focus(function () {
            var $buttonElements = $(this).closest(".newAddButtonContainer").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -60px");
                    position = position.replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = true;
            return false;
        });

        $(this).blur(function () {
            var $buttonElements = $(this).closest(".newAddButtonContainer").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "0px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -60px", "px 0px");
                    position = position.replace("px -30px", "px 0px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = false;
        });

        $(this).click(function () {
            var $buttonElements = $(this).closest(".newAddButtonContainer").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            $(this).blur();
        });


    });

    // Panel Button States

    $('.panelButton a').each(function () {
        var linkHasFocus = false;

        $(this).hover(function () {
            var $buttonElements = $(this).closest(".panelButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-30px");
                }
                else {
                    var cssPos = $(this).css("background-position");
                    if (cssPos.search(/%/) != -1) {
                        // if firefox 4, guessing is 0% 0% when no position is set... dunno though
                        var position = $(this).css("background-position").replace("% 0%", "px -30px");
                    } else {
                        // any other normal outcome.
                        var position = $(this).css("background-position").replace("px 0px", "px -30px");
                    }
                    $(this).css("background-position", position);
                }
            });
        },
		function () {
		    if (linkHasFocus === false) {
		        var $buttonElements = $(this).closest(".panelButton").children();
		        jQuery.each($buttonElements, function () {
		            if ($.browser.msie) {
		                $(this).css("background-position-y", "0px");
		            }
		            else {
		                var cssPos = $(this).css("background-position");
		                if (cssPos.search(/%/) != -1) {
		                    // if firefox 4, guessing is 0% 0% when no position is set... dunno though
		                    var position = $(this).css("background-position").replace("% -30px", "px 0px");
		                } else {
		                    // any other normal outcome.
		                    var position = $(this).css("background-position").replace("px -30px", "px 0px");
		                }
		                $(this).css("background-position", position);
		            }
		        });
		    }
		});

        $(this).focus(function () {
            var $buttonElements = $(this).closest(".panelButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -60px");
                    position = position.replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = true;
            return false;
        }
		);

        $(this).blur(function () {
            var $buttonElements = $(this).closest(".panelButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "0px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -60px", "px 0px");
                    position = position.replace("px -30px", "px 0px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = false;
        }
		);


        $(this).click(function () {
            var $buttonElements = $(this).closest(".panelButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            $(this).blur();
        }
		);


    });

    // Buy Button Blue States

    $('.buyButtonBlue a').each(function () {
        var linkHasFocus = false;

        $(this).hover(function () {
            var $buttonElements = $(this).closest(".buyButtonBlue").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-30px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -30px");
                    $(this).css("background-position", position);
                }
            });
        },
		function () {
		    if (!linkHasFocus) {
		        var $buttonElements = $(this).closest(".buyButtonBlue").children();
		        jQuery.each($buttonElements, function () {
		            if ($.browser.msie) {
		                $(this).css("background-position-y", "0px");
		            }
		            else {
		                var position = $(this).css("background-position").replace("px -30px", "px 0px");
		                $(this).css("background-position", position);
		            }
		        });
		    }
		});

        $(this).focus(function () {
            var $buttonElements = $(this).closest(".buyButtonBlue").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -60px");
                    position = position.replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = true;
            return false;
        });

        $(this).blur(function () {
            var $buttonElements = $(this).closest(".buyButtonBlue").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "0px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -60px", "px 0px");
                    position = position.replace("px -30px", "px 0px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = false;
        });


        $(this).click(function () {
            var $buttonElements = $(this).closest(".buyButtonBlue").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            $(this).blur();
        });

    });

    // Buy Button States

    $('.buyButton a').each(function () {
        var linkHasFocus = false;

        $(this).hover(function () {
            var $buttonElements = $(this).closest(".buyButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-30px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -30px");
                    $(this).css("background-position", position);
                }
            });
        },
		function () {
		    if (!linkHasFocus) {
		        var $buttonElements = $(this).closest(".buyButton").children();
		        jQuery.each($buttonElements, function () {
		            if ($.browser.msie) {
		                $(this).css("background-position-y", "0px");
		            }
		            else {
		                var position = $(this).css("background-position").replace("px -30px", "px 0px");
		                $(this).css("background-position", position);
		            }
		        });
		    }
		});

        $(this).focus(function () {
            var $buttonElements = $(this).closest(".buyButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px 0px", "px -60px");
                    position = position.replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = true;
            return false;
        });

        $(this).blur(function () {
            var $buttonElements = $(this).closest(".buyButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "0px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -60px", "px 0px");
                    position = position.replace("px -30px", "px 0px");
                    $(this).css("background-position", position);
                }
            });
            linkHasFocus = false;
        });


        $(this).click(function () {
            var $buttonElements = $(this).closest(".buyButton").children();
            jQuery.each($buttonElements, function () {
                if ($.browser.msie) {
                    $(this).css("background-position-y", "-60px");
                }
                else {
                    var position = $(this).css("background-position").replace("px -30px", "px -60px");
                    $(this).css("background-position", position);
                }
            });
            $(this).blur();
        });

    });

    /* Con Op Product Detail Tabs */

    $('.newProductTabs').each(function () {
        var $largestHeight = 0;
        $('.smallTabsContent', this).each(function () {
            if ($(this).height() > $largestHeight) {
                $largestHeight = $(this).height();
            }
        });
        //Removed so each tab retains its own height.
        //$(this).height($largestHeight);

        $(this).addClass('js');
        $('.smallTabsHeader:first', this).before('<div class="smallTabsHeaderContainer"></div>');
        var $tabHeaders = $('.smallTabsHeaderContainer', this);
        $('.smallTabsHeader', this).appendTo($tabHeaders);
        $('.smallTabsHeader', this).each(function (tabNumber) {
            $('a', this).click(function () {
                $('.selectedTab', $tabHeaders).removeClass('selectedTab');
                $(this).addClass('selectedTab');
                $(this).closest('.newProductTabs').children('.showPanel').removeClass('showPanel').addClass('hidePanel');
                $(this).closest('.newProductTabs').children('.smallTabsContent:eq(' + tabNumber + ')').removeClass('hidePanel').addClass('showPanel');
                return false;
            });
        });
    });

    /* Content Optimisation Tariff Details Overlay - Add Class */

    $(".newCOTariffDetailsContainer").addClass('JS');

    /* Content Optimisation Tariff Details Overlay: START */

    //Open

    $('.showCoTariffOverlay').click(function () {

        // this serves the content of tariffDetailOverlay based on ID (href)
        var tariffDetailOverlay = $(this).attr("href");
        $('.CoTariffDetailsLightBox .lightBoxMiddle').empty();
        $("" + tariffDetailOverlay).clone().appendTo('.CoTariffDetailsLightBox .lightBoxMiddle').show();


        $('.panelButton a').each(function () {
            var linkHasFocus = false;
            $(this).hover(function () {
                var $buttonElements = $(this).closest(".panelButton").children();
                jQuery.each($buttonElements, function () {
                    if ($.browser.msie) {
                        $(this).css("background-position-y", "-30px");
                    }
                    else {
                        var position = $(this).css("background-position").replace("px 0px", "px -30px");
                        $(this).css("background-position", position);
                    }
                });
            }, function () {
                if (!linkHasFocus) {
                    var $buttonElements = $(this).closest(".panelButton").children();
                    jQuery.each($buttonElements, function () {
                        if ($.browser.msie) {
                            $(this).css("background-position-y", "0px");
                        }
                        else {
                            var position = $(this).css("background-position").replace("px -30px", "px 0px");
                            $(this).css("background-position", position);
                        }
                    });
                }
            });
            $(this).focus(function () {
                var $buttonElements = $(this).closest(".panelButton").children();
                jQuery.each($buttonElements, function () {
                    if ($.browser.msie) {
                        $(this).css("background-position-y", "-60px");
                    }
                    else {
                        var position = $(this).css("background-position").replace("px 0px", "px -60px");
                        position = position.replace("px -30px", "px -60px");
                        $(this).css("background-position", position);
                    }
                });
                linkHasFocus = true;
                return false;
            });
            $(this).blur(function () {
                var $buttonElements = $(this).closest(".panelButton").children();
                jQuery.each($buttonElements, function () {
                    if ($.browser.msie) {
                        $(this).css("background-position-y", "0px");
                    }
                    else {
                        var position = $(this).css("background-position").replace("px -60px", "px 0px");
                        position = position.replace("px -30px", "px 0px");
                        $(this).css("background-position", position);
                    }
                });
                linkHasFocus = false;
            });
            $(this).click(function () {
                var $buttonElements = $(this).closest(".panelButton").children();
                jQuery.each($buttonElements, function () {
                    if ($.browser.msie) {
                        $(this).css("background-position-y", "-60px");
                    }
                    else {
                        var position = $(this).css("background-position").replace("px -30px", "px -60px");
                        $(this).css("background-position", position);
                    }
                });
                $(this).blur();
            });
        });

        // this displays the overlay relative to the browser window
        showLightBox('CoTariffDetailsLightBox', $('.CoTariffDetailsLightBox').width());
        return false;
    });

    //Close
    $('.CoTariffDetailsLightBox .closeCoTariffLightBox, #COoverlay a').click(function () {
        $('.CoTariffDetailsLightBox').hide();
        $('#COoverlay').hide();

        return false;
    });

    //args - class of lightbox you want to display, width of content in pixels
    function showLightBox($lightBoxID) {
        $lightBoxID = '.' + $lightBoxID; //add a dot to the class name
        var $positionFromTop = 100; //pixels from the top of window or use percentage ($(window).height()/10)
        var $scrollposition = $(window).scrollTop() + $positionFromTop; //calculate how far down the lightbox should be	
        $($lightBoxID).css(
		{
		    top: $scrollposition
		}
		).show();
        $('#COoverlay').show();
    }

    refreshJS();
    setTimeout("refreshJS()", 500);
    if ($('.handsetResultsRow').length != 0) {
        gridLines();
    }

});

