// common javascript functions

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

$(document).ready(function() {

    // fix IE 6 background image caching problem
    if ($.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    if (objFlash = $('div.flash-home')) {
        flash(objFlash, root + 'Content/images/flash/home.swf', 777, 320);
    }

    if (objForm = $('form.client-login')) {
        clientlogin(objForm, 'a.client-login', 'Hide Login', 'Client Login');
    }
	
	$("#languages").change(function() 
    { 
        message_index = $("#languages").val(); 
 		window.location = message_index;
    });

    

});


if (objRandImage = $('#random')) {
        var strBodyClass = $('body').attr("class").rtrim();
        var arrRandImages = ['one','two'];
        if ($('body').hasClass('affinity')) {
            arrRandImages = [strBodyClass + '_one'];
        }
        if ($('body').hasClass('contact-us')) {
            arrRandImages = ['contact-us'];
        }
        if ($('body').hasClass('motor-dealers') || $('body').hasClass('motor-retailers')) {
            arrRandImages = [strBodyClass + '_one', strBodyClass + '_two'];
        }
        if ($('body').hasClass('manufacturers')) {
            arrRandImages = [strBodyClass + '_one'];
        }
        if ($('body').hasClass('insurance-intermediaries')) {
            arrRandImages = [strBodyClass + '_one'];
        }
        if ($('body').hasClass('affinity-banking--finance')) {
            arrRandImages = [strBodyClass + '_one'];
        }
        if ($('body').hasClass('worldwide')) {
            arrRandImages = [strBodyClass + '_one', strBodyClass + '_two'];
        }
        if ($('body').hasClass('wide') || $('body').hasClass('news') || $('body').hasClass('press-office')) {
            arrRandImages = [strBodyClass + '_contact'];
            objRandImage.css('background-image', 'url(' + root + 'Content/images/random/contact.jpg)');
        } else {
            $('body').removeClass('wide');
            strBodyClass = $('body').attr('class');
            var strNum = Math.floor(Math.random() * arrRandImages.length);
            objRandImage.css('background-image', 'url(' + root + 'Content/images/random/' + arrRandImages[strNum] + '.jpg)');
        }
    }
	
function flash(objFlash, strFlashSrc, strFlashW, strFlashH) {
	objFlash.children('.flash-hide').hide();
	objFlash.flash({
		  src: strFlashSrc,
		  width: strFlashW,
		  height: strFlashH,
		  wmode: 'transparent'
		},
		{ version: 9 }
	);	
}

function clientlogin(objForm, strFormLink, strHide, strShow) {
	/*
	objForm.hide();
	$(strFormLink).toggle(
		function () {
			$(this).addClass('on');
			$(this).text(strHide);
			objForm.slideDown('medium');
			return false;
		},
		function () {
			$(this).removeClass('on');
			$(this).text(strShow);
			objForm.slideUp('medium');
			return false;
		}
	);	
	*/
}

$(function() {

    //new rotateElements().init('.client-image-rotator ul', 5000, 'li', true, 'slow');
    // container class, rotate delay, element to rotate, autoplay, fade speed
    $(".client-image-rotator ul").cycle({ delay: 2000 });
});
