$(document).ready(function() {  

  drawLang();

  inNewWindow();

  fontchange();  

  openModal('.opencontact',"#askQuestionForm",'#askQuestionForm .close a');

  doTopMenu($('#menuTop li>a[class=act]'));   

  //betTopMenu();  

  flyTo();  

  var ttime;

  if ($.browser.msie ) {

   if (parseInt($.browser.version, 10)==6)

   {

     DD_belatedPNG.fix('.fly_pad .fly_img2, #slogan a img,.block200 .b,#core li,#service .text,#estimate, .buttons, #service .b,#wrapperStatic,#cbottom,#stimul .b,#reason ul li,#ctop,#presentationBox .box .im img,#presentationBox .box .specmenu .bgR,#presentationBox .box,#prestige,#partners .text,#partners .b,.imoney,.block200 .text,#worksBox,.specmenu .bgL,.specmenu .bgR,#slideInner .im,.sign img,.ls_logo img,.portfolio_img .cornerRB,.portfolio_img .cornerLB,.portfolio_img .cornerR,.portfolio_img .cornerL,.portfolio_line,.port .ram,.fly_img,.specmenu .bg a,#prestige ul li img');

   }

  }

  slideIn('#slideInner',true);

  playflash();

  livechat();



	accord();

  $(function() {  

     $('.fly_pad').activeXOverlap();  

  }); 

	fixWidth();

	changePhones();

});



function detSel(selector) {

	$('#callus .text').each(function () {

		if ($(this).attr('id') == selector) {

			$('#callus .text').css({'position':'absolute','top':'0px','left':'0px'}).fadeOut(600);

			$(this).css({'position':'relative','top':'0px','left':'0px'}).fadeIn(800);

		}

	});

}



function changeChoice(el,ln) {

	var callustext = new Array();

	callustext['ru'] = {'lv': 'в Латвии', 'en': 'в Великобритании', 'se':'в США'};

	callustext['lv'] = {'lv': 'Latvijā', 'en': 'Lielbritānijā', 'se':'Zviedrijā'};

	callustext['en'] = {'us': 'in USA', 'en': 'in UK', 'se':'in Sweden'};

	callustext['se'] = {'us': 'i USA', 'en': 'i UK', 'se':'i Sverige'};

	

	var lang = $('#containerA').attr('class');

	

	$(el).html(callustext[lang][ln]);

	//$(el).html('test');

	$(el).attr('class', ln);



	return false;

}





function changePhones() {

	

	$('#callus .text').each(function() {

		var lang = $('#callus').attr('class');
		if ($(this).attr('id') == lang) {

			$(this).show();

		}

	});		



	$('.phones li a').click(function() {

		var selector = $(this).attr('class');

		var removeLang = $('#callus .text:visible').attr('id');

		//alert(removeLang);

		if (selector == 'showAll') {

			$('.popUp').fadeIn(700);

		} else {

			detSel(selector);

			changeChoice($(this),removeLang);

		}

		return false;

	});

	

	$('.popUp .close').click(function() {

		$('.popUp').fadeOut(600);

	});

	

	//$("body").click(function(e) {

		//if(e.target.className != 'popUp') {

			//$('.popUp').fadeOut();

		//} else {

			//alert('!');

			//return false;

		//}

	//});	

}



function fixWidth() {

	$('.button').each(function () {

		var width = $(this).children('.center').width();

		var width_to_fix = $('.shadow .centerShadow').width();

		var width_to_fix = $(this).children('.shadow').children('.centerShadow').width();

		if (width_to_fix < width) {

			$(this).children('.shadow').children('.centerShadow').css('min-width', width-72);

		}

	});



}



function accord() {

	$('.accord h2 span').click(

		function () {

			el = $(this).parents().eq(0);

			if (!$(el).hasClass('opened')) {

				$('.accord h2.opened').next().slideUp('fast');

				$('.accord h2.opened').removeClass('opened');

				$(el).addClass('opened');

				$(el).next().slideDown('fast');				

			}

			else {

				$(el).removeClass('opened');

				$(el).next().slideUp('fast');

			}

			return false;

		}

	);

}





function inNewWindow(){

  $('a[rel="nofollow"],a[rel="external"]').attr('target', '_blank'); 

}  



function flyHide(it){

      it.fadeOut(250); 

      it.next().hide();

	  it.prev('.act').children('span').remove(); 

    if (!it.prev('.act').hasClass('actalways')){

      it.prev('.act').removeClass('act');    

    }

      

	  //it.prev('.act').removeClass('act').children('span').remove();  

      //flyTo();  

}

function flyShow(it) {  

    it.next().fadeIn(0,function(){

    });

    it.next().next().fadeIn(0,function(){  

    });          

}    

          

function flyTo() {    

 var i2=0;    

 //$('.fly').prev(':not(.act)').mouseover(    

 $('.fly').prev('a').mouseover(     

  function(){ 

  

	$(this).addClass('act');         



    flyShow($(this));

    

    $(this).mouseleave( function(){    

      i=$(this).next();      

      i.oneTime(5, function() {   

        flyHide($(this));       

      });   

    });

               

    $(this).next().mouseleave( function(){   

      i2=$(this);      

      i2.oneTime(5, function() {

        flyHide($(this));       

      });        

    }); 



    $(this).next().next().mouseenter( function(){  

        i.stopTime(); 

    }); 

        

    $(this).next().mouseenter( function(){   

        i.stopTime();         

    });

                 

    if (i2!=0) {

     if ($('#menuTop>ul>li>a:.act').size()<2){   

       i2.stopTime();  

       i2=0;

     }

    }        



  }

 );

}







//Better Top Menu [add border left & right] bug in IE [NOT USED]

function betTopMenu(){

  if (!$.browser.msie) {

  $('#menuTop li a[class!=act]').hover(

  function(){  

    doTopMenu($(this));    

  }  

  ,

  function(){

    $('#menuTop li a[class!=act] span').remove();    

  }

  );     

  } 

}

//bug in IE [NOT USED]

function doTopMenu(make){

  make.prepend('<span class="l"></span>');

  make.append('<span class="r"></span>');   

} 



function drawLang(){

  $('#stlang').hide();

  var datasrc=Array();

  var datatitle=Array();

  var datahref=Array();  

  var nowselect;  

  $('#stlang dt').each(function(){

    datasrc.push($(this).children('img').attr('src'));  

  });

  $('#stlang dd').each(function(i){

    nowselect=$(this).has('a').length?nowselect:i;

    datahref[i]=$(this).children('a').attr('href');

    datatitle.push($(this).text());  

  });  

                  

  sel=' <div id="dynlang"> <img src="'+datasrc[nowselect]+'" alt=""><select name="lang">';

  for(var i=0; i<datasrc.length; i++) {

    sel+=nowselect==i?'<option value="'+datasrc[i]+'" selected="selected">'+datatitle[i]+'</option>':'<option value="'+datasrc[i]+'">'+datatitle[i]+'</option>';            

	}

  sel+='</select></div>';

                  

  //$('#callus .phone').after(sel);

  $('#callus .phones').after(sel);  



  $('#dynlang select').change(function(){

    $(location).attr('href',datahref[$('#dynlang select option:selected').index()]);

  });

} 



function slideIn(who,autoslide){

  var currentPosition = 0;

  var slideWidth = $(who+' > ul > li').width();

  var slides = $(who+' > ul > li');

  var numberOfSlides = slides.length;

  var staticOfSlides = numberOfSlides;  

  var numberOfSlidesminus =0;

  var margLeftUl=parseInt($(who).css('padding-left'));

  var slideInnerUl = slideWidth * numberOfSlides;

  var blockslider=0;

  $('#prestige #slideControl').show();

  

  // Float left to display horizontally, readjust .slides width

	$(who+' > ul').css({

      'float' : 'left',

      'left' : margLeftUl+'px',

      'position' : 'absolute',      

      'width' : slideWidth

    });



  // Set #slideInner width equal to total width of all slides

  $(who+' > ul').css('width', slideInnerUl);



  // Insert controls in the DOM

  $(who).parent()

    .prepend('<span class="control" id="arrL"></span>')

    .append('<span class="control" id="arrR"></span>');



        eqr=(currentPosition+1)-staticOfSlides;

        $(who+' > ul>li:eq('+eqr+')').clone().appendTo('#slideInner > ul');  

  // Hide left arrow control on first load

  //manageControls(currentPosition);

   

  // Create event listeners for .controls clicks

  $('.control')

    .bind('click', function(){

    if (blockslider==0){

    blockslider=1;

    

    if ($(this).attr('id')=='arrR') {

      currentPosition= currentPosition+1;

      if (numberOfSlides-numberOfSlidesminus<=currentPosition+1){

        eqr=(currentPosition+1)-staticOfSlides;

        //alert(eqr);

        $(who+' > ul>li:eq('+eqr+')').clone().appendTo('#slideInner > ul');  

        slideInnerUl=slideInnerUl+slideWidth;  

        $(who+' > ul').css('width',slideInnerUl);

        numberOfSlides++;  

        //alert($('.controlCenter .contr a.item').size());   

      }

        itemnow=$('.controlCenter .contr a.itemact').index();



        if ((itemnow+1)<$('.controlCenter .contr a.item').size())

        {

          $('.controlCenter .contr a.itemact').removeClass('itemact');

          $('.controlCenter .contr a.item').eq(itemnow+1).addClass('itemact');        

        }

        else

        {

          $('.controlCenter .contr a.itemact').removeClass('itemact');

          $('.controlCenter .contr a.item').eq(0).addClass('itemact');          

        }       

    }

    else

    {

      currentPosition=currentPosition-1;   

      if (0>=currentPosition+1){

         eqr=staticOfSlides+(currentPosition);

         $(who+' > ul > li:eq('+eqr+')').clone().prependTo('#slideInner > ul');

         slideInnerUl=slideInnerUl+slideWidth;                 

         $(who+' > ul').css('width',slideInnerUl); 

         $(who+' > ul').css('left', '-'+(-(margLeftUl)+slideWidth)+'px'); 

         currentPosition=0;               

         numberOfSlidesminus--;

      } 

        itemnow=$('.controlCenter .contr a.itemact').index();

        if ((itemnow+1)<0)

        {

          $('.controlCenter .contr a.itemact').removeClass('itemact');

          $('.controlCenter .contr a.item').eq($('.controlCenter .contr a.item').size()).addClass('itemact');        

        }

        else

        {

          $('.controlCenter .contr a.itemact').removeClass('itemact');

          $('.controlCenter .contr a.item').eq(itemnow-1).addClass('itemact');          

        }    

    }

     

    // Move slideInner

    $('#slideInner > ul').animate({

      'left' : margLeftUl+(slideWidth)*(-currentPosition)

    },800,function() {

       blockslider=0;

    }); 

    }     

  });         



  if (autoslide==true) {

    startAutoScroll();   

    bindAutoScroll();   

    var play=1;       

    if ($('#slideControl').length > 0) {

      $('#slideControl .play').addClass('playact');



      $('#slideControl .pause').live('click',function() {

        if ($(this).not('.pauseact')) {      

          $('#slideControl .pause').addClass('pauseact');

          $('#slideControl .play').removeClass('playact');

          stopAutoScroll();

          bindStopAutoScroll(); 

          play=0;

        }   

        return false;    

      });

      $('#slideControl .play').live('click',function() { 

        if ($(this).not('.playact')) { 

          if (play==0) {        

            $('#slideControl .play').addClass('playact');

            $('#slideControl .pause').removeClass('pauseact');

            startAutoScroll();  

            $('.scroller').live('mouseleave', function () {

              $('.scroller').die();              

              bindAutoScroll();   

            }); 

            play=1;

          }   

        }  

        return false;    

      });      

    }

  }

  // manageControls: Hides and Shows controls depending on currentPosition

  function manageControls(position){

    // Hide left arrow if position is first slide

	if(position==0){ $('#arrL').hide() } else{ $('#arrL').show() }

	// Hide right arrow if position is last slide

    if(position==numberOfSlides-1){ $('#arrR').hide() } else{ $('#arrR').show() }

  }	

        

}



function startAutoScroll() {

  $('#slideInner').everyTime(6000, 'autoscroll', function() {

     $('#arrR').click();

	}); 

}



function stopAutoScroll() {

  $('#slideInner').stopTime('autoscroll');

}



function bindAutoScroll(){  

    $('.scroller').live('mouseenter', function() {

        stopAutoScroll();

        $('#slideControl .play').removeClass('playact');        

    }

    ).live('mouseleave',function(){

        startAutoScroll();

        $('#slideControl .play').addClass('playact');        

    }

    );

}

function bindStopAutoScroll(){ 

    $('.scroller').die();

}

    

function fontchange(){

  if (os().name!='linux') {

    var detective = new Detector();

    if (detective.test('Calibri')){

      //make Calibri if find font Calibri

      $('.buttons').css('font','20px/31px Calibri')

      $('#header #callus .phone').css('font','32px/26px Calibri');

      $('#menuTop>ul>li>a').css('font','20px/50px Calibri'); 

      $('.flymenu a').css('font','20px/30px Calibri');     

      $('#prestige h3').css('font','30px/26px Calibri');

      $('#estimate h4').css('font','20px Calibri');

      $('#content h1').css('font','43px/40px Calibri');

      $('#presentation ul.tabs li').css('font','bold 14px Calibri');

      $('#reason h3').css('font','48px Calibri');

      $('.block200 .text').css('font','18px Calibri');

      $('#slideInner .text h3').css('font','30px/26px Calibri'); 

      $('#partners .text h4').css('font','20px Calibri');   



	  $('.button a').css('font','20px/31px Calibri');

	  $('.button .textShadow').css('font','20px/31px Calibri');

	  $('.button button').css('font','20px/31px Calibri');

    }   

    else

    {

      //init cufu 

    }

  }

} 



function openModal(where,what,close){

		$(where).click(function() {



      $(what).modal({overlayClose:true,opacity:35,overlayCss: {backgroundColor:"#aeaeae"},

      onOpen: function (dialog) {

      	dialog.overlay.fadeIn(1000);

		    dialog.data.hide();

		    dialog.container.fadeIn(1000);

		    dialog.data.fadeIn(1000);



        var pathname = window.location.pathname;

	      if (pathname == '/ru/') {

		      ln1 = 'ru';

        }

        else if (window.location.hostname=='www.bdcolors.com'){

 		      ln1 = 'en';       

        }

        else if (window.location.hostname=='www.bdcolors.lv'){

 		      ln1 = 'lv';       

        }



        _gaq.push(['_trackPageview', '/formfly/open'+ln1]);        

      

      },

      onClose: function (dialog) {

	      dialog.data.fadeOut('slow');

        dialog.container.fadeOut('slow');

  			dialog.overlay.fadeOut('slow', function () {

  				$.modal.close();

  			});

      }

      });

 

			return false;

		});

    

		$(close).click(function() {

       $.modal.close();

       return false;

    }); 

}



function livechat(){

 		$('.livechat').click(function() {

      if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 && window.event.preventDefault) {

       window.event.preventDefault();

      }

      this.newWindow = window.open($(this).attr('href')+'&amp;url='+escape(document.location.href)+'&amp;referrer='+escape(document.referrer), 'webim', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=740,height=850,resizable=1');

      this.newWindow.focus();

      this.newWindow.opener=window;

      return false;   

    });

}



function os(){

  $os = { name: (/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase()) || [u])[0].replace('sunos', 'solaris') };

  return $os;

}



function playflash(){  



		$('.miniplayer').flash(

			{

				swf: 'assets/flash/mini_player.swf?url_stream=assets/media/bdc.mp3&amp;auto_start=false',

				width: 26,

				height: 26,

				play: false,

				loop: true

			}

		);



  

  

  $('#slogan a[rel=external]').toggle(function() {

    $('.miniplayer').flash(

			{

				swf: 'assets/flash/mini_player.swf?url_stream=assets/media/bdc.mp3&amp;auto_start=true',

				width: 26,

				height: 26,

				play: true,

				loop: true

			}

		);

    $(this).children('img').css('margin-left','-152px');

    return false;  

  }, function() {

    $('.miniplayer').flash(

			{

				swf: 'assets/flash/mini_player.swf?url_stream=assets/media/bdc.mp3&amp;auto_start=false',

				width: 26,

				height: 26,

				play: false,

				loop: true

			}

		);

    $(this).children('img').css('margin-left','0px');    

    return false;  

  });

  

}



jQuery.fn.activeXOverlap = function() {   

     $(this).each(function(i){  

        //var h   = $(this).outerHeight();  

        //var w   = $(this).outerWidth();  

        var h = 270;

        var w = 400;  

        var iframe  = '<!--[if IE 6]>' +  

                         '<iframe src="javascript:false;" style="height: ' +  

                      h +  

                        'px; width: ' +  

                        w +  

                        'px" class="selectOverlap">' +  

                      '</iframe>' +  

                       '<![endif]-->'  

        $(this).prepend(iframe);  

     });  

}  
