var msie = false;
var J = jQuery.noConflict();

J(document).ready(function ($) {
	msie = $.browser.msie();

    bind_links_click($);

    $('.links').append('<iframe id="iframe" src="" frameborder="0"></iframe>');


	$(".media").bind("click", function(e){
		$(this).css('padding','0');
		var cnt = document.getElementById( $(this).attr('id') );
		var src = '../local/xaml/wmvplayer.xaml';
		var cfg = {
			file: $(this).children().attr('src'),
			height:'20',
			width:'481'
		};
		var ply = new jeroenwijering.Player(cnt,src,cfg);
		return false;
	});

		jQuery("#ask-the-expert").validate();

		jQuery("#contact-us").validate();

		jQuery("#contact-us1").validate();


	if(jQuery('.homepage .content_managed p').length>0){

	}else{
		if(jQuery('.content_managed p')){
		jQuery('.content_managed p').each(function(e){
			//console.log('dupa');
			var p_width = jQuery(e.target).width();
			if (p_width>480){
				jQuery(this).css("width", 480);
				}
			});
		jQuery('.full_width p').each(function(e){
			//console.log('dupa');
			var p_width = jQuery(e.target).width();
			if (p_width>480){
				jQuery(this).css("width", '100%');
				}
			});
		
		jQuery('.content_managed .generic p').css("width", '100%');
		}
	}

	jQuery("#make-an-enquiry").validate();

		jQuery('#news_rotator').cycle({
			    fx: 'fade',
				pager: '#news_pager',
				timeout: 10000,
				cleartype:  1
			});

		jQuery('#news_holder').cycle({
			fx: 'scrollLeft',
			timeout: 10000
		});




	if(jQuery("#question_list")){
			jQuery('.btn_toggle').toggle(function(e){
				e.preventDefault();
				jQuery(e.target).removeClass('toggle_down').addClass('toggle_up').parent().siblings('.answer-box').slideDown('fast');
			}, function(e){
				e.preventDefault();
				jQuery(e.target).removeClass('toggle_up').addClass('toggle_down').parent().siblings('.answer-box').slideUp('fast');
			});
		}

	if(jQuery("#question_list")){
		jQuery('.q_title').click(function(e){
			e.preventDefault();
			jQuery(e.target).parent().siblings('.btn_toggle').trigger('click');
			return false;
		});

	}

	jQuery('#sidenav li a').click(
		function(e){
			e.preventDefault();
			if (jQuery(this).siblings('ul').length > 0) {
				if (jQuery(this).parent().hasClass('selected')) {
					jQuery(this).siblings('ul').slideUp('fast').parent().removeClass('selected');
				} else {
					jQuery(this).siblings('ul').slideDown('fast').parent().addClass('selected');
				}
			} else {
				window.location.href = this.href;
			}
			return false;
		}
	);

	//open side nav items on services page
	jQuery('#services #sidenav li.hasChildren a.level1').each(
		function(e){
			jQuery(this).trigger('click');
		}
	);

	jQuery('#news_pager a').click(function (e)
	  {
		e.preventDefault();
		return false;
	  });

	//centering pager
	if (jQuery('ul#news_pager')) {
		var pagination_width = jQuery('ul#news_pager').width();
		if (pagination_width > 194){
			pagination_width = 194;
			jQuery('ul#news_pager').css("width", + pagination_width);

		}
		var margin = (194 - pagination_width)/2;
		jQuery('ul#news_pager').css("margin-left", + margin);
	}


	// Ajax Chat
	if (jQuery('.chat').length>0){
	jQuery('.chat').ajaxChat({
		messagesScroller: '.chat .scroller',
		updateInterval: 6000 // in miliseconds
	});
	}

	jQuery('a.captcha-getnew').click(function(e){
		e.preventDefault();
		var url = this.href + '?rand=' + Math.random().toString();
		jQuery('img.captcha_secure_image').get(0).src = url;
		return false;
	});

	jQuery('a.captcha-refetch').click(function(e){
		e.preventDefault();
		var url = this.href + '?rand=' + Math.random().toString();
		jQuery(this).parent().siblings('#Captcha_Image').get(0).src = url;
		return false;
	});

	// Check Flash plugin version
	if (typeof(pageRequiresFlash) != 'undefined' && pageRequiresFlash == true) {
	    if (FlashDetect.installed != true || FlashDetect.majorAtLeast(9) != true) {
			alert("Our website requires the Adobe Flash plugin version 9 or newer." +
			      "\n\nYou can download the plugin from http://get.adobe.com/flashplayer/");
		}
	}

});

function bind_links_click($){
	$(".links A").bind("click", function(e){
		if ($(this).attr("rel")) {

			$(".links A").unbind("click");
			$(".links").children('A').css('cursor','default');


			$('.popup').hide();

			var popupid = '#' + $(this).attr("rel");

			var pos = jQuery(this).position();

			$('#iframe').css('width', $(popupid).width() - 15);
			$('#iframe').css('height', $(popupid).height() - 40);

			$('#iframe').css('top', pos.top + 30);
			$('#iframe').css('left', (pos.left + $(this).width()) - $(popupid).width() + 15);

			$('#iframe').css('display','block');
			$('#iframe').css('z-index', 10);

			$(popupid).css('display','block');

			$(popupid).css('z-index', 20);

			$(popupid).children().children().children('.icon-close').bind("click", function(e){
				$(popupid).hide();
				$('#iframe').hide();
				$(".links A").css('cursor','pointer');
				bind_links_click($);
				return false;
			});
		}
		return false;
    });
}