<?php get_header(); ?>

<?php
$aOptions = SofaShoppr::initOptions( false );
$singleview_imgsize = $aOptions[ 'singleview_imgsize' ];
$entrycurrency = $aOptions[ 'entrycurrency' ];
$gdc = $aOptions[ 'gdc' ];
$use_stock_inventory = urlencode( $aOptions[ 'use_stock_inventory' ] );
?>

<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>

<div id="content">

    <div class="article">
    
    <h1 class="itemtitleh1"><?php the_title(); ?></h1>
    <h1 class="titleprice" id="tp">
        <?php
		$item_prices_list = 0;
		$item_prices = get_post_meta( $post->ID, 'price', true );
		$item_prices_array = explode( "\n", $item_prices );
		if( $item_prices_array ) {
			$price = number_format( $item_prices_array[ 0 ], 2, '.', ',' );
			$item_prices_list = implode( ',', $item_prices_array );
			$item_prices_list = urlencode( $item_prices_list );
		}
		echo $entrycurrency . ' <span id="pricer">' . $price . '</span>';
		?>
    </h1>
    <div class="post-info">
        <span class="category"><?php the_category( ', ' ); ?></span>
        <?php if( $gdc == 'no' ) { ?>
        <span class="comment-count"> &bull; 
        <?php comments_popup_link( '<span>0</span> ' . __( "reviews", "sofa_shoppr" ), '<span>1</span> ' . __( "review", "sofa_shoppr" ), '<span>%</span> ' . __( "reviews", "sofa_shoppr" ) ); ?> 
        </span>
        <?php } ?>
        <?php edit_post_link( 'EDIT', ' &bull; <span class="comment-count">', '</span>' ); ?>
    </div>
    <div class="post-info">
    <?php the_tags( __( "Tags: ", "sofa_shoppr" ), ', ', '' ); ?>
    </div>
    
	<?php 
	$content = get_the_content();
	$content = apply_filters( 'the_content', $content );
	$content = str_replace( ']]>', ']]&gt;', $content );
	$content = preg_replace( '|(class=[\'"]wp-caption.*?[\'"]).*?(style=[\'"](.*?)[\'"]).*?|i', '$1', $content );
	// extract images
	$arr_of_images = array();
	$required_file_path = '';
	$arrImages =& get_children( 'post_type=attachment&post_mime_type=image&orderby=menu_order&order=DESC&post_parent=' . $post->ID );
	if( $arrImages ) {
		// reverse, preserve keys
		$arrImages = array_reverse( $arrImages, true );
		$arrKeys = array_keys( $arrImages );
		$i = 0;
		while( $i < count( $arrImages ) ) {
			// first image will always be of a medium size...
			if( $i == 0 ) {
				$the_big_one = wp_get_attachment_url( $arrKeys[ $i ] );
				$medium_array = image_downsize( $arrKeys[ $i ], 'medium' );
				$required_file_path = $medium_array[ 0 ];
				$entire_link = '<a href="' . $the_big_one . '" class="nyroModal" rel="gal"><img src="' . $required_file_path . '" class="productimagebig" alt="" /></a>';
				array_push( $arr_of_images, $entire_link );
			} else {
				//get thumbnail otherwise
				$the_big_one = wp_get_attachment_url( $arrKeys[ $i ] );
				$last_class = '';
				if( $i % 2 == 0 ) $last_class = ' last';
				$required_file_path = wp_get_attachment_thumb_url( $arrKeys[ $i ] );
				$entire_link = '<a href="' . $the_big_one . '" class="nyroModal" rel="gal"><img src="' . $required_file_path . '" class="productimagesmall' . $last_class . '" alt="" /></a>';
				array_push( $arr_of_images, $entire_link );
			}
			
			$i ++;
		}
	}
	
	// image container
	echo '<div id="productimages">';
	if( count( $arr_of_images ) > 0 ) {
		$z = 0;
		$al = count( $arr_of_images );
		while( $z < $al ) {
			echo $arr_of_images[ $z ];
			$z ++;
		}
	} else echo '&nbsp;';
	// custom fields
	if( $use_stock_inventory == 'yes' ) {
		$custom_fields = get_post_custom( (int)$post->ID );
		$item_stock    = $custom_fields[ 'stock' ][ 0 ];
		$item_stock_list = 0;
		if( $item_stock != NULL ) {
			$items_in_stock = get_post_meta( $post->ID, 'stock', true );
			$count_item_stock = 0;
			$item_stock_array = explode( "\n", $items_in_stock );
			$item_stock_list = implode( ',', $item_stock_array );
			$item_stock_list = urlencode( $item_stock_list );
			$fin_stock_val = intval( $custom_fields[ 'stock' ][ 0 ] );
			if( $fin_stock_val < 0 ) $fin_stock_val = 0;
			echo '<div class="stockinfo"><p>';
			echo __( "In stock", "sofa_shoppr" ) . ': <span id="stocknum">' . $fin_stock_val . '</span>';
			echo '</p></div>';
		}
	}
	
	echo '</div>';
	
	// content
	echo '<div id="productinfo">';
	$content = preg_replace( '|<a.*?href=[\'"](.*?)[\'"].*?>.*?<img.*?src=[\'"](.*?)[\'"].*?alt=[\'"](.*?)[\'"].*?><.*?a>|i', '', $content );
	echo $content;
	// take care about the form
	$item_sizes = get_post_meta( $post->ID, 'size', true );
	$item_colors = get_post_meta( $post->ID, 'color', true );
	// form begin
	echo '<form method="post" id="addbasket" action="' . get_bloginfo( 'template_directory' ) . '/add_item_save.php' . '" class="nyroModal">';
	// hidden stuff
    echo '<p style="visibility: hidden;">';
    echo '<input type="hidden" name="itemid" id="itemid" value="' . $post->ID . '" />';
	echo '<input type="hidden" name="itemp" id="itemp" value="" />';
	echo '<input type="hidden" name="itemst" id="itemst" value="" />';
	echo '</p>';
	// end hidden
	// size - if any
	if( $item_sizes ) {
		$count_item_sizes = 0;
		$item_sizes_array = explode( "\n", $item_sizes );
		echo '<p class="displayinline">';
		echo '<label for="itemsize">' . __( "Select size", "sofa_shoppr" ) . ': </label>';
		echo '<select id="itemsize" name="itemsize" size="1" tabindex="1">';
		foreach( $item_sizes_array as $size ) {
			if( $count_item_sizes == 0 ) $add_str = ' selected="selected" ';
			else $add_str = ' ';
			$option = '<option' . $add_str . 'value="' . $size . '" rel="' . $count_item_sizes . '"> - ';
			$option .= $size;
			$option .= ' - </option>';
			echo $option;
			$count_item_sizes ++;
		}
		echo '</select>';
		echo '</p>';
	}
	// end size
	// color - if any
	if( $item_colors ) {
		$count_item_colors = 0;
		$item_colors_array = explode( "\n", $item_colors );
		echo '<p class="displayinline">';
		echo '<label for="itemcolor">' . __( "Select color", "sofa_shoppr" ) . ': </label>';
		echo '<select id="itemcolor" name="itemcolor" size="1" tabindex="2">';
		foreach( $item_colors_array as $color ) {
			if( $count_item_colors == 0 ) $add_str = ' selected="selected" ';
			else $add_str = ' ';
			$option = '<option' . $add_str . 'value="' . $color . '"> - ';
			$option .= $color;
			$option .= ' - </option>';
			echo $option;
			$count_item_colors ++;
		}
		echo '</select>';
		echo '</p>';
	}
	// end color
	// quantity
    echo '<p class="displayinline">';
    echo '<label for="itemamount">' . __( "Quantity", "sofa_shoppr" ) . ': </label>';
    echo '<input class="small" type="text" name="itemamount" id="itemamount" size="5" value="1" tabindex="3" onkeydown="krak_IEnter();" />';
    echo '</p>';
	// end quantity
	// buy button
    echo '<p class="basketbutton displayinline">';
    echo '<button type="submit" id="submit" value="" tabindex="4"><span>' . __( "ADD TO BASKET", "sofa_shoppr" ) . '</span></button>';
    echo '</p>';
	// end buy button
	echo '</form>';
	// form end
	echo '</div>';
    ?>
    
    </div>
    
    <div class="pnpostcontainer">
    <p class="browse-post left">
        <span class="small"><?php _e( "PREVIOUS:", "sofa_shoppr" ); ?></span>
        <span class="big"><?php previous_post_link( '%link', '%title', TRUE ); ?></span>
    </p>
    <p class="browse-post right">
        <span class="small"><?php _e( "NEXT:", "sofa_shoppr" ); ?></span>
        <span class="big"><?php next_post_link( '%link', '%title', TRUE ); ?></span>
    </p>
    </div>

	<?php
    // related entries based on tags
    $backup = $post; // backup the current object
    $tags = wp_get_post_tags( $post->ID );
    $tagIDs = array();
	if( $tags ) {
		$tagcount = count( $tags );
		for( $i = 0; $i < $tagcount; $i++ ) {
			$tagIDs[ $i ] = $tags[ $i ]->term_id;
		}
		$args = array(
			'tag__in' => $tagIDs,
			'post__not_in' => array( $post->ID ),
			'showposts' => -1,
			'caller_get_posts' => 1
		);
		$my_query = new WP_Query( $args );
		// how many related posts? $my_query->post_count;
		if( $my_query->have_posts() ) : 
	?>
    
    <div class="related">
    <h1 class="itemtitleh1"><?php _e( "Related products", "sofa_shoppr" ); ?></h1>
    	<div class="scrollable">
            <ul class="images items">
	<?php 
    $count_items = 0;
    while( $my_query->have_posts() ) : $my_query->the_post(); 
	
		$image = get_children( 'post_parent=' . $post->ID . '&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image' );
		
		if( $image ) {
			$keys = array_keys( $image );
			$path = wp_get_attachment_thumb_url( $keys[ 0 ] );
        	echo '<li>';
        	echo '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow"><img src="' . $path . '" alt="" /></a>';
			echo '<p class="relatedp"><a href="' . get_permalink( $post->ID ) . '" title="' . get_the_title() . '">' . get_the_title() . '</a></p>';
			echo '<p class="relatedp"><strong>' . $entrycurrency . ' ' . number_format( get_post_meta( $post->ID, 'price', true ), 2, '.', ',' ) . '</strong></p>';
			echo '</li>';

		}
		$count_items ++;
		
	endwhile; 
	$post = $backup;
	wp_reset_query();
	?>
    
            </ul>
        </div>
        <?php if( $my_query->post_count > 4 ) { ?>
        <span class="browse left prevPage"><a href="#l">&lt;</a></span>
        <span class="browse right nextPage"><a href="#r">&gt;</a></span>
         <?php } ?>
    </div>
    
    <?php 
	endif;
	}
	?>
    
    <?php if( 'open' == $post->comment_status && $gdc == 'no' ) comments_template(); ?>
    
</div>

<?php endwhile; else: ?>

<div id="content">

    <div class="article">
    
    <h1><?php _e( "Error!", "sofa_shoppr" ); ?></h1>
    <div class="post-info">
        <span class="date"><?php _e( "Page not found", "sofa_shoppr" ); ?></span>
    </div>
    
    <p><?php _e( "The Page you are looking for doesn't exist!", "sofa_shoppr" ); ?></p>
    
    </div>
    
</div>
    
<?php endif; ?>

<script type="text/javascript">

jQuery( document ).ready( function() {
	
	var obey_elem = jQuery( '#itemsize option:selected' ).attr( 'rel' );
	updatr( obey_elem );
	
	jQuery( '#itemsize' ).change( function() {
		var obey_elem = jQuery( 'option:selected', this ).attr( 'rel' );
		updatr( obey_elem );
	});
	
} );

function updatr( obey_elem ) {
	var in_array = parseInt( obey_elem, 10 ); // my array position
	// handle stock
	var use_stock = decodeURIComponent( "<?php echo $use_stock_inventory; ?>" );
	if( use_stock == "yes" ) {
		var in_stock = decodeURIComponent( "<?php echo $item_stock_list; ?>" );
		in_stock = in_stock.replace( /\+/g, ' ' );
		var ar_stock = in_stock.split( ',' );
		var stockvald = ( !ar_stock[ in_array ] ) ? in_stock : ar_stock[ in_array ];
		if( stockvald < 0 ) stockvald = 0;
		jQuery( '#stocknum' ).html( stockvald );
		if( stockvald <= 0 ) {
			jQuery( '#submit' ).attr( 'disabled', 'disabled' );
			jQuery( '#submit' ).addClass( 'outofstock' );
			jQuery( '#submit' ).html( "<span><?php _e( "OUT OF STOCK", "sofa_shoppr" ); ?></span>" );
		} else {
			jQuery( '#submit' ).removeAttr( 'disabled' );
			jQuery( '#submit' ).removeClass( 'outofstock' );
			jQuery( '#submit' ).html( "<span><?php _e( "ADD ITEM", "sofa_shoppr" ); ?></span>" );
			jQuery( '#itemst' ).attr( 'value', stockvald );
		}
	} else {
		jQuery( '#itemst' ).attr( 'value', 1 );
	}

	//handle prices
	var prices = decodeURIComponent( "<?php echo $item_prices_list; ?>" );
	prices = prices.replace( /\+/g, ' ' );
	var ar_pri = prices.split( ',' );
	var pricevald = ( !ar_pri[ in_array ] ) ? prices : ar_pri[ in_array ];
	jQuery( '#pricer' ).html( pricevald );
	jQuery( '#itemp' ).attr( 'value', pricevald );
	if( ar_pri.length > 1 ) jQuery( '#tp' ).shake( 2, 10, 100 );
}

jQuery.fn.shake = function( intShakes, intDistance, intDuration, easing ) {
	var easing = ( jQuery.easing.def && easing ) ? easing : 'swing';
	return this.each( function() {
		var origLeft = parseInt( jQuery( this ).css( 'right' ), 10 );
		for( var x = 1; x <= intShakes; x++ ) {
			jQuery( this ).animate( { 'right': origLeft - ( intDistance * -1 ) + 'px' }, ( intDuration / intShakes ) / 4, easing ).animate( { 'right': origLeft + intDistance + 'px' }, ( intDuration / intShakes ) / 2, easing ).animate( { 'right': origLeft + 'px' }, ( intDuration / intShakes ) / 4, easing );
		}
	} );
};

	function krak_IEnter() {    
		if( event.keyCode == 13 ) {        
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}

</script>

<?php include( TEMPLATEPATH . '/sidebar_single.php' ); ?> 

<?php get_footer(); ?>
