$( function() {
	
	$('a.click_ticker').click( function( e ) {
		e.preventDefault();
		if( $( 'div.signin_content' ).length > 0 ) {
			$( 'div.signin_content' ).lightbox();
		} else {
			$('#ticker').lightbox();
			
		}
	});	
	$("ul#ticker01").liScroll({travelocity: .1});

	$( 'div.socialized a' ).attr( 'target', '_blank' ).each( function( i ) {
		switch( i ) {
			case 0 : {
				$( this ).attr( 'title', 'BB PIN: 21D650C4' ).attr( 'href', 'javascript:void(0);' ).attr( 'target', '' );
				break;
			} case 1 : {
				$( this ).attr( 'href', 'http://www.twitter.com/utvworldmovies/' );
				break;
			} case 2 : {
				$( this ).attr( 'href', 'http://www.facebook.com/utvworldmovies/' );
				break;
			} case 3 : {
				$( this ).attr( 'href', 'http://www.youtube.com/utvworldmovies/' );
				break;
			}
		}
	});

	$.fn.wmFade = function( options ) {
		var settings = {
			autoplay : true,
			transitionSpeed : 'slow'
		};

		for( var eachProperty in options ) {
			if( typeof settings[ eachProperty ] != 'undefined' ) {
				settings[ eachProperty ] = options[ eachProperty ];
			}
		}

		return this.each( function( i ) {
			var $mainContainer = $( this ), $mainLi = $( 'li', $mainContainer );
			
			$mainLi.hide().eq( 0 ).show();
			$mainContainer.after( "<div class='stripTransmitter' id='stripTransmitter" + ( i ) + "'><ul><\/ul><\/div>" );
			$mainLi.each( function( n ) {
				if( n == 0 ) {
					$( "#stripTransmitter" + i + " ul" ).append( '<li><a class="current" title="' + $( 'img', this ).attr( "alt" ) + '" href="#">' + ( n + 1 ) + "<\/a><\/li>" );
				} else {
					$( "#stripTransmitter" + i + " ul" ).append( "<li><a title='" + $( 'img', this ).attr( "alt" ) + "' href='#'>" + ( n + 1 ) + "<\/a><\/li>" );
				}
			});

			$( "#stripTransmitter" + i + " a" ).click( function() {
				var $currentAnchor = $( this );
				$mainLi.eq( $( "#stripTransmitter" + i + ' a.current' ).parent().prevAll().length ).fadeOut( settings.transitionSpeed, function() {
					$mainLi.eq( $currentAnchor.parent().prevAll().length ).fadeIn( settings.transitionSpeed );
					$currentAnchor.addClass( "current" ).parent().parent().find( "a" ).not( $currentAnchor ).removeClass( "current" );
				});
				return false;
			});

			if( settings.autoplay ){
				setInterval( function() {
					var $currentActive = $( "#stripTransmitter" + i + ' a.current' );
					if( $currentActive.parent().next().length > 0 ) {
						$currentActive.parent().next().find( 'a' ).trigger( 'click' );
					} else {
						$currentActive.parent().siblings().eq(0).find( 'a' ).trigger( 'click' );
					}
				}, 3000 );
			}
		});
	};

	window.getYoutubeVideo = function( videoId, noLightbox, width, height ) {
		noLightbox = typeof noLightbox == 'undefined' ? false : noLightbox;
		width = typeof width != 'undefined' ? width : 480;
		height = typeof height != 'undefined' ? height : 385;
		var youtubeEmbedCode = '<div class="lb_overlay none"> \
									<div class="lightbox clearfix"> \
										<div class="cross clearfix"><a href="javascript:void(0);" class="closeLightbox cancel">X</a></div> \
										<div class="lightbox_bg"> \
											<div class="grey_bg"> \
												<object width="' + width + '" height="' + height + '"> \
													<param name="movie" value="http://www.youtube.com/v/${videoId}&hl=en_US&fs=1&"></param> \
													<param name="allowFullScreen" value="true"></param> \
													<param name="allowscriptaccess" value="always"></param> \
													<embed src="http://www.youtube.com/v/${videoId}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed> \
												</object> \
											</div> \
										</div> \
									</div> \
								</div>', onlyCode = '<object width="' + width + '" height="' + height + '"> \
														<param name="movie" value="http://www.youtube.com/v/${videoId}&hl=en_US&fs=1&"></param> \
														<param name="allowFullScreen" value="true"></param> \
														<param name="allowscriptaccess" value="always"></param> \
														<embed src="http://www.youtube.com/v/${videoId}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed> \
													</object>';
		youtubeEmbedCode = noLightbox ? onlyCode.replace( '${videoId}', videoId ).replace( '${videoId}', videoId ) : youtubeEmbedCode.replace( '${videoId}', videoId ).replace( '${videoId}', videoId );
		return youtubeEmbedCode;
	}

	$.fn.center = function () {
		if( !window.centerElements ) {
			window.centerElements = [];
		}
		for( var i = 0; i < this.length; i ++ ) {
			window.centerElements.push( this[ i ] );
		}

		var centerlize = function() {
			$( window.centerElements ).each( function() {
				var $this = $( this );
				$this.css( { position : 'absolute' } ).css( {
					top : ( ( $( window ).height() - $this.height() ) / 3 ) + $( window ).scrollTop() + "px",
					left : ( ( $( window ).width() - $this.width() ) / 2 ) + $( window ).scrollLeft() + "px"
				});
			});
		};

		$( window ).bind( 'resize', centerlize );
		setTimeout( function() {
			$( window ).trigger( 'resize' );
		}, 100 );
		return this;
	};

	$.fn.lightbox = function( options ) {
		var defaults = { backColor : '#999', opacity : .5 };
		for( var eachProp in options ) {
			if( defaults[ eachProp ] ) {
				defaults[ eachProp ] = options[ eachProp ];
			}
		}
		defaults.alphaFilter = defaults.opacity * 100;
		$( '<div style="position:absolute;width:100%;height:' + $( document ).height() + 'px;left:0px;top:0px;z-index:1000;background-color:' + defaults.backColor + ';opacity:' + defaults.opacity + ';filter: alpha(opacity = ' + defaults.alphaFilter + ');" id="lightbox-overlay"></div>' ).appendTo( 'body' ).click( function() {
			$( document ).trigger( 'closeLightbox' );
		});
		$( this ).css( 'z-index', 1001 ).show().center();
		$( 'a.closeLightbox' ).click( function( e ) {
			e.preventDefault();
			$( document ).trigger( 'closeLightbox' );
		});
		var $selector = $( this );
		$( document ).unbind( 'closeLightbox' ).bind( 'closeLightbox', function() {
			$( '#lightbox-overlay' ).remove();
			$selector.hide();
		});
	};

	$( 'a[name=signupAnchor]' ).click( function( e ) {
		e.preventDefault();
		if( $( 'div.registration_content' ).length > 0 ) {
			$( 'div.registration_content' ).lightbox();
		} else {
			alert( 'You\'ve already logged in, no need to register again!' );
		}
	});

	$( 'a.not_registered' ).click( function() {
		$( document ).trigger( 'closeLightbox' );
		if( $( 'div.registration_content' ).length > 0 ) {
			$( 'div.registration_content' ).lightbox();
		} else {
			alert( 'You\'ve already logged in, no need to register again!' );
		}
	});

	$( 'a.already_registered' ).click( function() {
		$( document ).trigger( 'closeLightbox' );
		if( $( 'div.signin_content' ).length > 0 ) {
			$( 'div.signin_content' ).lightbox();
		} else {
			alert( 'You\'ve already logged in!' );
		}
	});

	$( 'a[name=loginAnchor]' ).click( function( e ) {
		e.preventDefault();
		if( $( 'div.signin_content' ).length > 0 ) {
			$( 'div.signin_content' ).lightbox();
		} else {
			alert( 'You\'ve already logged in!' );
		}
	});

	$( 'a[name=feedbackAnchor]' ).click( function( e ) {
		e.preventDefault();
		$( 'div.feedback_content' ).lightbox();
	});

	$( 'a[name=acceptt]' ).click( function( e ) {
		e.preventDefault();
		$( '#terms-lightbox' ).lightbox();
	});
	
	if( $( 'div.feedback_thanks_lightbox' ).length > 0 ) {
		$( 'div.feedback_thanks_lightbox' ).lightbox();
	}

	if( $( 'div.login-err' ).length > 0 ) {
		$( 'a[name=loginAnchor]' ).trigger( 'click' );
	}

	if( $( 'div.register_thanks_lightbox' ).length > 0 ) {
		$( 'div.register_thanks_lightbox' ).lightbox();
	}

	if( $( 'div.registration_content div.err-message' ).length > 0 ) {
		$( 'a[name=signupAnchor]' ).trigger( 'click' );
	}

	if( $( 'div.feedback_content div.err-message' ).length > 0 ) {
		$( 'a[name=feedbackAnchor]' ).trigger( 'click' );
	}

	$( 'input[name=get_world_movies]' ).click( function() {
		this.value == 'Y' ? $( 'div.feedback_show_comments select' ).attr( 'disabled', '' ) : $( 'div.feedback_show_comments select' ).attr( 'disabled', 'disabled' );
	}).filter( ':checked' ).trigger( 'click' );

	$( 'form.login_form' ).validate();

	$( 'form.ticker_form' ).validate();

	$( 'form.register_form' ).validate({
		rules: {
			user_password : {
				required : true,
				minlength : 5
			},
			confirm_user_password : {
				required : true,
				minlength : 5,
				equalTo : "#user_password"
			},
			user_login : {
				required : true,
				minlength : 3
			}
		},
		submitHandler : function() {
			if( $.trim( $( 'span.login-message' ).text() ) != 'Available' ) {
				alert( 'Login already exists, try another!' );
				$( 'input[name=user_login]' ).focus();
				return false;
			}
			$( 'form.register_form' ).bind( 'submit', function() {
				return true;
			}).submit();
		}
	});
	$( 'form.feedback_form' ).validate();

	$( 'input[name=user_login]' ).bind( 'keypress', function() {
		$( 'span.login-message' ).hide();
		$( 'a.login-avail' ).show();
	}).bind( 'blur', function() {
		$( 'a.login-avail' ).trigger( 'click' );
	});

	$( 'a.login-avail' ).click( function() {
		var userLogin = $.trim( $( 'input[name=user_login]' ).val() );
		if( userLogin == '' ) {
			return;
		}
		$.ajax( {
			url : '/ajax/check_login.php',
			type : 'post',
			data : 'l='+userLogin,
			success : function( resp ) {
				$( 'span.login-message' ).html( resp ).show();
				$( 'a.login-avail' ).hide();
			}
		});
	});

	
});

$(document).ready(function() {
	$('div.single_comment_div').each(function () {		
		var url = $(this).attr('rel');
		var title = $(this).attr('title');

		$(this).attr('meebo:tweet' , title);
		$(this).attr('meebo:url' , url);

		$(this).attr('rel' , '');
		$(this).attr('title', '');
	});
	//$('.read_more').html('<a href='+$(this).parent().attr('rel')+' meebo:title='+$(this).attr('title')+' meebo:url='+$(this).attr('rel')+' >read more</a>');
});
function show_country_cities( country_id)
{
	var div=document.getElementById( "city_div" );
	var postdata="country_id=" + country_id;
	$.ajax({type:"POST",url:"/ajax/show_country_cities.php",data: postdata, success: function( msg )
	{	
		div.innerHTML = msg;
	}});
}

function search()
{
	var search_string=document.getElementById("search-term").value;
	search_string=removeSpaces(search_string);
	search_string=trim(search_string);
	window.location="http://"+window.location.host+"/search?q="+search_string;
}
function trim (str, charlist)
{
	var whitespace, l = 0, i = 0;
	str += '';
	if (!charlist) {
		// default list
		whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
	} else {
		// preg_quote custom list
		charlist += '';
		whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
	}
	l = str.length;
	for (i = 0; i < l; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	l = str.length;
	for (i = l - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function removeSpaces(string)
{
	var nsText = string;
	nsText = nsText.replace(/(\n\r|\n|\r)/gm,"<1br />");
	nsText = nsText.replace(/\t/g," ");
	re1 = /\s+/g;
	nsText = nsText.replace(re1," ");
	re2 = /\<1br \/>/gi;
	nsText = nsText.replace(re2, "\n");
	return nsText;
}

jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar
				});
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				//jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};

if (typeof Meebo == 'undefined') {
	var M=Meebo=function(){(M._=M._||[]).push(arguments)},w=window,a='addEventListener',b='attachEvent',c='load',
	d=function(){M.T(c);M(c)},z=M.$={0:+new Date};
	M.T=function(a){z[a]=+new Date-z[0]};if(w[a]){w[a](c,d,false)}else{w[b]('on'+c,d)};M.v=3;
	(function(_){var d=document,b=d.body,c;if(!b){c=arguments.callee;
	return setTimeout(function(){c(_)},100)}var a='appendChild',c='createElement',
	m=b.insertBefore(d[c]('div'),b.firstChild),n=m[a](d[c]('m')),i=d[c]('iframe');
	m.style.display='none';m.id='meebo';i.frameBorder="0";n[a](i).id="meebo-iframe";
	function s(){return['<body onload=\'var d=document;d.getElementsByTagName("head")[0].',
	a,'(d.',c,'("script")).sr','c="//',_.stage?'stage-':'',
	'cim.meebo.com','/cim?iv=',M.v,'&network=',_.network,_.lang?'&lang='+_.lang:'',
	_.d?'&domain='+_.d:'','"\'></bo','dy>'].join('')}try{
	d=i.contentWindow.document.open();d.write(s());d.close()}catch(e){
	_.d=d.domain;i['sr'+'c']='javascript:d=document.open();d.write("'+s().replace(/"/g,'\\"')+'");d.close();'}M.T(1)})
    ({ network: 'utvworldmovies_ti59za', stage: false });    
}
