﻿$(document).ready(function() {

if (!($('body').hasClass('page-template-promo-page-php'))) {
$('#content').prepend('<div id="promowrap" class="inside"><div id="layer1" class="layer layer1" style="background-position: 10px -30px;"></div><div id="layer2" class="layer layer2" style="background-position: 0 -30px;"></div><div id="layer3" id="layer3" class="layer layer3" style="background-position: 0 0;"></div><div id="promoctrls"><div id="promoctrll" class="promoctrl">&#9668;</div><div id="promoctrlm" class="promoctrl">&#9616;&#9616;</div><div id="promoctrlr" class="promoctrl">&#9658;</div></div></div>');
}

else {
$('#promowrap').prepend('<div id="layer1" class="layer layer1" style="background-position: 10px 120px;"></div><div id="layer2" class="layer layer2" style="background-position: 0 20px;"></div><div id="layer3" class="layer layer3" style="background-position: 0 250px;"></div><div id="promoctrls"><div id="promoctrll" class="promoctrl">&#9668;</div><div id="promoctrlm" class="promoctrl">&#9616;&#9616;</div><div id="promoctrlr" class="promoctrl">&#9658;</div></div>');
}

if ($('body').hasClass('page')) {
$('#reply-title').html('Get a Quote');
$('.comment-form-comment label').html('Send us your requirements');
$('.form-allowed-tags').hide();
$('.form-submit #submit').val('Send');
$('#commentform').hide();
$('#reply-title').click(function(){
$('#commentform').show();
});
}

var intervalID1;
var intervalID2;
var intervalID3;

$('#promowrap h2').wrap('<div class="promoheaderwrap" />');

$('.promoheaderwrap').each(function(){
var $set = $();
var nxt = this.nextSibling;
while(nxt) {if(!$(nxt).is(".promoheaderwrap")) {$set.push(nxt);nxt = nxt.nextSibling;} else break;}
$set.wrapAll('<div class="promocontentwrap" />');
});

$('.promoheaderwrap').each(function(){
var $set = $(this);
var nxt = this.nextSibling;
while(nxt) {
if(!$(nxt).is(".promoheaderwrap")) {$set.push(nxt);nxt = nxt.nextSibling;} else break;}
$set.wrapAll('<div class="promosectionwrap" />');
});

$(".promoheaderwrap").css('left', $("#promowrap").width());
$(".promoheaderwrap").addClass("static");
$(".promoheaderwrap").prepend("<div class=\"close\">&times;</div>");
$(".promoheaderwrap a").removeAttr("title");
$(".promocontentwrap").addClass("hidden");
$(".layer").addClass("animated").addClass("left");

var bgposX = parseInt($(".layer1").css("background-position-x"), 10);

if (bgposX == 10){
intervalID1 = setInterval(function (){
$('.promoheaderwrap.animated').css('left', "-=1px");
if ($(".layer1.animated").hasClass('left')){
var layer1bgPosX = parseInt(document.getElementById('layer1').style.backgroundPositionX);
var layer2bgPosX = parseInt(document.getElementById('layer2').style.backgroundPositionX);
var layer3bgPosX = parseInt(document.getElementById('layer3').style.backgroundPositionX);
layer1bgPosX++;
layer2bgPosX+=2;
layer3bgPosX+=3;
$(".layer1.animated.left").css("background-position-x", layer1bgPosX);
$(".layer2.animated.left").css("background-position-x", layer2bgPosX);
$(".layer3.animated.left").css("background-position-x", layer3bgPosX);
}
else if ($(".layer1.animated").hasClass('right')){
var layer1bgPosX = parseInt(document.getElementById('layer1').style.backgroundPositionX);
var layer2bgPosX = parseInt(document.getElementById('layer2').style.backgroundPositionX);
var layer3bgPosX = parseInt(document.getElementById('layer3').style.backgroundPositionX);
layer1bgPosX--;
layer2bgPosX-=2;
layer3bgPosX-=3;
$(".layer1.animated.right").css("background-position-x", layer1bgPosX);
$(".layer2.animated.right").css("background-position-x", layer2bgPosX);
$(".layer3.animated.right").css("background-position-x", layer3bgPosX);
}
}, 65);
}

else {
intervalID1 = setInterval(function (){
$('.promoheaderwrap.animated').css('left', "-=1px");
$(".layer1.animated.left").stop().animate({backgroundPosition: '+=1px +=0px'}, 0, "linear");
$(".layer2.animated.left").stop().animate({backgroundPosition: '+=2px +=0px'}, 0, "linear");
$(".layer3.animated.left").stop().animate({backgroundPosition: '+=3px +=0px'}, 0, "linear");
$(".layer1.animated.right").stop().animate({backgroundPosition: '-=1px +=0px'}, 0, "linear");
$(".layer2.animated.right").stop().animate({backgroundPosition: '-=2px +=0px'}, 0, "linear");
$(".layer3.animated.right").stop().animate({backgroundPosition: '-=3px +=0px'}, 0, "linear");
}, 65);
}

var headerWidthx = 0;
var numHeaders = $('.promoheaderwrap.static').length;
var promoSpace = 0;
var headerSpace = 0;

$('.promoheaderwrap.static').each(function(){
headerWidthx+=$(this).outerWidth(true);
});

if ($("#promowrap").width() > headerWidthx){
promoSpace = $("#promowrap").width()-headerWidthx;
headerSpace = Math.round(promoSpace/numHeaders);
}

var headerWidthx = 0;
var headerSpacex = headerSpace;

intervalID2 = setInterval(function () {
	
if ($(".promoheaderwrap").hasClass("static")) {
randomHeader = jQuery(".promoheaderwrap.static").get().sort(function(){ 
return Math.round(Math.random())-0.5
}).slice(0, 1);

var headerWidth = $(randomHeader).outerWidth(true);
var number = 1 + Math.floor(Math.random() * 50);

$(randomHeader).hide();
$(randomHeader).removeClass("static");
$(randomHeader).css('top', number);
$(randomHeader).css('left',headerWidthx+headerSpacex+"px");
$(randomHeader).css('z-index', number);
$(randomHeader).addClass("animated");
$(randomHeader).delay(10*number).fadeIn();

headerWidthx+=headerWidth;
headerSpacex+=headerSpace;

}

else {clearInterval(intervalID2);}

}, 65);

var thisLeft = 0;
var thisWidth = 0;
var prevWidth = 0;
var animStart = true;

intervalID3 = setInterval(function () {
$('.promoheaderwrap.animated').each(function(){
if (animStart == true) {
if (parseInt($(this).css("left"), 10) <= -$(this).outerWidth(true)){
animStart = false;
thisWidth = $(this).outerWidth(true);
$(this).removeClass("animated");
if ($("#promowrap").width() > headerWidthx ) {
$(this).css('left', $("#promowrap").width());}
else {$(this).css('left', headerWidthx);}
var number = 1 + Math.floor(Math.random() * 50);
$(this).css('top', number);
$(this).css('z-index', number);
$(this).addClass("animated");
setTimeout(function(){animStart = true;}, thisWidth*65);}}})
}, 65);

$('.promosectionwrap').hover(function () {
$('.promosectionwrap').removeClass('hover');
$('.promoheaderwrap').removeClass('hover').addClass('animated');
$('.promocontentwrap').removeClass('visible').addClass('hidden');
var promoWidth = $("#promowrap").width();
contentTop = parseInt($('.promoheaderwrap',this).css("top"), 10)+$('.promoheaderwrap',this).outerHeight(true)-2;
var headerLeft = parseInt($('.promoheaderwrap',this).css("left"), 10);
if (headerLeft < 0){contentLeft = -1;}
else if (headerLeft > promoWidth/2 && promoWidth < 1024){contentLeft = promoWidth/2;}
else if (headerLeft > promoWidth/4*3){contentLeft = promoWidth/4*3;}
else{contentLeft = headerLeft;}
if (promoWidth < 1024){$('.promocontentwrap',this).css('max-width', "50%");}
else {$('.promocontentwrap',this).css('max-width', "25%");}
if (headerLeft > promoWidth-$('.promoheaderwrap',this).outerWidth(true)-10){$('.promocontentwrap',this).css('right', -1);}
else {$('.promocontentwrap',this).css('right', 10);}
$(this).addClass('hover');
$('.promoheaderwrap',this).removeClass('animated').addClass('hover');
$('.promocontentwrap',this).css('top', contentTop).css('left', contentLeft).removeClass('hidden').addClass('visible');

}, function () {
$('.promosectionwrap').removeClass('hover');
$('.promoheaderwrap').removeClass('hover').addClass('animated');
$('.promocontentwrap').removeClass('visible').addClass('hidden');
});

$(".close").click(function(){
$('.promosectionwrap').removeClass('hover');
$('.promoheaderwrap').removeClass('hover').addClass('animated');
$('.promocontentwrap').removeClass('visible').addClass('hidden');
});

$("#promoctrll").click(function(){$(".layer").removeClass("right");$(".layer").addClass("animated");$(".layer").addClass("left");});
$("#promoctrlm").click(function(){$(".layer").removeClass("animated");});
$("#promoctrlr").click(function(){$(".layer").removeClass("left");$(".layer").addClass("animated");$(".layer").addClass("right");});

});
