/**
* Get marquee preferences and elements
* Displays marquee if activated in control panel and turned on
*/
(function($) {
	$.fn.htmlMarquee = function(displayType, config) {
		var dsMarquee = new DS.MarqueeService(s);
		var marquee = dsMarquee.getMarquee();
		var marqueePrefs = dsMarquee.getMarqueePreferences();

		var mText, mColor, mLink, mTarget;
		var mActive, mDisplay, mSpeed, mStyle;
		var mContainer = this;

		mActive = marqueePrefs.bIsActive;
		mDisplay = marqueePrefs.bDisplayOnSite;
		mSpeed = marqueePrefs.speed;
		mStyle = marqueePrefs.styleID;

		config = $.extend({
			fx:'fade',
			speed:1500,
			timeout:6000,
			sync:1,
			pause:1
		}, config);

		if (mActive){
			if (mDisplay){
				mWidth = $(mContainer).css('width');
				mWidth = mWidth.substring(0,mWidth.length-2);
				mHeight = $(mContainer).css('height');
				mHeight = mHeight.substring(0,mHeight.length-2);
				var cssObj = {
					'position' : 'relative',
					'overflow' : 'hidden'
				}
				$(mContainer).css(cssObj);
				switch(displayType){
					case "cycle":
						for (i in marquee){
							if (marquee[i].link == -1){
								$(mContainer).append('<div class="mTextSpan"><span style="color:#' + marquee[i].color.substr(2) + ';">' + marquee[i].text + '</span></div>');
							} else {
								$(mContainer).append('<div class="mTextSpan"><a style="color:#' + marquee[i].color.substr(2) + ';" href="' + marquee[i].link + '" target="' + marquee[i].target + '" title="' + marquee[i].text + '">' + marquee[i].text + '</a></div>');
							}
						}
						$(mContainer).cycle({fx:config.fx,speed:config.speed,timeout:config.timeout,sync:config.sync,pause:config.pause});
						break;
					case "typewriter":
						$.getScript("http://sitespecific.dealerskins.com/webassets/jquery/js/jquery.text-effects.js", function(){
							for (i in marquee){
								if (marquee[i].link == -1){
									$(mContainer).append('<div class="mTextSpan" style="color:#' + marquee[i].color.substr(2) + ';"><span>' + marquee[i].text + '</span></div>');
								} else {
									$(mContainer).append('<div class="mTextSpan"><a style="color:#' + marquee[i].color.substr(2) + ';" href="' + marquee[i].link + '" target="' + marquee[i].target + '" title="' + marquee[i].text + '"><span>' + marquee[i].text + '</span></a></div>');
								}
							}
							//$(mContainer).typewriter(); 
							$(mContainer).cycle({fx:'none',speed:config.speed,timeout:config.timeout,sync:config.sync,pause:config.pause,after:onAfter});
							function onAfter() { 
								toType = $(this).find("span");
								$(toType).typewriter(); 
							}
						});
						break;
					case "unscramble":
						$.getScript("http://sitespecific.dealerskins.com/webassets/jquery/js/jquery.text-effects.js", function(){
							for (i in marquee){
								if (marquee[i].link == -1){
									$(mContainer).append('<div class="mTextSpan" style="color:#' + marquee[i].color.substr(2) + ';"><span>' + marquee[i].text + '</span></div>');
								} else {
									$(mContainer).append('<div class="mTextSpan"><a style="color:#' + marquee[i].color.substr(2) + ';" href="' + marquee[i].link + '" target="' + marquee[i].target + '" title="' + marquee[i].text + '"><span>' + marquee[i].text + '</span></a></div>');
								}
							}
							//$(mContainer).typewriter(); 
							$(mContainer).cycle({fx:'none',speed:config.speed,timeout:config.timeout,sync:config.sync,pause:config.pause,after:onAfter});
							function onAfter() {
								toType = $(this).find("span");
								toType.unscramble(); 
							}
						});
						break;
					case "scrambledWriter":
						$.getScript("http://sitespecific.dealerskins.com/webassets/jquery/js/jquery.text-effects.js", function(){
							for (i in marquee){
								if (marquee[i].link == -1){
									$(mContainer).append('<div class="mTextSpan" style="color:#' + marquee[i].color.substr(2) + ';"><span>' + marquee[i].text + '</span></div>');
								} else {
									$(mContainer).append('<div class="mTextSpan"><a style="color:#' + marquee[i].color.substr(2) + ';" href="' + marquee[i].link + '" target="' + marquee[i].target + '" title="' + marquee[i].text + '"><span>' + marquee[i].text + '</span></a></div>');
								}
							}
							//$(mContainer).typewriter(); 
							$(mContainer).cycle({fx:'none',speed:config.speed,timeout:config.timeout,sync:config.sync,pause:config.pause,after:onAfter});
							function onAfter() { 
								toType = $(this).find("span");
								$(toType).scrambledWriter(); 
							}
						});
						break;
					default:
						$.getScript("http://sitespecific.dealerskins.com/webassets/jquery/js/jquery.marquee.js", function(){
							if (mHeight != 0) {
								$(mContainer).append('<marquee class="marqueeText" behavior="scroll" direction="left" scrollamount="' + mSpeed + '" width="' + mWidth + '" height="' + mHeight + '"></marquee>');
							} else {
								$(mContainer).append('<marquee class="marqueeText" behavior="scroll" direction="left" scrollamount="' + mSpeed + '" width="' + mWidth + '"></marquee>');
							}
							for (i in marquee){
								if (marquee[i].link == -1){
									$('.marqueeText').append('<span class="mTextSpan" style="color:#' + marquee[i].color.substr(2) + '; padding-right:300px; font-style:italic;">' + marquee[i].text + '</span>');
								} else {
									$('.marqueeText').append('<span class="mTextSpan"><a style="color:#' + marquee[i].color.substr(2) + '; padding-right:300px; font-style:italic;" href="' + marquee[i].link + '" target="' + marquee[i].target + '" title="' + marquee[i].text + '">' + marquee[i].text + '</a></span>');
								}
							}
							$('.marqueeText').marquee('marqueeElement').mouseover(function () {
									$(this).trigger('stop');
							}).mouseout(function () {
									$(this).trigger('start');
							});
						});						
				}
			} else {$(mContainer).css('display','none');}
		} else {$(mContainer).css('display','none');}

	};
})(jQuery);

