var defaultScreenshot1 = '';
var defaultScreenshot2 = '';
var defaultScreenshot3 = '';

if( parent.location.href == 'http://www.gazup.com/' || parent.location.href == 'http://www.gazup.com/beta-index/' )
{
	parent.location.href = location.href;
}

$( document ).ready(function(){
	$( '.show_more' ).click(function(){
		$( this ).css( 'display', 'none' );
		$( '.hidden_' + $( this ).attr( 'rel' ) ).slideDown( 'slow' );
		return false;
	});
	
	//----------------------------------------------
	// SHARE WIDGET
	//----------------------------------------------
	var selectedTab = 0;
	var is_clickable = 1;
	var tabs = {
		1:'share_forums',
		2:'share_website',
		3:'share_friends'
	};
	
	$( '#forumshare' ).click(function(){
		if( selectedTab != 1 && is_clickable )
		{
			is_clickable = 0;
			$( this ).addClass( 'selected' );
			$( '#websiteshare' ).removeClass( 'selected' );
			$( '#friendshare' ).removeClass( 'selected' );
			$( '#' + tabs[selectedTab] ).fadeOut( '', function(){
				$( '#share_forums' ).fadeIn( 1000, function(){
					is_clickable = 1;
					selectedTab = 1;
				});
			});
			
			if( selectedTab == 0 )
			{
				$( '#share_forums' ).fadeIn( 1000, function(){
					is_clickable = 1;
					selectedTab = 1;
				});
			}
		}
		
		return false;
	});
	
	$( '#websiteshare' ).click(function(){
		if( selectedTab != 2 && is_clickable )
		{
			is_clickable = 0;
			$( this ).addClass( 'selected' );
			$( '#forumshare' ).removeClass( 'selected' );
			$( '#friendshare' ).removeClass( 'selected' );
			$( '#' + tabs[selectedTab] ).fadeOut( '', function(){			
				$( '#share_website' ).fadeIn( 1000, function(){
					is_clickable = 1;
					selectedTab = 2;
				});
			});
			
			if( selectedTab == 0 )
			{
				$( '#share_website' ).fadeIn( 1000, function(){
					is_clickable = 1;
					selectedTab = 2;
				});
			}
		}
		
		return false;
	});
	
	$( '#friendshare' ).click(function(){
		if( selectedTab != 3 && is_clickable )
		{
			is_clickable = 0;
			$( this ).addClass( 'selected' );
			$( '#forumshare' ).removeClass( 'selected' );
			$( '#websiteshare' ).removeClass( 'selected' );
			$( '#' + tabs[selectedTab] ).fadeOut( '', function(){			
				$( '#share_friends' ).fadeIn( 1000, function(){
					is_clickable = 1;
					selectedTab = 3;
				});
			});
			
			if( selectedTab == 0 )
			{
				$( '#share_friends' ).fadeIn( 1000, function(){
					is_clickable = 1;
					selectedTab = 3;
				});
			}
		}
		
		return false;
	});
	
	
	var forumsCurrentTab = 1;
	// 1
	$( '#forums_with_thumb' ).click(function(){
		if( forumsCurrentTab != 1 )
		{
			forumsCurrentTab = 1;
			$( '#forum_without_thumbs' ).fadeOut( 'slow', function(){
				$( '#forum_with_thumbs' ).fadeIn( 'slow' );
				
				$( '#forums_with_thumb' ).text( 'Share file with thumbnail (selected)' );
				$( '#forums_without_thumb' ).text( 'Share file without thumbnail' );
			})
		}
		
		return false;
	})
	
	$( '#forums_without_thumb' ).click(function(){
		
		if( forumsCurrentTab != 2 )
		{
			forumsCurrentTab = 2;
			$( '#forum_with_thumbs' ).fadeOut( 'slow', function(){
				$( '#forum_without_thumbs' ).fadeIn( 'slow' );
				
				$( '#forums_with_thumb' ).text( 'Share file with thumbnail' );
				$( '#forums_without_thumb' ).text( 'Share file without thumbnail (selected)' );
			})
		}
		
		return false;
	})
	
	$( '.file_1' ).change(function(){
		$( '#fthumb1' ).val( $( '#fthumb1' ).val().replace( defaultScreenshot1, $( this ).val() + '_thumb.jpg' ) );
		defaultScreenshot1 = $( this ).val() + '_thumb.jpg';
	});
	
	$( '.file_2' ).change(function(){
		$( '#fthumb2' ).val( $( '#fthumb2' ).val().replace( defaultScreenshot2, $( this ).val() + '_thumb.jpg' ) );
		defaultScreenshot2 = $( this ).val() + '_thumb.jpg';
	});
	
	$( '.file_3' ).change(function(){
		$( '#fthumb3' ).val( $( '#fthumb3' ).val().replace( defaultScreenshot3, $( this ).val() + '_thumb.jpg' ) );
		defaultScreenshot3 = $( this ).val() + '_thumb.jpg';
	});
	
	var currentThumbForum = '_900.jpg';
	$( '.vselect' ).change(function(){
		mnew = ( $( this ).val() != 1500 ) ? '_' + $( this ).val() + '.jpg' : '.jpg';
		$( '#vthumb' ).val( $( '#vthumb' ).val().replace( currentThumbForum, mnew ) );
		$( '#preview_1' ).attr( 'alt', $( '#preview_1' ).attr( 'alt' ).replace( currentThumbForum + '/1/', mnew + '/1/' ) );
		currentThumbForum = mnew;
	})
	
	var currentThumbForum2 = '_900.jpg';
	$( '.vselect2' ).change(function(){
		mnew = ( $( this ).val() != 1500 ) ? '_' + $( this ).val() + '.jpg' : '.jpg';
		$( '#vthumb2' ).val( $( '#vthumb2' ).val().replace( currentThumbForum2, mnew ) );
		$( '#preview_2' ).attr( 'alt', $( '#preview_2' ).attr( 'alt' ).replace( currentThumbForum2 + '/2/', mnew + '/2/' ) );
		currentThumbForum2 = mnew;
	})
	
	var currentThumbWebsite = '.jpg';
	$( '.wselect' ).change(function(){
		mnew = ( $( this ).val() != 1500 ) ? '_' + $( this ).val() + '.jpg' : '.jpg';
		$( '#wvideo' ).val( $( '#wvideo' ).val().replace( currentThumbWebsite, mnew ) );
		$( '#preview_3' ).attr( 'alt', $( '#preview_3' ).attr( 'alt' ).replace( currentThumbWebsite + '/3/', mnew + '/3/' ) );
		currentThumbWebsite = mnew;
	})
});

function setDefaultScreenshot( pic, num )
{
	if( num == 1 )
	{
		defaultScreenshot1 = pic;
	}
	else if( num == 2 )
	{
		defaultScreenshot2 = pic;
	}
	else
	{
		defaultScreenshot3 = pic;
	}
}

function startChecking( id )
{
	checkScreenshots( id );	
}

var myJson;
function checkScreenshots( id )
{
	$.post( 'http://www.gazup.com/ajax-screencheck/' + id + '/', { 'foo' : 'bar' }, function( json ){

			var i = 0;
			var k = 0;
			$( '.screenshot' ).each(function(){
				if( k < json.screenshots.length )
				{	
					if( json.screenshots[i].overview == 1 && $( '.overview_avail' ).length != 1 )
					{
						$( this ).children( 'img' ).attr( 'src', 'screenshots/' + json.screenshots[i].filename + '_thumb.jpg' );
						$( this ).attr( 'href', 'screenshots/' + json.screenshots[i].filename + '.jpg' );	
						
						if( json.screenshots[i].overview == 1 )
						{
							location.reload();	
						}
					}
					else if( json.screenshots[i].overview != 1 )
					{
						$( this ).children( 'img' ).attr( 'src', 'screenshots/' + json.screenshots[i].filename + '_thumb.jpg' );
						$( this ).attr( 'href', 'screenshots/' + json.screenshots[i].filename + '.jpg' );	
					}
					
					i++;
				}
				k++;
			});
	}, 'json');
	
	setTimeout( "checkScreenshots( '" + id + "' );", 5000 );	
}
