function showAnswer(qid) {
	if (document.getElementById(qid).style.display=='none') {
 	document.getElementById(qid).style.display='block';
	}
	else if (document.getElementById(qid).style.display=='block') {
	document.getElementById(qid).style.display='none';	
	}
}

function setValue(x) {
	var item = document.getElementById(x);
	if (item.value == "Keyword(s)")
	{
		item.value = "";
	}
}

function returnValue(x) {
	var item = document.getElementById(x);
	if (item.value == "")
	{
		item.value = "Keyword(s)";
	}
}
function clearDefault(x) {
	var item = document.getElementById(x);
	if (item.value == " -- ")
	{
		item.value = "";
	}
}
function setDefault(x) {
	var item = document.getElementById(x);
	if (item.value == "")
	{
		item.value = " -- ";
	}
}
function clearDate(x) {
	var item = document.getElementById(x);
	if (item.value == "dd/mm/yyyy")
	{
		item.value = "";
	}
}
function setDate(x) {
	var item = document.getElementById(x);
	if (item.value == "")
	{
		item.value = "dd/mm/yyyy";
	}
}
function addTrackingToLink(itemID, attr){
	document.getElementById(itemID).parentNode.onclick = function(){dcsMultiTrack('DCS.dcsuri', '/offers.aspx', attr, '1');};
}

$(document).ready(function() {


    $('.modalPopup').click(function() {

        var theImageURL = $(this).children().attr('src');

        var whichImage = theImageURL.substr(28, 2);

        switch (whichImage) {

            case "1.": case "1b": case "1c": case "1d":
                $('.modalPopupBox').hide();
                $("#modal_Fabia").show();
                break;

            case "2.":
                $('.modalPopupBox').hide();
                $("#modal_Roomster").show();
                break;

            case "3.":
                $('.modalPopupBox').hide();
                $("#modal_Yeti").show();
                break;

            case "4.": case "3b":
                $('.modalPopupBox').hide();
                $("#modal_Octavia").show();
                break;

            case "5.":
                $('.modalPopupBox').hide();
                $("#modal_Superb").show();
                break;
        }

        return false;

    });

    $('.closeBtn').click(function() {
        $('.modalPopupBox').hide();
        return false;
    });


    //Fabia
    $('.modalPopup_Fabia').click(function() {
        $('#modal_Fabia').show();
        return false;
    });

    //Roomster
    $('.modalPopup_Roomster').click(function() {
        $('#modal_Roomster').show();
        return false;
    });

    //Octavia
    $('.modalPopup_Octavia').click(function() {
        $('#modal_Octavia').show();
        return false;
    });

    //Superb
    $('.modalPopup_Superb').click(function() {
        $('#modal_Superb').show();
        return false;
    });

    //Yeti
    $('.modalPopup_Yeti').click(function() {
        $('#modal_Yeti').show();
        return false;
    });

    $('.closeBtn_Fabia').click(function() {
        $('#modal_Fabia').hide();
        return false;
    });

    $('.closeBtn_Roomster').click(function() {
        $('#modal_Roomster').hide();
        return false;
    });

    $('.closeBtn_Octavia').click(function() {
        $('#modal_Octavia').hide();
        return false;
    });

    $('.closeBtn_Superb').click(function() {
        $('#modal_Superb').hide();
        return false;
    });

    $('.closeBtn_Yeti').click(function() {
        $('#modal_Yeti').hide();
        return false;
    });


    // The following gives a grey zebra stripe to the <li> in the modalPopup

    $('#modal_Fabia .modalLeft ul li:odd, #modal_Fabia .modalRight ul li:odd').css('backgroundColor', '#eee');

    $('#modal_Roomster .modalLeft ul li:odd, #modal_Roomster .modalRight ul li:odd').css('backgroundColor', '#eee');

    $('#modal_Superb .modalLeft ul li:odd, #modal_Superb .modalRight ul li:odd').css('backgroundColor', '#eee');

    $('#modal_Octavia .modalLeft ul li:odd, #modal_Octavia .modalRight ul li:odd').css('backgroundColor', '#eee');

    $('#modal_Yeti .modalLeft ul li:odd, #modal_Yeti .modalRight ul li:odd').css('backgroundColor', '#eee');

    $('#thumb1').click(function() {
        $('#enlarged1').removeClass('hide');
        $('#enlarged1').show();
        $('#enlarged2').hide();
        $('#enlarged3').hide();
        $('#enlarged4').hide();
        $('#enlarged5').hide();
        $('#enlarged6').hide();
    })
    $('#thumb2').click(function() {
        $('#enlarged2').removeClass('hide');
        $('#enlarged2').show();
        $('#enlarged1').hide();
        $('#enlarged3').hide();
        $('#enlarged4').hide();
        $('#enlarged5').hide();
        $('#enlarged6').hide();

    })
    $('#thumb3').click(function() {
        $('#enlarged3').removeClass('hide');
        $('#enlarged3').show();
        $('#enlarged1').hide();
        $('#enlarged2').hide();
        $('#enlarged4').hide();
        $('#enlarged5').hide();
        $('#enlarged6').hide();

    })
    $('#thumb4').click(function() {
        $('#enlarged4').removeClass('hide');
        $('#enlarged4').show();
        $('#enlarged1').hide();
        $('#enlarged2').hide();
        $('#enlarged3').hide();
        $('#enlarged5').hide();
        $('#enlarged6').hide();

    })
    $('#thumb5').click(function() {
        $('#enlarged5').removeClass('hide');
        $('#enlarged5').show();
        $('#enlarged1').hide();
        $('#enlarged2').hide();
        $('#enlarged3').hide();
        $('#enlarged4').hide();
        $('#enlarged6').hide();

    })
    $('#thumb6').click(function() {
        $('#enlarged6').removeClass('hide');
        $('#enlarged6').show();
        $('#enlarged1').hide();
        $('#enlarged2').hide();
        $('#enlarged3').hide();
        $('#enlarged4').hide();
        $('#enlarged5').hide();

    })

    //hide content of noscript tag from opera when javascript is switched on

    $("#contentBoxXtraLarge .mid .inside .yetiContent fieldset.registerYeti input.noscriptTag").addClass("hide");

    //Add indicator for selectable item in fleet compare
    var selectText = [];
    $('.selectGroup select:not(:disabled) :selected').each(function() {
        selectText = $(this).text();
    });
    $('.selectGroup select:not(:disabled) :selected').each(function() {
        if ($(this).text() == selectText) {
            $(this).parents('span').css('border', '2px solid #129f76').animate({ dummy: 1 }, 500).animate({ borderTopColor: '#fff', borderRightColor: '#fff', borderBottomColor: '#fff', borderLeftColor: '#fff' }, 3000);
        }
    });

});


$(document).ready( function(){
	$(".carPanel").prepend("<ul class='pagination paginationList'></ul>")
	if ($(".carPanel").attr("class") != "carPanel detailPage") {
		
		$(".carPanel h2").after("<ul class='offerList Searchresult'></ul>")
	}


	$("#botRow .detailPage table tr:nth-child(even)").addClass("even");
	
	if($(".carPanel:not(#CarPanelYeti)").attr("class") == "carPanel detailPage"){
			$(".heroItem").hide();
		}
	if ($(".yetiStaticOffer").attr("class") != null) {
	
	}
	else {
		$("#botRow .carPanel .hiddenresult").hide()
       // When the HTML has loaded, call initPagination to paginate the elements        
        if ($("#botRow .carPanel").length > 0) {
			$("#botRow .carPanel").each(function(intIndex){ 

       			initPagination($(this));
			})
		}  
	}
	
	})
            /**
             * Callback function that displays the content.
             *
             * Gets called every time the user clicks on a pagination link.
             *
             * @param {int}page_index New Page index
             * @param {jQuery} jq the container with the pagination links as a jQuery object
             */
			
			function pageselectCallback(page_index, jq){
				var cp = $(jq).parents(".carPanel");
                var new_content = $('.hiddenresult li.result:eq('+page_index+')',cp).clone();
                $('.Searchresult',cp).empty().append(new_content);
                return false;
            }
           
            /** 
             * Callback function for the AJAX content loader.
             */
            function initPagination(elem) {
			//need to add testing for detail page to ensure correct pagination highlight
			var currentPageIndex = getPageIndex();
               var num_entries = elem.find('.hiddenresult li.result').size();
			   var elemPagination =  elem.find('.paginationList')
                elemPagination.pagination(num_entries, {
                    num_edge_entries: 0,
                    num_display_entries: 6,
                    callback: pageselectCallback,
                    items_per_page:1, 
					current_page:currentPageIndex
                });
				
             }
                    
             function getPageIndex(){
			 	var highlightedPageIndex = 0;
				if ($(".detailPage").size() > 0) {
					var locationId = location.href.substring(location.href.lastIndexOf("#") + 1);
					var numItems = $('#offers .hiddenresult li.result').size();
					for (var i = 0; i < numItems; i++) {
						if ($('#offers .hiddenresult li.result:eq(' + i + ')').attr('id') == locationId) {
							highlightedPageIndex = i;
							break;
						}
					}
				}
				return highlightedPageIndex;
			 }


	
	function OpenMapPopup(url)
	{
		var popupWindow = window.open(url,'RetailerMapPopup','width=460,height=510,toolbar=0,status=0,menubar=0,location=0');
		if (window.focus) { popupWindow.focus(); }
		return false;
	}

	/**
	* jQuery.ScrollTo
	* Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
	* Dual licensed under MIT and GPL.
	* Date: 5/25/2009
	*
	* @projectDescription Easy element scrolling using jQuery.
	* http://flesler.blogspot.com/2007/10/jqueryscrollto.html
	* Works with jQuery +1.2.6. Tested on FF 2/3, IE 6/7/8, Opera 9.5/6, Safari 3, Chrome 1 on WinXP.
	*
	* @author Ariel Flesler
	* @version 1.4.2
	*
	*/
	; (function($) {

	    var $scrollTo = $.scrollTo = function(target, duration, settings) {
	        $(window).scrollTo(target, duration, settings);
	    };

	    $scrollTo.defaults = {
	        axis: 'xy',
	        duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1
	    };

	    // Returns the element that needs to be animated to scroll the window.
	    // Kept for backwards compatibility (specially for localScroll & serialScroll)
	    $scrollTo.window = function(scope) {
	        return $(window)._scrollable();
	    };

	    // Hack, hack, hack :)
	    // Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
	    $.fn._scrollable = function() {
	        return this.map(function() {
	            var elem = this,
				isWin = !elem.nodeName || $.inArray(elem.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) != -1;

	            if (!isWin)
	                return elem;

	            var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;

	            return $.browser.safari || doc.compatMode == 'BackCompat' ?
				doc.body :
				doc.documentElement;
	        });
	    };

	    $.fn.scrollTo = function(target, duration, settings) {
	        if (typeof duration == 'object') {
	            settings = duration;
	            duration = 0;
	        }
	        if (typeof settings == 'function')
	            settings = { onAfter: settings };

	        if (target == 'max')
	            target = 9e9;

	        settings = $.extend({}, $scrollTo.defaults, settings);
	        // Speed is still recognized for backwards compatibility
	        duration = duration || settings.speed || settings.duration;
	        // Make sure the settings are given right
	        settings.queue = settings.queue && settings.axis.length > 1;

	        if (settings.queue)
	        // Let's keep the overall duration
	            duration /= 2;
	        settings.offset = both(settings.offset);
	        settings.over = both(settings.over);

	        return this._scrollable().each(function() {
	            var elem = this,
				$elem = $(elem),
				targ = target, toff, attr = {},
				win = $elem.is('html,body');

	            switch (typeof targ) {
	                // A number will pass the regex 
	                case 'number':
	                case 'string':
	                    if (/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(targ)) {
	                        targ = both(targ);
	                        // We are done
	                        break;
	                    }
	                    // Relative selector, no break!
	                    targ = $(targ, this);
	                case 'object':
	                    // DOMElement / jQuery
	                    if (targ.is || targ.style)
	                    // Get the real position of the target 
	                        toff = (targ = $(targ)).offset();
	            }
	            $.each(settings.axis.split(''), function(i, axis) {
	                var Pos = axis == 'x' ? 'Left' : 'Top',
					pos = Pos.toLowerCase(),
					key = 'scroll' + Pos,
					old = elem[key],
					max = $scrollTo.max(elem, axis);

	                if (toff) {// jQuery / DOMElement
	                    attr[key] = toff[pos] + (win ? 0 : old - $elem.offset()[pos]);

	                    // If it's a dom element, reduce the margin
	                    if (settings.margin) {
	                        attr[key] -= parseInt(targ.css('margin' + Pos)) || 0;
	                        attr[key] -= parseInt(targ.css('border' + Pos + 'Width')) || 0;
	                    }

	                    attr[key] += settings.offset[pos] || 0;

	                    if (settings.over[pos])
	                    // Scroll to a fraction of its width/height
	                        attr[key] += targ[axis == 'x' ? 'width' : 'height']() * settings.over[pos];
	                } else {
	                    var val = targ[pos];
	                    // Handle percentage values
	                    attr[key] = val.slice && val.slice(-1) == '%' ?
						parseFloat(val) / 100 * max
						: val;
	                }

	                // Number or 'number'
	                if (/^\d+$/.test(attr[key]))
	                // Check the limits
	                    attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max);

	                // Queueing axes
	                if (!i && settings.queue) {
	                    // Don't waste time animating, if there's no need.
	                    if (old != attr[key])
	                    // Intermediate animation
	                        animate(settings.onAfterFirst);
	                    // Don't animate this axis again in the next iteration.
	                    delete attr[key];
	                }
	            });

	            animate(settings.onAfter);

	            function animate(callback) {
	                $elem.animate(attr, duration, settings.easing, callback && function() {
	                    callback.call(this, target, settings);
	                });
	            };

	        }).end();
	    };

	    // Max scrolling position, works on quirks mode
	    // It only fails (not too badly) on IE, quirks mode.
	    $scrollTo.max = function(elem, axis) {
	        var Dim = axis == 'x' ? 'Width' : 'Height',
			scroll = 'scroll' + Dim;

	        if (!$(elem).is('html,body'))
	            return elem[scroll] - $(elem)[Dim.toLowerCase()]();

	        var size = 'client' + Dim,
			html = elem.ownerDocument.documentElement,
			body = elem.ownerDocument.body;

	        return Math.max(html[scroll], body[scroll])
			 - Math.min(html[size], body[size]);

	    };

	    function both(val) {
	        return typeof val == 'object' ? val : { top: val, left: val };
	    };

	})(jQuery);
